日期:2014-05-20 浏览次数:20709 次
package test.ww.swing; import java.awt.Color; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowEvent; import java.awt.event.WindowListener; import javax.swing.JButton; import javax.swing.JFrame; public class MyTest { public static void main(String[] args) { JFrame A = new JFrame("A"); JButton openB = new JButton("打开B"); A.add(openB); A.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); A.setSize(400, 300); A.setVisible(true); openB.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { final JFrame B = new JFrame("B"); B.setSize(400, 300); B.setBackground(Color.BLACK); B.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); B.setVisible(true); B.addWindowListener(new WindowListener() { @Override public void windowOpened(WindowEvent e) { // TODO Auto-generated method stub } @Override public void windowIconified(WindowEvent e) { // TODO Auto-generated method stub } @Override public void windowDeiconified(WindowEvent e) { // TODO Auto-generated method stub } @Override public void windowDeactivated(WindowEvent e) { // TODO Auto-generated method stub } @Override public void windowClosing(WindowEvent e) { B.dispose(); } @Override public void windowClosed(WindowEvent e) { // TODO Auto-generated method stub } @Override public void windowActivated(WindowEvent e) { // TODO Auto-generated method stub } }); } }); } }
------解决方案--------------------
可以有以下两种解决方法:
①.如果你是用awt写图形用户界面,就可以让B界面声明继承windowActionListener接口,并实现里面的抽象方法。
②.如果你是用javax.swing编写图形用户界面,就可以直接用this.setDefaultCloseOperation(JFrame.EXIT_ON_CLoSE);该方法就可以实图形界面B的关闭!
------解决方案--------------------
为什么要用awt这种过时的东西呢,有swing为什么不用呢
------解决方案--------------------
setVisibel(false)
------解决方案--------------------
setVisibel(true)或者false 用监听器的方法隐藏你想要影藏的窗体,显示你显示的窗体。