我正在编写一个使用基本摆动组件和动作监听器的程序。我已经让它大部分工作了,但我似乎无法让我的组合框动作监听器工作,我做错了什么?
据我了解,字符串应该被传递给 actionlistner 方法,但它在运行时不起作用!
public class LightControl extends JFrame implements ActionListener
{
private JButton on, off, twentyWatt, fortyWatt, sixtyWatt;
private JComboBox lightTimer;
private String [] comboSelection = new String[]{"Morning","Evening","All day"};
private JTextField statusText;
private LightBulb lightbulb;
private JPanel frameContainer;
private JPanel wattFrame;
private JPanel toggleFrame;
private JPanel comboFrame;
public LightControl()
{
super("Lightbulb");
lightbulb=new LightBulb();
Container container = getContentPane();
//FlowLayout layout=new FlowLayout();
//instantiate
statusText=new JTextField("Select an option");
statusText.setSize(100, 50);
statusText.setEditable(false);
lightTimer = new JComboBox(comboSelection);
on = new JButton("On");
off = new JButton("Off");
twentyWatt=new JButton("20W");
fortyWatt=new JButton("40W");
sixtyWatt=new JButton("60W");
//right hand side frames
comboFrame=new JPanel();
comboFrame.add(lightTimer);
toggleFrame=new JPanel();
toggleFrame.setLayout(new GridLayout(1, 2));
toggleFrame.add(on);
toggleFrame.add(off);
wattFrame=new JPanel();
wattFrame.setLayout(new GridLayout(1, 3));
wattFrame.add(twentyWatt);
wattFrame.add(fortyWatt);
wattFrame.add(sixtyWatt);
frameContainer=new JPanel();
frameContainer.setLayout(new GridLayout(3,3));
frameContainer.add(toggleFrame);
frameContainer.add(wattFrame);
frameContainer.add(comboFrame);
container.add(frameContainer, BorderLayout.EAST);
container.add(statusText);
//actions
on.addActionListener(this);
off.addActionListener(this);
twentyWatt.addActionListener(this);
fortyWatt.addActionListener(this);
sixtyWatt.addActionListener(this);
lightTimer.addActionListener(this);
setSize(600, 400);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
String Action = e.getActionCommand();
if (Action.equals ("On"))
{
lightbulb.setState(true);
twentyWatt.setEnabled(true);
fortyWatt.setEnabled(true);
sixtyWatt.setEnabled(true);
lightTimer.setEnabled(true);
statusText.setText("\t"+lightbulb.toString());
this.repaint();
}
else if (Action.equals ("Off"))
{
lightbulb.setState(false);
twentyWatt.setEnabled(false);
fortyWatt.setEnabled(false);
sixtyWatt.setEnabled(false);
lightTimer.setEnabled(false);
statusText.setText("\t"+lightbulb.toString());
this.repaint();
}
else if (Action.equals ("20W"))
{
lightbulb.setWattage(20);
statusText.setText("\t"+lightbulb.toString());
this.repaint();
}
else if (Action.equals ("40W"))
{
lightbulb.setWattage(40);
statusText.setText("\t"+lightbulb.toString());
this.repaint();
}
else if (Action.equals ("60W"))
{
lightbulb.setWattage(60);
statusText.setText("\t"+lightbulb.toString());
this.repaint();
}
else if (Action.equals ("Morning"))
{
lightbulb.setTime("Morning");
statusText.setText("\t"+lightbulb.toString());
this.repaint();
}
else if (Action.equals ("Evening"))
{
lightbulb.setTime("Evening");
statusText.setText("\t"+lightbulb.toString());
this.repaint();
}
else if (Action.equals ("All day"))
{
lightbulb.setTime("All day");
statusText.setText("\t"+lightbulb.toString());
this.repaint();
}
}
}