Java swing jcombobox actionListener

时间:2012-11-03 17:47:01

标签: java swing user-interface combobox

我正在使用基本的swing组件和actionlisteners编写程序。我已经完成了大部分工作,但我似乎无法让我的组合框动作列表工作,我做错了什么?

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


}   




 }

3 个答案:

答案 0 :(得分: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
     }
});

答案 1 :(得分:0)

有点难以分辨出实际问题是什么,因为你没有提供可执行的例子,但是查看你的代码我想问题是你没有为你的控件指定action命令。您获取的操作命令不是组件的文本,而是使用component.setActionCommand(...)设置的字段。

答案 2 :(得分: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();
        }
    }
}