简体   繁体   English

如何从一个JFrame或JPanel到Netbeans中的另一个?

[英]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. 我正在学习使用Java教程( 带有NetBeans IDELearning Swing课)做一些GUI,并且我已经完成了他们教的项目,到目前为止一切正常。

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? 但是,问题来了,我该如何制作一个程序,该程序首先在JFrameJPanel呈现诸如Hello User的消息和带有Enter的按钮,以便在单击按钮时进入一个新的框架或面板,您在哪里有了摄氏到华氏度的转换器,就像我在页面上给出的示例一样?

In case you need the information, I am using Netbeans IDE 8.1 如果您需要这些信息,我正在使用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). 在第一个JFrame的任何事件处理程序中,只需为第二个JFrame创建一个类的对象,然后在新JFrame上创建setVisible(true),然后创建this.setVisible(false)。

eg. 例如。 in class JFrameOne there is a button which when clicked calls: 在JFrameOne类中,有一个按钮,当单击该按钮时会调用:

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. Andrew Thompson提供的链接包含很多人的意见,这些人对这是否是一种好的做法有很强的见解,但我建议您尝试一下,考虑其他选择并下定决心。 At the very least it is easy to do. 至少很容易做到。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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