问一个GUI的问题

wiseforjava 2004-05-03 11:28:20
在主窗体上点击某个button会跳出另一个窗口,我现在使用下面的方法来实现的
在jb菜单中new 一个新的Frame,假设它的名字是Frame2,然后在上面放置要的组件
假设主窗体是Frame1,如果需要切换到Frame2的时,
Frame Frame2 = new Frame2();
Frame2.show() ;
Frame2.setSize(500,400) ;
但现在有个问题我在关闭Frame2时Frame1也一起关闭了。要怎样才能避免这样呢?关闭Frame2而Frame1继续存在。
...全文
21 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
baby429 2004-05-05
  • 打赏
  • 举报
回复
把system.exit(0);改成 this.dispose();
nodreamer 2004-05-04
  • 打赏
  • 举报
回复
呵呵
分都给我吧,这个问题我已经解决了
你只要把

protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
删掉就可以了
这样在点关闭的时候只会关闭frame2,
因为System.exit(0);这句是关闭主程序的意思
wiseforjava 2004-05-04
  • 打赏
  • 举报
回复
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类的代码 请指教。
wiseforjava 2004-05-04
  • 打赏
  • 举报
回复
非常感谢,问题已经解决了
但是给分的那个密码还能得到吗? 我没记下来
真是不好意思啊
要不你说个标题我再用这个标题发个帖子,你进来拿分ok?
dragon525 2004-05-04
  • 打赏
  • 举报
回复
要说的楼上都已经说了
system.exit(0)是退出程序的意思.所以当你关闭frame2时,frame2也会随着关闭.

如不是主窗体通常在
jbinit方法中加上setDefaultCloseOperation(DISPOSE_ON_CLOSE);
就可以了.
dragon525 2004-05-03
  • 打赏
  • 举报
回复
关闭Frame2的代码你是怎么写的?
在Frame2类中要这样写啊setDefaultCloseOperation(DISPOSE_ON_CLOSE);

你把你的Frame2类的代码贴部分出来看看。。。

50,528

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧