有没有办法可以为 JOptionPane.showInputDialog()
中的“确定”和“取消”按钮创建单独的条件语句?
我想这样做的原因是因为我不希望将空字符串或充满空格的字符串设置为 s
。
当我在空文本字段中单击“确定”或单击“取消”时,s
将设置为 null。
有没有一种方法可以让我为“确定”和“取消”按钮选择不同的选项,如果我单击“取消”,窗口将关闭,如果我单击“确定”,代码将继续执行条件语句?
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);
}
}
关于java - 使用 JOptionPane 按钮创建条件状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31147809/