我在jframe上有一个按钮,当单击时,我希望一个对话框弹出,其中有多个文本区域用于用户输入。我一直在寻找解决方法,但我继续变得更加困惑。谁能帮忙?

有帮助吗?

解决方案

如果您不需要太多的自定义行为,那么Joptionpane是一个很好的节省时光。它需要注意确定 /取消选项的放置和本地化,这是一种显示自定义对话框的快速和折扣方式,而无需定义自己的类。在大多数情况下,Joptionpane中的“消息”参数是一个字符串,但是您也可以传递JCOMPONENT或JOMPONENT的数组。

例子:

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

其他提示

尝试此简单的课程以自定义您喜欢的对话框:

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>";
    }
}

这一课 从Java教程中,用示例和API链接详细说明了每个秋千组件。

如果您使用 Netbeans IDE (目前最新版本为6.5.1),您可以使用它使用文件 - >新项目创建基本的GUI Java应用程序,然后选择Java类别然后选择Java桌面应用程序。

创建后,您将拥有一个简单的裸骨GUI应用程序,其中包含一个可以使用菜单选择打开的大约框。您应该能够适应您的需求,并学习如何通过单击按钮打开对话框。

您将能够直观地编辑对话框。删除那里的项目并添加一些文本区域。玩它,如果您陷入困境,请回来更多问题:)

好吧,您本质上可以创建一个JDialog,添加您的文本组件并使其可见。如果您缩小您遇到的特定位置,这可能会有所帮助。

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

来自CustomDialog API的示例错误对话框:定制错误消息

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top