Java - 如何创建自定义对话框?

时间:2009-04-25 18:58:53

标签: java swing jframe jdialog joptionpane

我在JFrame上有一个按钮,当点击时我想要一个弹出的对话框,其中有多个文本区域供用户输入。我一直在四处寻找如何做到这一点,但我一直在变得更加困惑。有人可以帮忙吗?

6 个答案:

答案 0 :(得分:80)

如果您不需要太多自定义行为,JOptionPane可以节省大量时间。它负责确定OK / Cancel选项的放置和本地化,并且是一种快速而又脏的方式来显示自定义对话框而无需定义自己的类。大多数情况下,JOptionPane中的“message”参数是一个String,但您也可以传入JComponent或JComponents数组。

示例:

JTextField firstName = new JTextField();
JTextField lastName = new JTextField();
JPasswordField password = new JPasswordField();
final JComponent[] inputs = new JComponent[] {
        new JLabel("First"),
        firstName,
        new JLabel("Last"),
        lastName,
        new JLabel("Password"),
        password
};
int result = JOptionPane.showConfirmDialog(null, inputs, "My custom dialog", JOptionPane.PLAIN_MESSAGE);
if (result == JOptionPane.OK_OPTION) {
    System.out.println("You entered " +
            firstName.getText() + ", " +
            lastName.getText() + ", " +
            password.getText());
} else {
    System.out.println("User canceled / closed the dialog, result = " + result);
}

答案 1 :(得分:3)

尝试这个简单的类来根据自己的喜好自定义对话框:

import java.util.ArrayList;
import java.util.List;

import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JRootPane;

public class CustomDialog
{
    private List<JComponent> components;

    private String title;
    private int messageType;
    private JRootPane rootPane;
    private String[] options;
    private int optionIndex;

    public CustomDialog()
    {
        components = new ArrayList<>();

        setTitle("Custom dialog");
        setMessageType(JOptionPane.PLAIN_MESSAGE);
        setRootPane(null);
        setOptions(new String[] { "OK", "Cancel" });
        setOptionSelection(0);
    }

    public void setTitle(String title)
    {
        this.title = title;
    }

    public void setMessageType(int messageType)
    {
        this.messageType = messageType;
    }

    public void addComponent(JComponent component)
    {
        components.add(component);
    }

    public void addMessageText(String messageText)
    {
        JLabel label = new JLabel("<html>" + messageText + "</html>");

        components.add(label);
    }

    public void setRootPane(JRootPane rootPane)
    {
        this.rootPane = rootPane;
    }

    public void setOptions(String[] options)
    {
        this.options = options;
    }

    public void setOptionSelection(int optionIndex)
    {
        this.optionIndex = optionIndex;
    }

    public int show()
    {
        int optionType = JOptionPane.OK_CANCEL_OPTION;
        Object optionSelection = null;

        if(options.length != 0)
        {
            optionSelection = options[optionIndex];
        }

        int selection = JOptionPane.showOptionDialog(rootPane,
                components.toArray(), title, optionType, messageType, null,
                options, optionSelection);

        return selection;
    }

    public static String getLineBreak()
    {
        return "<br>";
    }
}

答案 2 :(得分:1)

Java教程中的

This lesson详细解释了每个Swing组件,包括示例和API链接。

答案 3 :(得分:1)

如果您使用NetBeans IDE(此时最新版本为6.5.1),您可以使用它来创建基本的GUI Java应用程序,使用File-&gt; New Project并选择Java类别然后选择Java Desktop应用

创建后,您将拥有一个简单的裸骨GUI应用程序,其中包含一个可以使用菜单选项打开的框。您应该能够根据自己的需要进行调整,并了解如何通过单击按钮打开对话框。

您可以直观地编辑对话框。删除那里的项目并添加一些文本区域。如果你遇到困难,可以使用它来回答更多问题:)

答案 4 :(得分:1)

好吧,你基本上创建了一个JDialog,添加你的文本组件并使其可见。如果你缩小你遇到麻烦的具体位,可能会有所帮助。

答案 5 :(得分:1)

我创建了一个自定义对话框API。在这里查看https://github.com/MarkMyWord03/CustomDialog。它支持消息和确认框。输入和选项对话框就像在joptionpane中一样,很快就会实现。

来自CUstomDialog API的

示例错误对话框: CustomDialog Error Message

相关问题