Java - How to create a custom dialog box?

后端 未结 6 2032
慢半拍i
慢半拍i 2020-11-30 04:18

I have a button on a JFrame that when clicked I want a dialog box to popup with multiple text areas for user input. I have been looking all around to try to figure out how

相关标签:
6条回答
  • 2020-11-30 05:01

    Try this simple class for customizing a dialog to your liking:

    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>";
        }
    }
    
    0 讨论(0)
  • 2020-11-30 05:13

    Well, you essentially create a JDialog, add your text components and make it visible. It might help if you narrow down which specific bit you're having trouble with.

    0 讨论(0)
  • 2020-11-30 05:14

    If you don't need much in the way of custom behavior, JOptionPane is a good time saver. It takes care of the placement and localization of OK / Cancel options, and is a quick-and-dirty way to show a custom dialog without needing to define your own classes. Most of the time the "message" parameter in JOptionPane is a String, but you can pass in a JComponent or array of JComponents as well.

    Example:

    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);
    }
    
    0 讨论(0)
  • 2020-11-30 05:15

    If you use the NetBeans IDE (latest version at this time is 6.5.1), you can use it to create a basic GUI java application using File->New Project and choose the Java category then Java Desktop Application.

    Once created, you will have a simple bare bones GUI app which contains an about box that can be opened using a menu selection. You should be able to adapt this to your needs and learn how to open a dialog from a button click.

    You will be able to edit the dialog visually. Delete the items that are there and add some text areas. Play around with it and come back with more questions if you get stuck :)

    0 讨论(0)
  • 2020-11-30 05:16

    This lesson from the Java tutorial explains each Swing component in detail, with examples and API links.

    0 讨论(0)
  • 2020-11-30 05:17

    i created a custom dialog API. check it out here https://github.com/MarkMyWord03/CustomDialog. It supports message and confirmation box. input and option dialog just like in joptionpane will be implemented soon.

    Sample Error Dialog from CUstomDialog API: CustomDialog Error Message

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