Java swing组合框actionListner

Java swing组合框actionListner,java,swing,user-interface,combobox,Java,Swing,User Interface,Combobox,我正在使用基本的swing组件和ActionListener编写一个程序。我已经让它大部分工作,但我似乎无法让我的组合框动作列表器工作,我做错了什么 据我所知,字符串应该传递给actionlistner方法,但它在运行时不起作用 public class LightControl extends JFrame implements ActionListener { private JButton on, off, twentyWatt, fortyWatt, sixtyWatt; privat

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

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


}   




 }

JComboBox不是这样工作的

  • 对单独的组件使用单独的侦听器
  • 在这种情况下,为JComboBox提供自己的ActionListener。匿名内部类将很好地工作
  • 在其侦听器中,获取所选项目,对其调用
    toString()
    ,您将获得组合框的所选字符串
e、 g

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

     @Override
     public void actionPerformed(ActionEvent e) {
        String selection = lightTimer.getSelectedItem().toString();
        // use the String here
     }
});

由于您没有提供可执行示例,所以很难判断实际问题是什么,但看看您的代码,我想问题在于您没有为控件指定操作命令。您获得的操作命令不是组件的文本,而是使用
component.setActionCommand(…)
设置的字段。当触发组合框上的
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();
        }
    }
}

很抱歉,但这使得丑陋的开关板操作方法更加丑陋。我们应该努力避免将太多的东西合并到一个ActionListener中,因为这会造成调试和更新的噩梦。我完全同意这一评论,但上面的代码有助于OP继续开发。然而,一旦这个方法奏效,它应该被清理掉。我不同意,因为创建正确的代码要比以后尝试清理代码容易得多。此外,对所选项目调用
toString()
比将其转换为字符串安全得多。后者具有潜在的危险性,尤其是当JComboBox所持有的对象发生变化并变成非字符串时。更好的方法是使用一个通用的组合框,这取决于你在开始的时候有多少时间。toString()可以返回null,这也很危险。最好的选择是支持Java 1.7泛型的组合,比如JComboBox。请学习Java命名约定并坚持它们。非常感谢您在如何解决问题方面提供的建设性帮助……哦,等等。。。