简体   繁体   English

使用按钮上的自定义操作创建JOptionPane

[英]Creating a JOptionPane with custom actions on buttons

I'm trying to create a custom dialog with input from the user, after each step(of the input) the information is verified, and if it's correct a new panel appears, and the user can continue adding info. 我正在尝试使用来自用户的输入创建自定义对话框,在(输入的)每个步骤之后,都要验证信息,如果正确,则会出现一个新面板,并且用户可以继续添加信息。 To do this i'm using JOptionPane. 为此,我正在使用JOptionPane。 I'll have three buttons "Ok", enabled only if the user went trough the whole input proces, "Next", to check if the inputted info is correct and show a new pane, and lastly "Cancel", to cacel. 我将拥有三个按钮“确定”,仅当用户经过整个输入过程时才启用“下一步”,以检查输入的信息是否正确并显示新窗格,最后显示“取消”以取消。 Here how it looks: 这里看起来如何:

在此处输入图片说明

Right now if i click on any of the buttons, the dialog get closed, how should i change that? 现在,如果我单击任何按钮,对话框将关闭,我该如何更改?

My Code so far(most of it): 到目前为止,我的代码(大部分):

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

    this.title = "Make Booking";
    setMessageType(JOptionPane.PLAIN_MESSAGE);
    setRootPane(null);
    setOptions(new String[] { "OK", "Next", "Cancel" });
    setOptionSelection(0);

    JPanel roomNumPanel = new JPanel();
    lblRoomNum = new JLabel( "Enter the booking number" );
    roomNumPanel.add(lblRoomNum);
    txtFldRoomNum = new JTextField(20);
    roomNumPanel.add(txtFldRoomNum);
    this.addComponent(roomNumPanel);

}

public void show() //the function i use to show the form
{
    int optionType = JOptionPane.OK_CANCEL_OPTION;
    //JOptionPane.OK_OPTION
    Object optionSelection = null;

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

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

    System.out.println(selection);

    //return selection;
}

JOptionPane has its own concept of what the buttons should do, which you are setting via the OK_CANCEL_OPTION . JOptionPane有自己的按钮应做什么概念,您可以通过OK_CANCEL_OPTION进行设置。 All of the options result in the popup window closing. 所有选项都会导致弹出窗口关闭。 Technically, you could get the list of components dynamically, find the buttons that JOptionPane creates, remove the default handler, and then add your own... but that seems like a convoluted way of doing it (compared to not extending JOptionPane ). 从技术上讲,您可以动态获取组件列表,找到JOptionPane创建的按钮,删除默认处理程序,然后添加自己的按钮……但这似乎是一种复杂的工作方式(与不扩展JOptionPane相比)。

If you're set on using JOptionPane, I would recommend creating your own JButtons and adding them to the JPanel that you create. 如果您打算使用JOptionPane,我建议您创建自己的JButton,并将其添加到您创建的JPanel中。 That will allow you to set callback behavior as you wish. 这将允许您根据需要设置回调行为。 Then tell JOptionPane not show any buttons. 然后告诉JOptionPane不要显示任何按钮。 This can be done by setting the constructor's option argument to: new Object[]{} or calling: setOptions(new Object[]{}) ; 这可以通过将构造函数的option参数设置为: new Object[]{}或调用: setOptions(new Object[]{})

Because there will be no default buttons showing, JOptionPane will automatically pack() itself, leaving only your pane visible, without the default buttons that have undesirable behaviour. 由于没有默认按钮显示,因此JOptionPane会自动pack()本身,仅使您的窗格可见,而不会出现具有不良行为的默认按钮。

Because after showing your dialog you need to check int selection to know which one did user choose? 因为在显示您的对话框后,您需要检查int selection以了解用户选择了哪一个?

I'm trying to give some simple example to you : 我正在尝试给您一个简单的例子:

private int selection;
private final String password = "12345";

public TestClass() throws MalformedURLException {

    Object[] options = {"Enter", "Quit", "Cancel"};
    URL url = new URL("http://education.oracle.com/education/images/wdpsub/java.png");
    ImageIcon icon = new ImageIcon(url);

    JPanel panel = new JPanel();
    panel.add(new JLabel("Welcome to my\nJOptionPane example"));

    selection = JOptionPane.showOptionDialog(null, panel, "Show me yout skills", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE, icon, options, null);
    System.out.println(selection);//print to test which button returns wich value

    switch (selection) {
    case 2:
        JOptionPane.showMessageDialog(null, "Your job is cancelled");
        break;
    case 1:
        String answer = JOptionPane.showInputDialog("Are you sure?");
        if(answer.equalsIgnoreCase("YES")) {
            System.exit(0);
        }else {
            JOptionPane.showOptionDialog(null, panel, "Show me yout skills", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE, icon, options, null);
        }

        break;  
    case 0:
        String keylog = JOptionPane.showInputDialog("Enter your password : ");
        if(keylog.equals(password)) {
            JOptionPane.showMessageDialog(null, "Hello user.");
        }
        break;  
    }
}

public static void main(String[] args) throws MalformedURLException {
    new TestClass();
}

Note : This code written just to explain the logic of this work. 注意: 编写此代码只是为了解释这项工作的逻辑。

I hope to this help you. 希望对您有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM