0

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

Misa Lazovic
  • 2,805
  • 10
  • 32
  • 38
Sebe
  • 17
  • 7
  • 2
    You shouldn't use multiple `JFrames`, either use [dialogs](https://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html) or a [`CardLayout`](https://docs.oracle.com/javase/tutorial/uiswing/layout/card.html) – Lukas Rotter Dec 04 '15 at 14:13
  • 2
    *"How to go from one JFrame"* See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) *".. or JPanel"* [`CardLayout`](http://download.oracle.com/javase/8/docs/api/java/awt/CardLayout.html) as shown in [this answer](http://stackoverflow.com/a/5786005/418556). *"..to another in netbeans?"* Same way as in Java. Unless you mean in the GUI builder, which doesn't really work for developers until they understand *how* to code the GUI by hand (in Java). – Andrew Thompson Dec 04 '15 at 14:14
  • Oh ok, interesting the article about the use of Multiple JFrames. I think i just need to get the idea about adding buttons instead of Lists in the CardLayout. Thanks both – Sebe Dec 04 '15 at 14:35

1 Answers1

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.

WillShackleford
  • 6,918
  • 2
  • 17
  • 33