Insert JDialog before frameview or main frame

后端 未结 3 1631
暗喜
暗喜 2021-01-27 10:08

i want to add Jdialog before frameview...my frameview consist of my main page of application. i just want to add Jdialog which get password from user and then ent

3条回答
  •  清酒与你
    2021-01-27 10:30

    You could use following constructors to make a JDialog parent-less

    JDialog d = new JDialog((Dialog)null);
    
    JDialog d = new JDialog((Window)null);
    
    JDialog d = new JDialog((Frame)null);
    

    Quick code sample :

    public class TestFrame extends JFrame{
        public TestFrame(){
            setSize(100,200);
        }
    
        public static void main(String[] args) {
                //Using null constructor ( Since JDK 6)
            final JDialog loginDialog = new JDialog((Dialog)null);
                //just a button for demo
            JButton okButton = new JButton("Login");
            okButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent ae) {
                    TestFrame test = new TestFrame();
                    test.setVisible(true);
                    loginDialog.dispose();
                }
            });
            loginDialog.getContentPane().add(okButton);
            loginDialog.pack();
            loginDialog.setVisible(true);
        }
    }
    

提交回复
热议问题