问一个GUI的问题
在主窗体上点击某个button会跳出另一个窗口,我现在使用下面的方法来实现的
在jb菜单中new 一个新的Frame,假设它的名字是Frame2,然后在上面放置要的组件
假设主窗体是Frame1,如果需要切换到Frame2的时,
Frame Frame2 = new Frame2();
Frame2.show() ;
Frame2.setSize(500,400) ;
但现在有个问题我在关闭Frame2时Frame1也一起关闭了。要怎样才能避免这样呢?关闭Frame2而Frame1继续存在。
问题点数:0、回复次数:6Top
1 楼dragon525()回复于 2004-05-03 23:49:00 得分 0
关闭Frame2的代码你是怎么写的?
在Frame2类中要这样写啊setDefaultCloseOperation(DISPOSE_ON_CLOSE);
你把你的Frame2类的代码贴部分出来看看。。。Top
2 楼wiseforjava(枫影)回复于 2004-05-04 00:26:32 得分 0
package mapeditor;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
public class OpenDBFrame extends JFrame {
JPanel contentPane;
JLabel jLabel1 = new JLabel();
JEditorPane jEditorPane1 = new JEditorPane();
JLabel jLabel2 = new JLabel();
JButton jButton1 = new JButton();
JButton jButton2 = new JButton();
JEditorPane jEditorPane2 = new JEditorPane();
//Construct the frame
public OpenDBFrame() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception {
contentPane = (JPanel) this.getContentPane();
jLabel1.setText("Choose DB Name");
jLabel1.setBounds(new Rectangle(41, 39, 91, 29));
contentPane.setLayout(null);
this.setSize(new Dimension(400, 300));
this.setTitle("Open DB");
jEditorPane1.setText("jEditorPane1");
jEditorPane1.setBounds(new Rectangle(55, 94, 112, 92));
jLabel2.setHorizontalAlignment(SwingConstants.LEADING);
jLabel2.setText("Choose Directory");
jLabel2.setBounds(new Rectangle(193, 35, 100, 40));
jButton1.setBounds(new Rectangle(197, 221, 100, 29));
jButton1.setText("Cancel");
jButton2.setBounds(new Rectangle(57, 222, 99, 27));
jButton2.setText("OK");
jEditorPane2.setText("jEditorPane2");
jEditorPane2.setBounds(new Rectangle(214, 93, 118, 93));
contentPane.add(jLabel1, null);
contentPane.add(jLabel2, null);
contentPane.add(jEditorPane1, null);
contentPane.add(jEditorPane2, null);
contentPane.add(jButton2, null);
contentPane.add(jButton1, null);
}
//Overridden so we can exit when window is closed
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}
以上是Frame2类的代码 请指教。Top
3 楼nodreamer(我想抽烟)回复于 2004-05-04 12:31:58 得分 0
呵呵
分都给我吧,这个问题我已经解决了
你只要把
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
删掉就可以了
这样在点关闭的时候只会关闭frame2,
因为System.exit(0);这句是关闭主程序的意思Top
4 楼dragon525()回复于 2004-05-04 13:53:16 得分 0
要说的楼上都已经说了
system.exit(0)是退出程序的意思.所以当你关闭frame2时,frame2也会随着关闭.
如不是主窗体通常在
jbinit方法中加上setDefaultCloseOperation(DISPOSE_ON_CLOSE);
就可以了.Top
5 楼wiseforjava(枫影)回复于 2004-05-04 19:24:23 得分 0
非常感谢,问题已经解决了
但是给分的那个密码还能得到吗? 我没记下来
真是不好意思啊
要不你说个标题我再用这个标题发个帖子,你进来拿分ok?Top
6 楼baby429(baby)回复于 2004-05-05 00:31:44 得分 0
把system.exit(0);改成 this.dispose();Top




