Java Dialog - Find out if OK is clicked?

前端 未结 4 1657
一向
一向 2020-12-01 22:48

I have a dialog for a client-GUI that asks for the IP and Port of the server one wants to connect to. I have everything else, but how would I make it so that when the user c

相关标签:
4条回答
  • 2020-12-01 23:08

    I'd suggest creating a JPanel and using JOptionpane.showConfirmDialog() (or even JOptionPane.showOptionDialog() to display the dialog and retrieve the option. Here's a modification of your code as an example:

    import java.awt.*;
    import javax.swing.*;
    
    class ClientDialog {
        private JTextField ip = new JTextField(20);
        private JTextField port = new JTextField(20);
        private JOptionPane optionPane;
        private JPanel panel;
    
        public void CreateDialog(){
    
            panel = new JPanel(new GridLayout(2, 2));
    
            panel.add(new JLabel("IP"));
            panel.add(ip);
            panel.add(new JLabel("Port"));
            panel.add(port);
    
            int option = JOptionPane.showConfirmDialog(null, panel, "Client Dialog", JOptionPane.DEFAULT_OPTION);
    
            if (option == JOptionPane.OK_OPTION) {
                System.out.println("OK!"); // do something
            }
        }
    
    }
    
    public class Test {
    
       public static void main(String args[])
       {
          ClientDialog c = new ClientDialog();
          c.CreateDialog();
       }
    
    }
    

    Damn, took too long to post. Anyway just design the layout of the JPanel as you would for any other sort of frame.

    0 讨论(0)
  • 2020-12-01 23:12

    Please understand that the 2nd parameter in a JOptionPane, the object parameter, can be any Swing component including a JPanel that holds a simple or complex GUI.

    Consider creating a JPanel, placing a few components in it including JLabels and two JTextFields, one for IP, one for port, and then displaying this JPanel in a JOptionPane. Then you can easily check if OK has been pressed and act accordingly.

    import javax.swing.*;
    
    public class OptionEg {
       public static void main(String[] args) {
          final JTextField ipField = new JTextField(10);
          final JTextField portField = new JTextField(10);
          JPanel panel = new JPanel();
          panel.add(new JLabel("IP:"));
          panel.add(ipField);
    
          panel.add(Box.createHorizontalStrut(15));
          panel.add(new JLabel("Port:"));
          panel.add(portField);
    
          int result = JOptionPane.showConfirmDialog(null, panel,
                "Enter Information", JOptionPane.OK_CANCEL_OPTION);
    
          if (result == JOptionPane.OK_OPTION) {
             System.out.println("IP: " + ipField.getText());
             System.out.println("Port: " + portField.getText());
          }
    
       }
    }
    
    0 讨论(0)
  • 2020-12-01 23:16

    I'd suggest to use showConfirmDialog instead

    int result = JOptionPane.showConfirmDialog(myParent, "Narrative", 
           "Title", JOptionPane.INFORMATION_MESSAGE);
    

    and there you can test for various returns value from JDialog/JOptionPane

    if (result == JOptionPane.OK_OPTION, 
                  JOptionPane.CANCEL_OPTION, 
                  JOptionPane.CLOSED_OPTION, etc..
    
    0 讨论(0)
  • 2020-12-01 23:16

    Good solutions. If you want not to use something else, this is a working example: 1) set DO_NOTHING_ON_CLOSE to ensure user MUST press OK 2) verify if the JDialog is still visible.

    dialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
    dialog.setModal(false);
    dialog.setVisible(true);
    dialog.setAlwaysOnTop(true);
    while(dialog.isVisible())try{Thread.sleep(50);}
    catch(InterruptedException e){}
    

    This can be a solution if you want to implement a non-modal dialog box.

    0 讨论(0)
提交回复
热议问题