java - 如何让 JButton 运行依赖于用户在 JComboBox 中选择的内容的类

标签 java eclipse swing jbutton jcombobox

我在 Eclipse 中创建了一个用户界面,要求用户在 JComboBox 中选择一个选项,然后单击 JButton 来启动事件。根据他们选择的选项,将运行不同的类并输出其结果。一切都设置得很好,JButtons 可以自行工作,但我无法让它们响应 JComboBox 中的更改。

这是启动界面的代码和类的示例(完整的代码更长,包含更多按钮等,因此有额外的列和行):

package projectFinal;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;


public class test extends JFrame {

  public test() {

    setTitle("BIM Representative GUI");

    JPanel pane = new JPanel(new BorderLayout());

    int numberOfRows = 8;
    int numberOfColumns = 4;
    pane.setLayout(new GridLayout(numberOfRows, numberOfColumns));

    JLabel metric1label = new JLabel(" Greenhouse Gas Emissions: ");
    pane.add(metric1label);
    JLabel metric1alabel = new JLabel("  ");
    pane.add(metric1alabel);
    String[] pollutants = { "Total","CO2", "CH4","N2O"};
    final JComboBox<String> cb1 = new JComboBox<String>(pollutants);
    cb1.setVisible(true);
    getContentPane().add(cb1);
    JButton button1 = new JButton("Check");
    pane.add(button1);

    getContentPane().add(pane);

    pack();

    button1.setToolTipText("This button will show the Greenhouse Gas Emissions");

    button1.addActionListener(new MyActionListener1());    

  }
  public class MyActionListener1 implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            String choice = (String)cb1.getSelectedItem();
            if(choice.equals("Total"))
        {
            GHGEmissions.UI();
        }
        if(choice.equals("CO2"))
        {
            CO2Emissions.UI();
        }
        if(choice.equals("CH4"))
         {
             CH4Emissions.UI();
         }
        if(choice.equals("N2O"))
             {
             N2OEmissions.UI();
        }
             }
}}

以及运行界面的代码:

package projectFinal;

import projectFinal.test;

public class testRun {
      public static void main(String[] args) {
          test view = new test();
        view.setVisible(true);
      }
    } 

JComboBox 根本不会出现在界面上(当字符串选择和 if 语句被删除时,它就会出现)。有谁知道我如何解决这个问题,以便它将根据 JComboBox 运行不同的类。

唯一显示问题的部分是该行中的 cb1:

            String choice = (String)cb1.getSelectedItem();

谢谢

最佳答案

cb1 是构造函数中的局部变量,因此您需要将 ActionListener 声明为 annoynimus 类才能访问 cb1 变量,

button1.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            // you code.

        }
    });

还需要使用 .equals() 方法而不是 == 来比较字符串。

所以尝试将您的代码更改为:

if(choice.equals("Total"))
{
    GHGEmissions.UI();
}

引用this了解更多详情。

关于java - 如何让 JButton 运行依赖于用户在 JComboBox 中选择的内容的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23021474/

相关文章:

java - 类对象数组中元素的存储不当

java - struts2+tiles2 java.lang.ClassNotFoundException : org. apache.struts.taglib.bean.CookieTei

java - JSF 2.0 : how can I dynamically generate Input component

java - Xtext:这个异常是什么意思?

java - 关于安装eclipse插件的问题

java - JComboBox 在 GridBagLayout 中插入后更改 JTextField 宽度

java - 对匿名内部类使用最终的 1 元素数组

java - StringBuffer 和appender 方法

java - 当按下的键不是数字时,如何正确使用此事件?

java - 如何在java中的JFrame中显示自定义字体文本?