至少有两个字段的简单弹出式 java 表单
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3002787/
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
Simple popup java form with at least two fields
提问by nunos
When the user clicks a button, I want to show a popup form that should have at least two JTextFields and two JLabels, so using JOptionPane.showInputDialog
is not a possibility.
当用户单击按钮时,我想显示一个弹出窗体,该窗体应该至少有两个 JTextField 和两个 JLabel,因此JOptionPane.showInputDialog
不可能使用。
采纳答案by trashgod
You should at least consider one of the JOptionPane
methods such as showInputDialog()
or showMessageDialog()
.
您至少应该考虑其中一种JOptionPane
方法,例如showInputDialog()
或showMessageDialog()
。
Addendum: The choice to use JOptionPane
hinges more on the suitability of modality, rather than on the number of components shown. See also How to Make Dialogs.
附录:选择使用JOptionPane
更多取决于模式的适用性,而不是显示的组件数量。另请参阅如何制作对话框。
Addendum: As noted in a comment by @camickr, you can set the focus to a particular component using the approach discussed in Dialog Focus, cited here.
附录:如@camickr 的评论中所述,您可以使用此处引用的Dialog Focus 中讨论的方法将焦点设置为特定组件。
package gui;
import java.awt.EventQueue;
import java.awt.GridLayout;
import javax.swing.*;
/** @see https://stackoverflow.com/a/3002830/230513 */
class JOptionPaneTest {
private static void display() {
String[] items = {"One", "Two", "Three", "Four", "Five"};
JComboBox<String> combo = new JComboBox<>(items);
JTextField field1 = new JTextField("1234.56");
JTextField field2 = new JTextField("9876.54");
JPanel panel = new JPanel(new GridLayout(0, 1));
panel.add(combo);
panel.add(new JLabel("Field 1:"));
panel.add(field1);
panel.add(new JLabel("Field 2:"));
panel.add(field2);
int result = JOptionPane.showConfirmDialog(null, panel, "Test",
JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
if (result == JOptionPane.OK_OPTION) {
System.out.println(combo.getSelectedItem()
+ " " + field1.getText()
+ " " + field2.getText());
} else {
System.out.println("Cancelled");
}
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
display();
}
});
}
}
回答by milchreis
With the small UiBoosterlibrary you can build a form dialog in a few lines of code.
使用小型UiBooster库,您可以用几行代码构建一个表单对话框。
FilledForm form = new UiBooster()
.createForm("Personal informations")
.addText("Whats your first name?")
.addTextArea("Tell me something about you")
.addSelection(
"Whats your favorite movie?",
Arrays.asList("Pulp Fiction", "Bambi", "The Godfather", "Hangover"))
.show();