简体   繁体   中英

Java: how to open one Jframe from another Jframe which is not in the same file but in the same package;

Java: how to open one Jframe from another Jframe which is not in the same file but in the same package; For eg: project package is test1 and it has 2 Jframes ( home1 and home2 ) Need to open that second frame from the first one (home2 from home1) while clicking on a JButton called 'NEXT'.
can anyone help..

So your problem is to open a new frame from a departure frame ? It's simple you just need to instanciate a new frame object like in the following :

JFrame home2 = new Home2(); // don't forget the import since it's a custom made Frame ;)
home2.setVisible(true);

Now you want that to be done when you click on a JButton. To do so you need to add an ActionListener, using an anonyous class, to the JButton with the previous code.

jb.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent ae) {
            //stuff
        }
    });

See the addActionListener() method of JButton and the ActionListener .

You can show the other JFrame simply by calling

home2 h2 = new home2();
h2.setVisible(true);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM