如何在Java(Swing)中添加对话框?

时间:2011-05-06 01:21:00

标签: java swing user-interface dialog

  

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

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

2 个答案:

答案 0 :(得分:2)

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

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

Parameters:
The parameters to these methods follow consistent patterns:


parentComponent
     

定义要作为此对话框的父级的Component。它   有两种使用方式:Frame   包含它用作框架   对话框的父级,以及它的   屏幕坐标用于   放置对话框。在   一般来说,对话框只是放置   在组件下方。这个参数   可以为null,在这种情况下是默认值   帧用作父级,而   对话框将以屏幕为中心   (取决于L& F)。

message
     

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

     

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

     

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

     

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

     

他人   通过调用其toString方法将对象转换为String。结果将包装在JLabel中并显示。

答案 1 :(得分:1)

这里有两种选择。 要么使用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教程中有一个非常好的(可能太丰富)的例子。