如何删除 JOptionPane.showInputDialog 中的默认文本字段?
How do I get rid of the default text field in JOptionPane.showInputDialog?
我正在创建一个简单的选项窗格,要求多个用户输入。我已经指定了标签和文本字段,但是在我的选项窗格的末尾,有一个不属于任何变量的文本字段,所以我猜它是在指定选项窗格时出现的。
这是我的代码:
JTextField locationField = new JTextField(10);
JTextField usedByField = new JTextField(5);
JTextField commentField = new JTextField(50);
...
myPanel.add(new JLabel("Location: ");
myPanel.add(locationField);
myPanel.add(new JLabel("Used By: ");
myPanel.add(usedByField);
myPanel.add(new JLabel("Comments: ");
myPanel.add(commentField);
...
JOptionPane.showInputDialog(myPanel);
我的对话框最终看起来像这样,正如您所看到的,我的窗格底部有一个杂散的文本字段:
我的问题是,我会在我的代码中的什么地方秘密指定它?我不认为我是,那么我该怎么做才能摆脱这个我不需要的杂散文本字段。
谢谢。
解决方法很简单:不要使用 JOptionPane.showInputDialog(...)
。而是使用 JOptionPane.showMessageDialog(...)
.
showInputDialog 的构建是为了从用户那里获取单个字符串输入,因此它的结构是为了显示一个嵌入的 JTextField,并且 returns 输入该字段的字符串,一个您没有使用。
另一方面,showMessageDialog 不会执行此操作,而是 returns 一个整数,具体取决于按下了哪个按钮。
请查看 JOptionPane API 了解更多信息。
编辑:我错了。如果您希望对话框提供对话框处理按钮,例如 "OK"、"Cancel" 或 "Yes" 和 "No" 并允许用户使用 JOptionPane.showConfirmDialog(...)
按下这些按钮,然后从按钮获取输入。
例如:
final JTextField userNameField = new JTextField(10);
final JPasswordField passwordField = new JPasswordField(10);
JPanel pane = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0,
GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
new Insets(2, 2, 2, 2), 0, 0);
pane.add(new JLabel("User Name:"), gbc);
gbc.gridy = 1;
pane.add(new JLabel("Password:"), gbc);
gbc.gridx = 1;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.EAST;
pane.add(userNameField, gbc);
gbc.gridy = 1;
pane.add(passwordField, gbc);
int reply = JOptionPane.showConfirmDialog(null, pane, "Please Log-In",
JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
if (reply == JOptionPane.OK_OPTION) {
// get user input
String userName = userNameField.getText();
// ****** WARNING ******
// ** The line below is unsafe code and makes a password potentially discoverable
String password = new String(passwordField.getPassword());
System.out.println("user name: " + userName);
System.out.println("passowrd: " + password);
}
其中显示:
我正在创建一个简单的选项窗格,要求多个用户输入。我已经指定了标签和文本字段,但是在我的选项窗格的末尾,有一个不属于任何变量的文本字段,所以我猜它是在指定选项窗格时出现的。
这是我的代码:
JTextField locationField = new JTextField(10);
JTextField usedByField = new JTextField(5);
JTextField commentField = new JTextField(50);
...
myPanel.add(new JLabel("Location: ");
myPanel.add(locationField);
myPanel.add(new JLabel("Used By: ");
myPanel.add(usedByField);
myPanel.add(new JLabel("Comments: ");
myPanel.add(commentField);
...
JOptionPane.showInputDialog(myPanel);
我的对话框最终看起来像这样,正如您所看到的,我的窗格底部有一个杂散的文本字段:
我的问题是,我会在我的代码中的什么地方秘密指定它?我不认为我是,那么我该怎么做才能摆脱这个我不需要的杂散文本字段。
谢谢。
解决方法很简单:不要使用 JOptionPane.showInputDialog(...)
。而是使用 JOptionPane.showMessageDialog(...)
.
showInputDialog 的构建是为了从用户那里获取单个字符串输入,因此它的结构是为了显示一个嵌入的 JTextField,并且 returns 输入该字段的字符串,一个您没有使用。
另一方面,showMessageDialog 不会执行此操作,而是 returns 一个整数,具体取决于按下了哪个按钮。
请查看 JOptionPane API 了解更多信息。
编辑:我错了。如果您希望对话框提供对话框处理按钮,例如 "OK"、"Cancel" 或 "Yes" 和 "No" 并允许用户使用 JOptionPane.showConfirmDialog(...)
按下这些按钮,然后从按钮获取输入。
例如:
final JTextField userNameField = new JTextField(10);
final JPasswordField passwordField = new JPasswordField(10);
JPanel pane = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0,
GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
new Insets(2, 2, 2, 2), 0, 0);
pane.add(new JLabel("User Name:"), gbc);
gbc.gridy = 1;
pane.add(new JLabel("Password:"), gbc);
gbc.gridx = 1;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.EAST;
pane.add(userNameField, gbc);
gbc.gridy = 1;
pane.add(passwordField, gbc);
int reply = JOptionPane.showConfirmDialog(null, pane, "Please Log-In",
JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
if (reply == JOptionPane.OK_OPTION) {
// get user input
String userName = userNameField.getText();
// ****** WARNING ******
// ** The line below is unsafe code and makes a password potentially discoverable
String password = new String(passwordField.getPassword());
System.out.println("user name: " + userName);
System.out.println("passowrd: " + password);
}
其中显示: