【问题标题】:Button ActionListener runs both combo box choices at the same time why?Button ActionListener 同时运行两个组合框选项,为什么?
【发布时间】:2014-03-28 07:46:10
【问题描述】:

您好,我的组合框有问题。在我制作我的面板的应用程序中,有一个带有两个选项的组合框和一个组合框旁边的按钮,用于继续在组合框中选择的选项,但是如果语句运行并且不,我不知道为什么。

组合框代码很简单 private JComboBox mainChoice = new JComboBox();

mainChoice.addItem("") 等...

class mainPanelGoButtonListener implements ActionListener
{
    public void actionPerformed(ActionEvent ae) 
    {
        String choice = (String)mainChoice.getSelectedItem();

        System.out.printf(choice);

        if(choice == "View Passenger Details");
        {
            JTextField first = new JTextField();
            JTextField last = new JTextField();

            Object[] message = {  
                    "First Name:", first,  
                    "Last Name:", last
            };  

            int option = JOptionPane.showConfirmDialog(null, message, "Enter passenger name", JOptionPane.PLAIN_MESSAGE);

            if (option == JOptionPane.OK_OPTION)  
            {  
                // Load passenger data
                p = dataHandler.getPassengerData(first.getText(), last.getText());
                if(p != null)
                {
                    updateTextfields( p);
                    // Display passenger data
                    getContentPane().removeAll();
                    getContentPane().add(passengerDetailsPanel);
                    setSize(400,340);
                    setLocationRelativeTo(null);
                    validate();
                    repaint();
                    printAll(getGraphics());
                }
            }
        }

        if(choice == "Add New Passenger")
        {
            if(displayPassengerInputForm());
            {
                // Display passenger data
                getContentPane().removeAll();
                getContentPane().add(passengerDetailsPanel);
                setSize(400,340);
                setLocationRelativeTo(null);
                validate();
                repaint();
                printAll(getGraphics());
            }
        }

    }
}

//返回窗口 A 和窗口 B 的程序示例

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class Frame extends JFrame
{
private JPanel mainPanel = new JPanel();
private JComboBox<String> mainChoice = new JComboBox<String>();
private JButton goButton = new JButton("GO");

public Frame()
{
    createMainPanel();
    this.add(mainPanel);
}


private void createMainPanel()
{
    // Fill choice box
    mainChoice.addItem("Find Passenger");
    mainChoice.addItem("Add New Passenger");

    // Set button

    goButton.addActionListener(new mainPanelGoButtonListener());
    goButton.setPreferredSize(new Dimension(5,5));

    // Add to main panel
    mainPanel.setLayout(new GridLayout(1,2,4,4));
    mainPanel.add(mainChoice);
    mainPanel.add(goButton);

}


class mainPanelGoButtonListener implements ActionListener
{
    public void actionPerformed(ActionEvent ae) 
    {       
        if(mainChoice.getSelectedItem().equals("Find Passenger"));
        {
            // DISPLAYS WINDOW FOR INPUT
            System.out.printf(" WINDOW A ");

        }

        if(mainChoice.getSelectedItem().equals("Add New Passenger"));
        {
            // DISPLAYS WINDOW FOR INPUT
            System.out.printf(" WINDOW B ");
        }
    }
}

public static void main(String args[])
{
    Frame frame = new Frame();
    frame.setTitle("SSD Project");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLocationRelativeTo(null);
    frame.setSize(400,50);
    frame.setVisible(true);
}

}

每次我按下一个按钮,它都会打印出窗口 A 和窗口 B 而不是一个

【问题讨论】:

    标签: java button combobox listener actionlistener


    【解决方案1】:

    我看到的一个问题是您使用== 来比较字符串。

    不要使用== 比较字符串,因为这会比较两个字符串对象 是否相同,而您对测试不感兴趣。使用equals(...)equalsIgnoreCase(...) 方法来测试两个字符串是否包含相同的字符,以相同的顺序,分别有或没有相同的大小写,这才是你真正感兴趣的。

    • 下一步:确保使用if (something) { xxxx } else if (somethingElse) { xxxx } 以确保只有一个 if 块被执行。
    • 下一步:查看 CardLayout,它可以让您的 GUI 更清晰地更改视图。

    编辑
    你问:

    您的意思是:mainChoice.getSelectedItem().equals("Add New Passenger") 吗?导致我仍然得到相同的结果;

    是的,没错。但是经过进一步思考,您上面的代码不能导致两个 if 块都被触发,因为 String == 对于两个测试字符串都不能为真。是其他原因导致了您的问题。

    你能给我一些简单的例子吗?

    实际上,如果您可以创建并发布一个minimal runnable program 来为我们重现您的问题,那就更好了。

    【讨论】:

    • 你的意思是:mainChoice.getSelectedItem().equals("Add New Passenger") ??因为我仍然得到相同的结果;/你能给我一些简单的例子吗?
    • 我发现了错误,感谢您在编写我必须添加的代码时花费的时间;在导致括号中的代码执行的 if 语句的末尾 ^.^
    • @user3264021:确实。好接机。 1+
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-15
    • 2016-04-18
    • 2019-08-05
    • 1970-01-01
    • 2021-07-06
    • 1970-01-01
    相关资源
    最近更新 更多