当前位置: 代码迷 >> Java相关 >> 如何监听?请各位帮解释一下
  详细解决方案

如何监听?请各位帮解释一下

热度:311   发布时间:2006-06-20 18:28:43.0
如何监听?请各位帮解释一下

我写了一个窗口程序,可是对子窗口监听怎么无效??
可以的话帮我改一下,谢谢!!最好帮我解释一下。
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){}

}


搜索更多相关的解决方案: 监听  解释  

----------------解决方案--------------------------------------------------------
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){}

}
这个类虽然继承了WindowListener,但是没有加注册window事件啊
你应该在构造函数里加一句this.addWindowListener(this);


----------------解决方案--------------------------------------------------------
在哪里加?
在构造函数里加了 就编译出错了
----------------解决方案--------------------------------------------------------

请说清楚一点,在哪里加?


----------------解决方案--------------------------------------------------------
public MyJInternalFrame(String s,boolean b1,boolean b2,boolean b3,boolean b4)
{
super( s, b1, b2, b3, b4 );
this.addWindowListener(this);
}
当然在构造函数里加,怎么会出错呢
----------------解决方案--------------------------------------------------------
你能点的出来吗?冰封?
----------------解决方案--------------------------------------------------------
我没有运行他的代码,只是帮他把事件监听加进去了
其它的我没有看
----------------解决方案--------------------------------------------------------

你的电脑上不会错吗? 我的下面的错;


Microsoft Windows XP [版本 5.1.2600]
(C) 版权所有 1985-2001 Microsoft Corp.

C:\Documents and Settings\user>cd\

C:\>e:

E:\>cd lx

E:\lx>javac MainWindow.java

E:\lx>java MainWindow

E:\lx>javac MainWindow.java
MainWindow.java:69: 找不到符号
符号: 方法 addWindowListener(MyJInternalFrame)
位置: 类 MyJInternalFrame
this.addWindowListener(this);
^
1 错误

E:\lx>


----------------解决方案--------------------------------------------------------
怎么会出错呢,你已经实现了WindowListener的接口啊

----------------解决方案--------------------------------------------------------
import java.awt.*;
import java.awt.event.*;
public class MyAdapterDemo
{
public static void main(String args[])
{
MyFrame frm=new MyFrame();
frm.show();
}
}
class MyFrame extends Frame
{
boolean w;
public MyFrame()
{
w=false;
setTitle("测试适配器类");
setSize(300,200);
// MyPanel panel=new MyPanel();
// add(panel);
addWindowListener(new MyWindowAdapter());
}
private class MyWindowAdapter extends WindowAdapter
{
public void windowClosing(WindowEvent wevent)
{
if(w)
dispose();
else
System.out.println("22222");
}
}
}

不知道这样能不能帮着你
----------------解决方案--------------------------------------------------------