Java : Swing : Hide frame after button pressed

后端 未结 4 633
闹比i
闹比i 2021-01-14 17:37

I have a button in a java frame that when pressed it reads a value from a text field and uses that string as a port name attempting to connect to a serial device.

If

4条回答
  •  长发绾君心
    2021-01-14 18:30

    for Swing GUI is better create only once JFrame and another Top-Level Containers would be JDialog or JWindow(un-decorated by default),

    simple example here

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class SuperConstructor extends JFrame {
    
        private static final long serialVersionUID = 1L;
    
        public SuperConstructor() {
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setPreferredSize(new Dimension(300, 300));
            setTitle("Super constructor");
            Container cp = getContentPane();
            JButton b = new JButton("Show dialog");
            b.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent evt) {
                    FirstDialog firstDialog = new FirstDialog(SuperConstructor.this);
                }
            });
            cp.add(b, BorderLayout.SOUTH);
            JButton bClose = new JButton("Close");
            bClose.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent evt) {
                    System.exit(0);
                }
            });
            add(bClose, BorderLayout.NORTH);
            pack();
            setVisible(true);
        }
    
        public static void main(String args[]) {
            EventQueue.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    SuperConstructor superConstructor = new SuperConstructor();
                }
            });
        }
    
        private class FirstDialog extends JDialog {
    
            private static final long serialVersionUID = 1L;
    
            FirstDialog(final Frame parent) {
                super(parent, "FirstDialog");
                setPreferredSize(new Dimension(200, 200));
                setLocationRelativeTo(parent);
                setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
                setModalityType(Dialog.ModalityType.DOCUMENT_MODAL);
                JButton bNext = new JButton("Show next dialog");
                bNext.addActionListener(new ActionListener() {
    
                    @Override
                    public void actionPerformed(ActionEvent evt) {
                        SecondDialog secondDialog = new SecondDialog(parent, false);
                    }
                });
                add(bNext, BorderLayout.NORTH);
                JButton bClose = new JButton("Close");
                bClose.addActionListener(new ActionListener() {
    
                    @Override
                    public void actionPerformed(ActionEvent evt) {
                        setVisible(false);
                    }
                });
                add(bClose, BorderLayout.SOUTH);
                pack();
                setVisible(true);
            }
        }
        private int i;
    
        private class SecondDialog extends JDialog {
    
            private static final long serialVersionUID = 1L;
    
            SecondDialog(final Frame parent, boolean modal) {
                //super(parent); // Makes this dialog unfocusable as long as FirstDialog is visible
                setPreferredSize(new Dimension(200, 200));
                setLocation(300, 50);
                setModal(modal);
                setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
                setTitle("SecondDialog " + (i++));
                JButton bClose = new JButton("Close");
                bClose.addActionListener(new ActionListener() {
    
                    @Override
                    public void actionPerformed(ActionEvent evt) {
                        setVisible(false);
                    }
                });
                add(bClose, BorderLayout.SOUTH);
                pack();
                setVisible(true);
            }
        }
    }
    

    better would be re-use Top-Level Containers, as create lots of Top-Level Containers on Runtime (possible memory lack)

提交回复
热议问题