-1

我正在编写一个使用基本摆动组件和动作监听器的程序。我已经让它大部分工作了,但我似乎无法让我的组合框动作监听器工作,我做错了什么?

据我了解,字符串应该被传递给 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();
    }


}   




 }
4

3 回答 3

2

这不是 JComboBox 的工作方式。

  • 对单独的组件使用单独的侦听器。
  • 在这种情况下,给 JComboBox 自己的 ActionListener。匿名内部类会很好地工作。
  • 在它的侦听器中,获取选定的项目,调用toString()它,您将拥有组合框的选定字符串。

例如,

// lightTimer.addActionListener(this);
lightTimer.addActionListener(new ActionListener() {

     @Override
     public void actionPerformed(ActionEvent e) {
        String selection = lightTimer.getSelectedItem().toString();
        // use the String here
     }
});
于 2012-11-03T17:54:23.017 回答
0

由于您没有提供可执行示例,因此很难说出实际问题是什么,但是查看您的代码,我猜问题是您没有为控件指定操作命令。您获取的操作命令不是组件的文本,而是使用component.setActionCommand(...).

于 2012-11-03T17:55:49.073 回答
0

actionPerformed组合框上的事件被触发时,e.getActionCommand()值为comboBoxChanged。我将您actionPerformed方法中的代码更改为下面的代码,它确实运行了您期望它运行的代码:

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("comboBoxChanged")) {
        String item = (String) lightTimer.getSelectedItem();
        if (item.equals("Morning")) {
            lightbulb.setTime("Morning");
            statusText.setText("\t"+lightbulb.toString());
            this.repaint();
        } else if (item.equals("Evening")) {
            lightbulb.setTime("Evening");
            statusText.setText("\t"+lightbulb.toString());
            this.repaint();
        } else if (item.equals("All day")) {
            lightbulb.setTime("All day");
            statusText.setText("\t"+lightbulb.toString());
            this.repaint();
        }
    }
}
于 2012-11-03T17:57:57.197 回答