【问题标题】:How to add to Dialog Boxes in Java (Swing)? [duplicate]如何在 Java (Swing) 中添加到对话框? [复制]
【发布时间】:2011-05-06 01:20:59
【问题描述】:

可能重复:
Simple popup java form with at least two fields

我正在尝试制作一个具有多个按钮、输入文本的位置等的对话框。我正在使用本教程 - http://download.oracle.com/javase/tutorial/uiswing/components/dialog.html,但它没有关于拥有多个项目的任何内容。有没有办法做到这一点?

【问题讨论】:

  • 该教程的第一页显示了一个带有单选按钮、选项卡和按钮的对话框。那怎么不是“有多个项目”?
  • 在您按下“显示”按钮之前,该对话框不会显示。唯一显示的对话框是前面显示的更简单的对话框。
  • 这里的one 包含更多项目,但您必须运行它才能看到它们。 :-)

标签: java swing user-interface dialog


【解决方案1】:

许多 showXXXXDialog 方法将 Object 作为参数之一。如果该对象是一个字符串,它将显示一个字符串。如果该对象是一个包含按钮、输入字段和其他小部件的容器,它将显示。

引用http://download.oracle.com/javase/6/docs/api/javax/swing/JOptionPane.html

Parameters:
The parameters to these methods follow consistent patterns:


parentComponent

定义要成为此对话框的父级的组件。它 以两种方式使用: 包含它被用作框架 对话框的父级,及其 屏幕坐标用于 对话框的位置。在 一般,对话框放在刚刚 组件下方。这个参数 可能为 null,在这种情况下为默认值 Frame 用作父级,而 对话框将在屏幕上居中 (取决于 L&F)。

message

要放置在对话框中的描述性消息。在最常见的 用法,消息只是一个字符串或 字符串常量。然而,类型 这个参数实际上是Object。它的 解释取决于它的类型:

对象[] 对象数组被解释为一系列消息(每个 对象)排列成垂直堆栈。 解释是递归的—— 数组中的每个对象是 根据其类型进行解释。

组件 组件显示在对话框中。

图标 Icon 包装在 JLabel 中并显示在对话框中。

其他 该对象通过调用其 toString 方法转换为字符串。结果被包装在一个 JLabel 中并显示出来。

【讨论】:

    【解决方案2】:

    这里有两个选择。 要么使用 JOptionPane 方法显示Paul Tomblin 详述的对话框,要么构建您自己的对话框。

    如果您需要对对话框进行细粒度控制,则第二个选项是必需的,例如如果您需要不同的名称(这也可以通过使用 JOptionPane.showOptionDialog 来完成)或对话框上按钮的位置,或者如果您需要一个非模态对话框。

    简单示例:

    import java.awt.Color;
    import javax.swing.JDialog;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    import javax.swing.SwingUtilities;
    
    public class DialogsTest
    {   
        public static void main(String[] args)
        {
            SwingUtilities.invokeLater(new Runnable()
            {
                @Override
                public void run()
                {               
                    JPanel p = new JPanel();
                    JPanel contentPane = new JPanel();
                    contentPane.add(p);
                    JFrame f = new JFrame();
                    f.setContentPane(contentPane);
                    f.setSize(400, 300);
                    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    f.setVisible(true);         
                    /* 
                     * NOTE: It is not recomended to share the same instance of a component 
                     * by different parents. Thought it is fine here since the first 
                     * dialog will release it before the second will get it.
                     * But in a situation when we wouldn't make the 'dialog' modal and 
                     * we would show it after the 'option pane dialog' it would be empty.
                     */
                    JPanel message = new JPanel();
                    message.add(new JLabel("Label:"));              
                    message.add(new JTextField("ABCD"));
                    message.setBackground(Color.GREEN);
                    JOptionPane.showConfirmDialog(f, message, "Default made dialog", JOptionPane.YES_NO_OPTION);
    
                    Object[] options = new String[]{"a", "b", "c"};
                    JOptionPane.showOptionDialog(f, message, "", JOptionPane.YES_OPTION, JOptionPane.INFORMATION_MESSAGE, 
                            null, options, options[0]);
                    JDialog dialog = new JDialog(f, "Custom made dialog");
                    dialog.setModal(true);
                    dialog.setContentPane(message);
                    dialog.pack();
                    dialog.setLocationRelativeTo(f);
                    dialog.setVisible(true);
                }
            });
        }
    }
    

    顺便说一句,您阅读的 Java 教程中有一个非常好的(可能太丰富了)示例。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-30
      • 2011-11-15
      相关资源
      最近更新 更多