本文介绍了JOptionPane.showInputDialog中的多个输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有办法在 JOptionPane.showInputDialog
中创建多个输入而不只是一个输入?
Is there a way to create multiple input in JOptionPane.showInputDialog
instead of just one input?
推荐答案
是的。您知道可以将任何 Object
放入大多数 JOptionPane.showXXX方法的
,通常 Object
参数中对象
恰好是 JPanel
。
Yes. You know that you can put any Object
into the Object
parameter of most JOptionPane.showXXX methods
, and often that Object
happens to be a JPanel
.
在你的情况下,也许你可以使用 JPanel
,它有几个 JTextFields
in它:
In your situation, perhaps you could use a JPanel
that has several JTextFields
in it:
import javax.swing.*;
public class JOptionPaneMultiInput {
public static void main(String[] args) {
JTextField xField = new JTextField(5);
JTextField yField = new JTextField(5);
JPanel myPanel = new JPanel();
myPanel.add(new JLabel("x:"));
myPanel.add(xField);
myPanel.add(Box.createHorizontalStrut(15)); // a spacer
myPanel.add(new JLabel("y:"));
myPanel.add(yField);
int result = JOptionPane.showConfirmDialog(null, myPanel,
"Please Enter X and Y Values", JOptionPane.OK_CANCEL_OPTION);
if (result == JOptionPane.OK_OPTION) {
System.out.println("x value: " + xField.getText());
System.out.println("y value: " + yField.getText());
}
}
}
这篇关于JOptionPane.showInputDialog中的多个输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!