Java 是否可以在 JOptionPane.showInputDialog 中放置多个输入?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21290288/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it possible to put multiple input in JOptionPane.showInputDialog?
提问by
I was wondering if it is possible to put multiple inputs in JOptionPane.showInputDialog and then get the user input and if the user has given a wrong input for one of the questions then provide them with a error, asking them to re-enter that specific data again.
我想知道是否可以在 JOptionPane.showInputDialog 中放置多个输入,然后获取用户输入,如果用户对其中一个问题提供了错误的输入,则向他们提供错误,要求他们重新输入特定的再次数据。
For example, in the input I want questions like;
例如,在输入中我想要这样的问题;
- How many times have to been out? between 1-10.
- Do you like number 1 or 2 or 3?
- Please state for how many hours your have between 1-10 you have stop in a restaurant?
- and I will need to add some more at a later stage.
- 必须出去多少次?1-10 之间。
- 你喜欢数字 1 还是 2 或 3?
- 请说明您在 1-10 点之间有多少小时在餐厅停留?
- 我需要在稍后阶段添加更多内容。
So instead of have a JOptionPane.showInputDialog for each question like this:
因此,对于这样的每个问题,不要有一个 JOptionPane.showInputDialog :
int timeout;
do {
String timeoutinputbyuser = JOptionPane.showInputDialog("How many times have to been out? between 1-10.");
timeout = Integer.parseInt(timeoutinputbyuser);
} while (timeout < 1 || timeout > 10);
I want to have all the questions in one and provide a suitable error if the user gets any question wrong.
我想将所有问题合二为一,如果用户有任何问题,我想提供一个合适的错误。
回答by Andrew Thompson
No, the input dialog only accepts a single input area.
不,输入对话框只接受一个输入区域。
Put the components in a JPanel
and display it in a JOptionPane.showMessageDialog(..)
. Note that you can then have better components:
将组件放在 a 中JPanel
并在 a 中显示JOptionPane.showMessageDialog(..)
。请注意,您可以拥有更好的组件:
- A
JSpinner
for selecting a number. JRadioButton
objects in aButtonGroup
for the choice of 3..
- A
JSpinner
用于选择一个数字。 JRadioButton
对象在 aButtonGroup
中选择 3..
回答by Rush2sk8
Try this
尝试这个
JTextField field1 = new JTextField();
JTextField field2 = new JTextField();
JTextField field3 = new JTextField();
JTextField field4 = new JTextField();
JTextField field5 = new JTextField();
Object[] message = {
"Input value 1:", field1,
"Input value 2:", field2,
"Input value 3:", field3,
"Input value 4:", field4,
"Input value 5:", field5,
};
int option = JOptionPane.showConfirmDialog(parent, message, "Enter all your values", JOptionPane.OK_CANCEL_OPTION);
if (option == JOptionPane.OK_OPTION)
{
String value1 = field1.getText();
String value2 = field2.getText();
String value3 = field3.getText();
String value4 = field4.getText();
String value5 = field5.getText();
}