Java 如何在不使用swing的情况下在netbeans中连接两个JFrame?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20987838/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to connect two JFrames in netbeans without using swing?
提问by KS1
I am make a project on cars. How can I make distributor frame popup and cars frame not visible and close automatic? Kindly send any solution in simple and effective way. I have done coding this way:-
我正在做一个关于汽车的项目。如何使分配器框架弹出窗口和汽车框架不可见并自动关闭?请以简单有效的方式发送任何解决方案。我已经通过这种方式完成了编码:-
{
Cars frm1=new Cars();
Distributor frm2=new Distributor();
frm2.setVisible(true);
frm1.setVisible(false);
frm1.setDefaultCloseOperation(frm1.DISPOSE_ON_CLOSE);
}
采纳答案by Paul Samsotha
".Please help me to how I can make distributor frame popup and cars frame is not visible and close automatic."
“.请帮助我如何使分配器框架弹出和汽车框架不可见并自动关闭。”
Ok so in Netbeans GUI Builder, you may want to do the following (this is assuming you have created two separate JFrame
form files
好的,在 Netbeans GUI Builder 中,您可能想要执行以下操作(假设您已经创建了两个单独的JFrame
表单文件
- In the frame that is the launching program (we'll call it
MyFrame1
) add a button to it (we'll call itjButton1
) Add a listener to the button, then the following code should be auto-generated
public void jButton1ActionPerforemd(javax.swing.ActionEvent evt) { }
In that
actionPerformed
, just instantiate the second frame (we'll call itMyFrame2
) andsetVisible(false)
toMyFrame1
.MyFrame2
should already be visible upon instantiation, so you wouldn't have tosetVisisble(true)
on itpublic void jButton1ActionPerforemd(javax.swing.ActionEvent evt) { MyFrame2 frame2 = new MyFrame2(); MyFrame1.this.setVisible(false); // You can also use MyFrame1.this.dispose(); dependind if you ever need to use that frame again }
- 在作为启动程序的框架(我们称之为
MyFrame1
)中添加一个按钮(我们称之为jButton1
) 给按钮添加一个监听器,那么下面的代码应该是自动生成的
public void jButton1ActionPerforemd(javax.swing.ActionEvent evt) { }
在那
actionPerformed
,只需实例化第二帧(我们称之为MyFrame2
)和setVisible(false)
toMyFrame1
。MyFrame2
在实例化时应该已经可见,所以你不必setVisisble(true)
在它上面public void jButton1ActionPerforemd(javax.swing.ActionEvent evt) { MyFrame2 frame2 = new MyFrame2(); MyFrame1.this.setVisible(false); // You can also use MyFrame1.this.dispose(); dependind if you ever need to use that frame again }
I think this should work
我认为这应该有效
回答by user3452580
you need to setVisible Jframe2 as true...so it can apear on output sceen
您需要将Visible Jframe2 设置为true ...这样它就可以出现在输出屏幕上
public void jButton1ActionPerforemd(javax.swing.ActionEvent evt)
{
myFrame2 frame2=new myframe2();
myframe1.this.setVisible(false);
frame2.setVisible(true);
}
回答by Ray Njire
private void BTNConvertActionPerformed(java.awt.event.ActionEvent evt) {
/*
This is the action performed event for my Button "BTNConvert"
*/
java.awt.EventQueue.invokeLater
(new Runnable()
{
public void run()
{
new JFrame2().setVisible(true);
}
});
/*
This will set the second Frame Visible.
*/
JFrame1.this.setVisible(false);
/*
This should set the first frame invisible or whatever. Any other code should be
written before the curly brace below.
*/
}
//You're Welcome.
回答by HANSON KIBET
create action event for the button such that when when you click will take you to the next page for my case next page is nextjFrame
为按钮创建动作事件,以便当您单击时将带您到下一页,因为我的情况下一页是 nextjFrame
private void nextButtonActionPerformed(java.awt.event.ActionEvent evt) {
setVisible(false);
nextjFrame ob=new nextjFrame();
ob.setVisible(true);
}