使用 JOptionPane 按钮创建条件状态

Creating Conditional States with JOptionPane Buttons

有没有办法可以为 JOptionPane.showInputDialog() 中的确定和取消按钮创建单独的条件语句?
我想这样做的原因是因为我不想将一个空字符串或一个充满空格的字符串设置为 s.
当我单击带有空文本字段的确定或单击取消时,s 将设置为空。
有没有一种方法可以让我为“确定”和“取消”按钮选择不同的选项,如果我单击“取消”,window 将关闭,如果我单击“确定”,代码将继续执行条件语句?

public boolean stringIsNullOrOnlyWhiteSpaces(String s)  {
   //checks if string is null or if string only contains white spaces
}


private void nameOfString() {
String s = JOptionPane.showInputDialog(this, "Enter String Name");

if (!stringIsNullOrOnlyWhiteSpaces(s)) {
   // do action    
  }

else { // if "ok" were to be clicked and not "cancel"
JOptionPane.showMessageDialog(this, "String cannot be empty");
nameOfString();
}
}

我怀疑是否有一种方法可以只使用 JOptionPane,它们应该只是捕获一个值,在 .showInputMessage() 的情况下,它是一个字符串。

正确的方法是使用 JDialog 并添加组件及其内部逻辑。

我对工作真的很无聊所以我做了一些我认为对你有用的事情:

public class CustomInput {

private JButton btAccept;
private JButton btCancel;
private JLabel lbInput;
private JTextField tfInput;
private JPanel pnDialog;

private JPanel createPanel() {
    java.awt.GridBagConstraints gridBagConstraints;
    pnDialog = new JPanel();
    lbInput = new JLabel();
    tfInput = new JTextField();
    btAccept = new JButton();
    btCancel = new JButton();

    pnDialog.setLayout(new java.awt.GridBagLayout());

    btAccept.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            btAcceptAction(evt);
        }
    });

    btCancel.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            btCancelAction(evt);
        }
    });

    lbInput.setText("Enter your input.");
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 1;
    gridBagConstraints.gridy = 0;
    gridBagConstraints.gridwidth = 2;
    pnDialog.add(lbInput, gridBagConstraints);

    tfInput.setText("");
    tfInput.setColumns(10);
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 1;
    gridBagConstraints.gridy = 1;
    gridBagConstraints.gridwidth = 2;
    pnDialog.add(tfInput, gridBagConstraints);

    btAccept.setText("Accept");
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 1;
    gridBagConstraints.gridy = 2;
    pnDialog.add(btAccept, gridBagConstraints);

    btCancel.setText("Cancel");
    gridBagConstraints = new java.awt.GridBagConstraints();
    gridBagConstraints.gridx = 2;
    gridBagConstraints.gridy = 2;
    pnDialog.add(btCancel, gridBagConstraints);

    return pnDialog;
    //Mommy someone is reading my code
}

private void btAcceptAction(java.awt.event.ActionEvent evt) {
    if (tfInput.getText().isEmpty()) {
        JOptionPane.showMessageDialog(pnDialog, "Input cannot be null", "Error", JOptionPane.ERROR_MESSAGE);
    }
}

private void btCancelAction(java.awt.event.ActionEvent evt) {
    JDialog topFrame = (JDialog) SwingUtilities.getWindowAncestor(pnDialog);
    topFrame.dispose();
}

public static void main(String[] args) {
    JDialog dialog = new JDialog();
    CustomInput tp = new CustomInput();
    dialog.add(tp.createPanel());
    dialog.setSize(new Dimension(200,200));
    dialog.setVisible(true);
}

}