Creating Java dialogs

后端 未结 5 1790
梦如初夏
梦如初夏 2020-12-18 11:53

What would be the easiest way for creating a dialog:

  • in one window I\'m giving data for envelope addressing, also set font type from list of sizes
  • w
相关标签:
5条回答
  • 2020-12-18 12:29

    If it is allowed to use a GUI builder I would recommend you IntelliJ IDEA's

    You can create something like that in about 5 - 10 mins.

    If that's not possible ( maybe you want to practice-learn-or-something-else ) I would use a JFrame instead ) with CardLayout

    Shouldn't be that hard to do.

    0 讨论(0)
  • 2020-12-18 12:31

    You can use JOptionPane. You can add any Swing component to it.

    Create a panel with all the components you need except for the buttons and then add the panel to the option pane. The only problem with this approach is that focus will be on the buttons by default. To solve this problem see the solution presented by Dialog Focus.

    0 讨论(0)
  • 2020-12-18 12:33

    You will need to use JDialog. No point messing about with JOptoinPane - it's not meant for gathering more than a simple string. Additionally, use either MigLayout, TableLayout, or JGoodies forms - it will help you get a nice layout that's easy to code.

    0 讨论(0)
  • 2020-12-18 12:38

    This should get you going.

    class TestDialog extends JDialog {
    
        private JButton okButton = new JButton(new AbstractAction("ok") {
            public void actionPerformed(ActionEvent e) {
                System.err.println("User clicked ok");
                // SHOW THE PREVIEW...
                setVisible(false);
            }
        });
        private JButton cancelButton = new JButton(new AbstractAction("cancel") {
            public void actionPerformed(ActionEvent e) {
                System.err.println("User clicked cancel");
                setVisible(false);
            }
        });
    
        private JTextField nameField = new JTextField(20);
        private JTextField surnameField = new JTextField();
        private JTextField addr1Field = new JTextField();
        private JTextField addr2Field = new JTextField();
        private JComboBox sizes = new JComboBox(new String[] { "small", "large" });
    
        public TestDialog(JFrame frame, boolean modal, String myMessage) {
            super(frame, "Envelope addressing", modal);
    
            JPanel mainPanel = new JPanel();
            mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
            getContentPane().add(mainPanel);
    
            JPanel addrPanel = new JPanel(new GridLayout(0, 1));
            addrPanel.setBorder(BorderFactory.createTitledBorder("Receiver"));
            addrPanel.add(new JLabel("Name"));
            addrPanel.add(nameField);
            addrPanel.add(new JLabel("Surname"));
            addrPanel.add(surnameField);
            addrPanel.add(new JLabel("Address 1"));
            addrPanel.add(addr1Field);
            addrPanel.add(new JLabel("Address 2"));
            addrPanel.add(addr2Field);
            mainPanel.add(addrPanel);
    
            mainPanel.add(new JLabel(" "));
    
            mainPanel.add(sizes);
            JPanel buttons = new JPanel(new FlowLayout());
            buttons.add(okButton);
            buttons.add(cancelButton);
    
            mainPanel.add(buttons);
    
            pack();
            setLocationRelativeTo(frame);
            setVisible(true);
        }
    
    
        public String getAddr1() {
            return addr1Field.getText();
        }
    
        // ...
    }
    

    Result:

    enter image description here

    0 讨论(0)
  • 2020-12-18 12:39

    If you need to use JOptionPane :

    import java.awt.*;
    import javax.swing.*;
    
    public class Main extends JFrame {
    
        private static JTextField nameField = new JTextField(20);
        private static JTextField surnameField = new JTextField();
        private static JTextField addr1Field = new JTextField();
        private static JTextField addr2Field = new JTextField();
        private static JComboBox sizes = new JComboBox(new String[] { "small", "medium", "large", "extra-large" });
    
        public Main(){
            JPanel mainPanel = new JPanel();
            mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
            getContentPane().add(mainPanel);
    
            JPanel addrPanel = new JPanel(new GridLayout(0, 1));
            addrPanel.setBorder(BorderFactory.createTitledBorder("Receiver"));
            addrPanel.add(new JLabel("Name"));
            addrPanel.add(nameField);
            addrPanel.add(new JLabel("Surname"));
            addrPanel.add(surnameField);
            addrPanel.add(new JLabel("Address 1"));
            addrPanel.add(addr1Field);
            addrPanel.add(new JLabel("Address 2"));
            addrPanel.add(addr2Field);
            mainPanel.add(addrPanel);
            mainPanel.add(new JLabel(" "));
            mainPanel.add(sizes);
    
            String[] buttons = { "OK", "Cancel"};
    
            int c = JOptionPane.showOptionDialog(
                    null,
                    mainPanel,
                    "My Panel",
                    JOptionPane.DEFAULT_OPTION,
                    JOptionPane.PLAIN_MESSAGE,
                    null,
                    buttons,
                    buttons[0]
             );
    
            if(c ==0){
                new Envelope(nameField.getText(), surnameField.getText(), addr1Field.getText()
                        , addr2Field.getText(), sizes.getSelectedIndex());
            }
    
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            pack();
            setVisible(true);
        }
    
        public static void main(String[] args) {
            new Main();
        }
    }
    
    0 讨论(0)
提交回复
热议问题