简体   繁体   English

如何获得带有三个文本字段的JOptionPane

[英]How to get JOptionPane with three text fields

I want to know how i can do a messageBox from three input dialog .. 我想知道我如何从三个输入对话框中做一个messageBox ..

Like this: 像这样:

JOptionPane.showInputMessageDialog("Enter your FirstName");
JOptionPane.showInputMessageDialog("Enter your MiddleName");
JOptionPane.showInputMessageDialog("Enter your LastName");

But I want one message has a three input boxes. 但我希望一条消息有三个输入框。

Build a JPanel (supose it's named inputPanel) with the three JtextFields to input and then do this: 使用三个JtextFields构建一个JPanel(名称为inputPanel),然后执行以下操作:

if (JOptionPane.YES_OPTION == JOptionPane.showconfirmDialog(
    parentComponent, inputPanel, "Enter your data", JOptionPane.YES_NO_OPTION) {

    // retrieve data from the JTextFields and do things

} else {

    // User close the dialog, do things... or not

}

You can't do that with JOptionPane . 你不能用JOptionPane做到这一点。 Create a JDialog and add three JTextField 's to it instead. 创建一个JDialog并添加三个JTextField A JDialog will block the caller when you call setVisible(true) , so it's easy to create a dialog that waits for user input before it returns. 当调用setVisible(true)JDialog将阻塞调用者,因此很容易创建一个在返回之前等待用户输入的对话框。

showInputMessageDialog and its brethren are simple ways to whip up a simple "standard" dialog. showInputMessageDialog及其兄弟是简单的方法来创建一个简单的“标准”对话框。 For more complicated dialogs, I'm pretty sure you'll have to subclass JDialog or such. 对于更复杂的对话框,我很确定你必须JDialog等子类。

As Telcontar has suggested you can add Swing components (like a JPanel) to an option pane. 正如Telcontar建议您可以将Swing组件(如JPanel)添加到选项窗格中。 So it is easy to take advantage of the automatic creation of buttons rather than do it from scratch by building your own JDialog. 所以很容易利用按钮的自动创建,而不是通过构建自己的JDialog从头开始。

However, there is one small problem. 但是,有一个小问题。 The focus will be on the first button, not on the first component of your panel. 重点将放在第一个按钮上,而不是面板的第一个组件上。 To get around this problem you can try the solution presented in Dialog Focus . 要解决此问题,您可以尝试Dialog Focus中提供的解决方案。

You can find the standard Java Tutorial Example here: 您可以在此处找到标准的Java Tutorial示例:

Click Here to Open the example java file 单击此处打开示例java文件

The example has only one text box, but the example is clear enough for you to extend it. 该示例只有一个文本框,但示例足够清晰,可以扩展它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM