Have a problem with my traffic light loop [closed]
That's my support class
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TrafficLightPanel extends JPanel
{
private JButton red, amber, green, change;
private JLabel label, label1;
private JPanel buttonPanel;
public TrafficLightPanel ()
{
red = new JButton ("Red");
amber = new JButton ("Amber");
green = new JButton ("Green");
change = new JButton ("Change");
ButtonListener listener = new ButtonListener();
red.addActionListener (listener);
amber.addActionListener (listener);
green.addActionListener (listener);
change.addActionListener (listener);
buttonPanel = new JPanel();
buttonPanel.setPreferredSize (new Dimension(80, 390));
buttonPanel.setBackground (Color.white);
label = new JLabel ("Button Panel");
buttonPanel.add (label);
buttonPanel.add (red);
buttonPanel.add (amber);
buttonPanel.add (green);
buttonPanel.add (change);
label1 = new JLabel ("last pressed");
buttonPanel.add (label1);
setPreferredSize (new Dimension(200, 400));
setBackground (Color.blue);
add(buttonPanel);
LightPanel panel = new LightPanel();
add(panel);
panel.setPreferredSize (new Dimension(80, 390));
panel.setBackground (Color.cyan);
}
private class ButtonListener implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
if (event.getSource() == red)
label1.setText("Red");
buttonPanel.setBackground(Color.red);
if (event.getSource() == amber)
label1.setText("Amber");
buttonPanel.setBackground(Color.orange);
if (event.getSource() == green)
label1.setText("Green");
buttonPanel.setBackground(Color.green);
if(event.getSource() == change)
label1.setText("Change");
buttonPanel.setBackground(Color.white);
}
}
private class LightPanel extends JPanel
{
public void paintComponent (Graphics page)
{
super.paintComponent(page);
page.setColor(Color.red);
page.fillOval(15, 30, 40, 40);
page.setColor(Color.orange);
page.fillOval(15, 90, 40, 40);
page.setColor(Color.green);
page.fillOval(15, 150, 40, 40);
}
}
}
And that's my application class
import javax.swing.JFrame;
public class TrafficLight
{
public static void main (String[] args)
{
JFrame frame = new JFrame("Traffic Light");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new TrafficLightPanel());
frame.pack();
frame.setVisible(true);
}
}
I wanted to ask as my program does exactly what it's suppose to do, however i can't seem to get the loop change label1 to red and at the same time change the buttonPanel backgr开发者_运维技巧ound color any suggestions for that?
Not really checked the code , but shouldn't
if (event.getSource() == red)
label1.setText("Red");
buttonPanel.setBackground(Color.red);
be
if (event.getSource() == red)
{
label1.setText("Red");
buttonPanel.setBackground(Color.red);
}
This is why it's a good idea to always use {} , even in single-line conditionals .
From what I can see, you need to encapsulate your if
-statements with {}
tags, try this:
private class ButtonListener implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
if (event.getSource() == red)
{
label1.setText("Red");
buttonPanel.setBackground(Color.red);
}
if (event.getSource() == amber)
{
label1.setText("Amber");
buttonPanel.setBackground(Color.orange);
}
if (event.getSource() == green)
{
label1.setText("Green");
buttonPanel.setBackground(Color.green);
}
if(event.getSource() == change)
{
label1.setText("Change");
buttonPanel.setBackground(Color.white);
}
}
}
Hope it solves your question!
精彩评论