JButton btn=new JButton(new AbstractAction("关闭并打开") { @Override public void actionPerformed(ActionEvent e) { oldFrame.dispose();// 关闭并销毁,无需销毁可采用oldFrame.setVisible(false); newFrame.setVisible(true);// 打开新窗口 }});
import javax.swing.*;
import java.awt.Rectangle;
import java.awt.event.*;
public class Swing7 extends JFrame implements ActionListener {
JButton jb = new JButton();
public Swing7() {
this.setTitle("Java——");
jb.setText("确定");
jb.setMnemonic('a');
this.add(jb);
this.setBounds(200, 300, 250, 300);
ctionListener就是Swing7实例。
}
public void actionPerformed(ActionEvent e) {// 实现ActionListener接口的actionPerformed接口。
JFrame frame = new JFrame("新窗口");//构造一个新的JFrame,作为新窗口。
frame.setBounds(// 让新窗口与Swing7窗口示例错开50像素。
new Rectangle(
(int) this.getBounds().getX() + 50,
(int) this.getBounds().getY() + 50,
(int) this.getBounds().getWidth(),
(int) this.getBounds().getHeight()
)
);
JLabel jl = new JLabel();// 注意类名别写错了。
frame.getContentPane().add(jl);
jl.setText("这是新窗口");
jl.setVerticalAlignment(JLabel.CENTER);
jl.setHorizontalAlignment(JLabel.CENTER);// 注意方法名别写错了。
frame.setVisible(true);
}
public static void main(String args[]) {
Swing7 s = new Swing7();
}
}
假设本身是Form1,第二个窗口是Form2,你双击按钮进入那个按钮的点击事件,编写以下代码:
Form2 f2=new Form2();
f2.Show();
this.Hide();
简单的程序代码,运用代码就可以完美解决