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);
}
}
我想问一下,因为我的程序完全按照它的设想做了,但是我似乎无法将循环更改label1变为红色,同时更改了buttonPanel背景颜色的任何建议吗?
答案 0 :(得分:4)
没有真正检查过代码,但不应该
if (event.getSource() == red)
label1.setText("Red");
buttonPanel.setBackground(Color.red);
是
if (event.getSource() == red)
{
label1.setText("Red");
buttonPanel.setBackground(Color.red);
}
这就是为什么总是使用{}的好主意,即使在单行条件下也是如此。
答案 1 :(得分:3)
从我所看到的情况来看,您需要使用if
标记封装{}
- 语句,请尝试以下操作:
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);
}
}
}
希望它能解决你的问题!