0

I am very new to java and not aware of the methods I can use to achieve what I'm trying to do. I need to make a program that simulates a light switch. One button that turns the light on and off. I set the background colour to dark gray before the event is fired and after fired the background color is set to yellow. The problem I have is when the background is yellow how can I change it back to dark gray using the same button?

My code:

import javax.swing.JApplet;
import javax.swing.JButton;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Question2 extends JApplet implements ActionListener {
    public void init() {
        Container contentPane = getContentPane();
        contentPane.setBackground(Color.DARK_GRAY);

        contentPane.setLayout(new FlowLayout());

        JButton OnOffSwitch = new JButton("On/Off");
        contentPane.add(OnOffSwitch);
        OnOffSwitch.addActionListener(this);    
    }

    public void actionPerformed(ActionEvent e) {
        Container contentPane = getContentPane();

        if (e.getActionCommand().equals("On/Off"))
            contentPane.setBackground(Color.YELLOW);
        else 
            contentPane.setBackground(Color.DARK_GRAY);
    }
}
sandrstar
  • 12,503
  • 8
  • 58
  • 65
Bropex
  • 11
  • 4
  • 2
    *"One button that turns the light on and off."* Use a `JToggleButton` or `JCheckBox` for this. – Andrew Thompson May 22 '13 at 09:02
  • 1
    *`JApplet`* Why code an applet? If it is due due to spec. by teacher, please refer them to [Why CS teachers should stop teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). – Andrew Thompson May 22 '13 at 09:04
  • Although the boolean is the way to go (better even, follow what @andrew suggested), you were actually on the right path yourself. Instead of having the label as `On/Off` you could've initialized it with just `Turn On` and in the `actionPerformed` if it is found to be equal to `Turn On`, change the colour to Yellow and also set the new label as `Turn Off`. Wouldn't recommend using it though, this was just for your knowledge :) – Niks May 22 '13 at 09:10

2 Answers2

3

E.G. (Using images from this answer).

JToggleButton with icons

import java.awt.*;
import javax.swing.*;
import javax.imageio.ImageIO;
import java.net.URL;

class ToggleButtons {

    public static void main(String[] args) throws Exception {
        URL offUrl = new URL("https://i.stack.imgur.com/gJmeJ.png");
        URL onUrl = new URL("https://i.stack.imgur.com/5v2TX.png");
        final Image offImg = ImageIO.read(offUrl);
        final Image onImg = ImageIO.read(onUrl);
        Runnable r = new Runnable() {

            @Override
            public void run() {
                int s = 5;
                JPanel gui = new JPanel(new GridLayout(s, s));
                Icon offIcon = new ImageIcon(offImg);
                Icon onIcon = new ImageIcon(onImg);

                for (int ii=0; ii<s*s; ii++) {
                    JToggleButton tb = new JToggleButton(offIcon, ii%2==0);
                    tb.setSelectedIcon(onIcon);
                    gui.add(tb);
                }

                JOptionPane.showMessageDialog(null, gui);
            }
        };
        // Swing GUIs should be created and updated on the EDT
        // http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html
        SwingUtilities.invokeLater(r);
    }
}
Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1
    +1 for accessing images via `URL`; see also this related [example](http://stackoverflow.com/a/6036048/230513). – trashgod May 22 '13 at 09:44
2

Have a boolean field called on set to false

boolean on = false;

and in your actionPerformed have a flip logic something like this

public void actionPerformed(ActionEvent e)
{
    Container contentPane = getContentPane();

    if (!on) {
            contentPane.setBackground(Color.YELLOW);
            on = true;
    }
    else  {
        contentPane.setBackground(Color.DARK_GRAY);
        on = false;
    }
}
sanbhat
  • 17,522
  • 6
  • 48
  • 64