我写了一个窗口程序,可是对子窗口监听怎么无效??
可以的话帮我改一下,谢谢!!最好帮我解释一下。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MainWindow extends JFrame{
private JDesktopPane theDesktop;
private JMenuBar bar;
public MainWindow()
{
super("这里是主窗口");
bar = new JMenuBar();
JMenu addWindow = new JMenu("add");
JMenuItem open = new JMenuItem("add one");
addWindow.add( open );
bar.add ( addWindow );
setJMenuBar( bar );
theDesktop = new JDesktopPane();//用来放子窗
getContentPane().add( theDesktop );//把子装子窗的面板放进
open.addActionListener(
new ActionListener(){
public void actionPerformed( ActionEvent ae ){
MyJInternalFrame frame = new MyJInternalFrame(
"这里是子窗口",true,true,true,true);
Container container = frame.getContentPane();
MyJPanel panel = new MyJPanel();
container.add( panel, BorderLayout.CENTER );
frame.pack();
theDesktop.add( frame );
frame.setVisible( true );
}
}
);
setSize(800,600);
setVisible( true );
}
public static void main(String arg[])
{
MainWindow mainWindow = new MainWindow();
mainWindow.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}
}
class MyJPanel extends JPanel{
public MyJPanel()
{
}
public void paintComponent( Graphics g )
{
super.paintComponent( g );
g.drawRect( 25,25,450,300);
g.drawString("在窗口里我也能画",30,60);
}
public Dimension getPerferredSize()
{
return new Dimension(700,500);
}
}
class MyJInternalFrame extends JInternalFrame implements WindowListener{
public MyJInternalFrame(){}
public MyJInternalFrame(String s,boolean b1,boolean b2,boolean b3,boolean b4)
{
super( s, b1, b2, b3, b4 );
}
public void windowClosing( WindowEvent we )
{
Sysem.out.println("窗口关闭");
JOptionPane.showMessageDialog(null,"窗口关闭");
}
public void windowClosed(WindowEvent we){}
public void windowActivated(WindowEvent we){}
public void windowDeactivated(WindowEvent we){}
public void windowIconified(WindowEvent we){}
public void windowDeiconified(WindowEvent we){}
public void windowOpened(WindowEvent we){}
}