至少有两个字段的简单弹出式 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 15:28:52  来源:igfitidea点击:

Simple popup java form with at least two fields

javaswinguser-interface

提问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.showInputDialogis not a possibility.

当用户单击按钮时,我想显示一个弹出窗体,该​​窗体应该至少有两个 JTextField 和两个 JLabel,因此JOptionPane.showInputDialog不可能使用。

采纳答案by trashgod

You should at least consider one of the JOptionPanemethods such as showInputDialog()or showMessageDialog().

您至少应该考虑其中一种JOptionPane方法,例如showInputDialog()showMessageDialog()

Addendum: The choice to use JOptionPanehinges 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 中讨论的方法将焦点设置为特定组件。

image

图片

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();

enter image description here

enter image description here