简体   繁体   中英

How to go from one JFrame or JPanel to another in Netbeans?

I am learning to do some GUI with the Java Tutorials (the Learning Swing with the NetBeans IDE lesson) and I already did the project they teach, everything OK so far.

But, here comes the question, how can I make a program that first presents, in a JFrame or a JPanel , a message like Hello User and a button with Enter , so that when you click the button you come in a new frame or panel, where you have the a converter for Celsius to Fahrenheit, like the example in the page I gave?

In case you need the information, I am using Netbeans IDE 8.1

In any of the event handlers for the first JFrame just create an object of the class for second JFrame and setVisible(true) on the new JFrame and then this.setVisible(false).

eg. in class JFrameOne there is a button which when clicked calls:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    new JFrameTwo().setVisible(true);
    this.setVisible(false);
} 

The link Andrew Thompson provided includes a lot of discussion from people with strong opinions about whether this is good practice, but I suggest you try it, consider the alternatives and make up your own mind. At the very least it is easy to do.

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