JOptionPane 动作侦听器在退出时发布

JOptionPane action listener posting on exit

我创建了一个应用于框架的名为 addSupplier 的按钮,然后我创建了一个动作侦听器,因此一旦按下 addSupplier 按钮,它就会创建一个 JOptionPane,其中有一个带有 JTextFields 的面板。

        addSupplier.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent ae) {
            JOptionPane.showMessageDialog(null,addSupplierPanel,"Add new supplier", JOptionPane.PLAIN_MESSAGE);
            suppNameInsert = suppNameIn.getText();
            System.out.println(suppNameInsert);
        }
    });

此 JOptionPane 的目的是将信息添加到 JTextFields,然后进行处理并发送到 MySQL 数据库,但是,因为如果我按 Okay 或 'X' out 按钮它将打印 JTextField 中的任何内容。

我只希望在我按下 'OKAY' 时发生这种情况,但我假设我将不得不以单独的方式生成 JOptionPane?

Changing the showXXX(...) changes the input and content of the pane but doesn't stop the exit out button from being seen as a button press?

您需要检查从 showXXX(….) 方法返回的 int 参数。此值将告诉您单击了哪个按钮。

类似于:

int result = JOptionPane.showConfirmDialog(…);

if(result == JOptionPane.YES_OPTION)
{
    // do your processing here
}