简单的弹出式Java表单,至少包含两个字段 [英] Simple popup java form with at least two fields

查看:203
本文介绍了简单的弹出式Java表单,至少包含两个字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户点击一个按钮时,我想显示一个应该至少有两个JTextFields和两个JLabel的弹出窗体,所以使用 JOptionPane.showInputDialog 是不可能的。

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.

推荐答案

你至少应该考虑一个 JOptionPane 方法,例如 showInputDialog( ) showMessageDialog()

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

附录:使用 JOptionPane 更多地取决于模态的适用性,而不是所示组件的数量。另请参见 如何制作对话框

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.

附录:如@camickr的评论中所述,您可以使用 Dialog Focus ,引用这里

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.

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

这篇关于简单的弹出式Java表单,至少包含两个字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆