刚刚接触java,自学中~~~~现在遇到一个问题,希望得到大家的帮助,谢谢。问题描述如下:
我新建了两个窗体类分别命名 aframe bframe,在aframe上有一个控件jbutton1,按下后通过代码 new bframe().setVisible(true); 打开bframe。 在bframe上有一个控件jbutton2。我希望通过这个按键实现重新打开aframe(将之前打开的aframe关闭)或者实现让aframe恢复到初始状态不知道如何操作?谢谢大家帮助了(我尝试了new aframe().setVisible(true); dispose()能打开新的窗口,但是之前开着的窗体依然处于开启状态……)
我新建了两个窗体类分别命名 aframe bframe,在aframe上有一个控件jbutton1,按下后通过代码 new bframe().setVisible(true); 打开bframe。 在bframe上有一个控件jbutton2。我希望通过这个按键实现重新打开aframe(将之前打开的aframe关闭)或者实现让aframe恢复到初始状态不知道如何操作?谢谢大家帮助了(我尝试了new aframe().setVisible(true); dispose()能打开新的窗口,但是之前开着的窗体依然处于开启状态……)
我 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();
}
}
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);
}
}
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);
}
}