大家帮帮我吧,关于关闭窗体的问题,谢谢谢谢

jjdbest 2008-01-17 04:47:24
刚刚接触java,自学中~~~~现在遇到一个问题,希望得到大家的帮助,谢谢。问题描述如下:
我新建了两个窗体类分别命名 aframe bframe,在aframe上有一个控件jbutton1,按下后通过代码 new bframe().setVisible(true); 打开bframe。 在bframe上有一个控件jbutton2。我希望通过这个按键实现重新打开aframe(将之前打开的aframe关闭)或者实现让aframe恢复到初始状态不知道如何操作?谢谢大家帮助了。。。(我尝试了new aframe().setVisible(true); dispose()能打开新的窗口,但是之前开着的窗体依然处于开启状态……)
...全文
99 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
wangle860102 2008-01-17
  • 打赏
  • 举报
回复
构造方法加入一个super(aframe,false)就可以了
yiyi_wx 2008-01-17
  • 打赏
  • 举报
回复
打开另一个窗体的同时关闭本窗体

另外,LZ需注意:类名称的命名首字母需大写
方法名称的命名首字母小写,并采取驼峰命名法
这是一种规范~
呵呵~
yiyi_wx 2008-01-17
  • 打赏
  • 举报
回复


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import java.awt.Rectangle;
import javax.swing.JFrame;

class FrameB extends JFrame {
public FrameB() {
super("B窗体");
try {
jbInit();
this.setSize(200,200);
this.setLocation(100,100);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
} catch (Exception ex) {
ex.printStackTrace();
}
}

private void jbInit() throws Exception {
this.setLayout(null);
jButton1.setBounds(new Rectangle(57, 106, 116, 42));
jButton1.setText("打开A窗口");
jButton1.addActionListener(new FrameB_jButton1_actionAdapter(this));
this.add(jButton1);
}

JButton jButton1 = new JButton();
public void jButton1_actionPerformed(ActionEvent e) {
FrameA a = new FrameA();
this.dispose();
}

}


class FrameB_jButton1_actionAdapter implements ActionListener {
private FrameB adaptee;
FrameB_jButton1_actionAdapter(FrameB adaptee) {
this.adaptee = adaptee;
}

public void actionPerformed(ActionEvent e) {
adaptee.jButton1_actionPerformed(e);
}
}

yiyi_wx 2008-01-17
  • 打赏
  • 举报
回复


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import java.awt.Rectangle;

class FrameA extends JFrame {
public FrameA() {
super("A窗体");
try {
jbInit();
this.setSize(200,200);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
} catch (Exception ex) {
ex.printStackTrace();
}
}

private void jbInit() throws Exception {
this.setLayout(null);
jButton1.setBounds(new Rectangle(57, 106, 112, 42));
jButton1.setText("打开B窗口");
jButton1.addActionListener(new FrameA_jButton1_actionAdapter(this));
this.add(jButton1);
}

JButton jButton1 = new JButton();
public void jButton1_actionPerformed(ActionEvent e) {
FrameB b = new FrameB();
this.dispose();
}

public static void main(String[] arg) {
new FrameA();
}

}

class FrameA_jButton1_actionAdapter implements ActionListener {
private FrameA adaptee;
FrameA_jButton1_actionAdapter(FrameA adaptee) {
this.adaptee = adaptee;
}

public void actionPerformed(ActionEvent e) {
adaptee.jButton1_actionPerformed(e);
}
}

jjdbest 2008-01-17
  • 打赏
  • 举报
回复
问题解决了。谢谢大家
老紫竹 2008-01-17
  • 打赏
  • 举报
回复
new bframe(aframe);

把aframe作为参数传过去,保存在本地属性中,然后再那面直接调用 aframe的显示代码。
我 Swing 很烂,我这个代码只是示意而已,请别笑话,呵呵!


import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

class FrameA extends Frame {
private FrameA self = this;

public FrameA() {
setSize(40, 60);
setTitle("Ping命令");
setVisible(true);
setLayout(new FlowLayout());
setResizable(false);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
Button btn = new Button("打开FrameB");
btn.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent evt) {
new FrameB(self);
self.setVisible(false);
}
});
add(btn);
validate();
}
}

class FrameB extends Frame {
private final FrameA frame;

public FrameB(FrameA framea) {
this.frame = framea;
setSize(400, 600);
this.setLocation(400, 0);
setVisible(true);
setLayout(new FlowLayout());
setResizable(false);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
Button btn = new Button("打开FrameA");
btn.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent evt) {
frame.setVisible(true);
}
});
add(btn);
validate();
}
}

public class TestFrame extends Frame {
public static void main(String[] args) {
new FrameA();
}
}

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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