java拿到窗体的句柄后如何监听一个 窗体上的控件? 30
1个回答
展开全部
/**
* WindowListener:窗口监听事件 ex:关闭、最小化...
* @author Administrator
*/
public class WindowListenerDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
JFrame frame = new JFrame("窗口程序");//创建一个带标题的窗口对象frame
frame.setVisible(true);//显示窗口
frame.setSize(600, 400);//设置窗口大小
frame.setLocationRelativeTo(null);//设置窗口居中
//frame.setDefaultCloseOperation(3);//点击X关闭程序 3为结束程序
frame.setLayout(null);//清空布局
MyWindowListener myWindowListener = new MyWindowListener();//创建一个窗口监听事件对象
frame.addWindowListener(myWindowListener);//添加myMouseListener鼠标监听事件
frame.dispose();//关闭程序
}
}
class MyWindowListener implements WindowListener{
@Override
public void windowOpened(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowClosing(WindowEvent e) {
// TODO Auto-generated method stub
}
/*
* 关闭窗口之后,自动调用此方法
*/
@Override
public void windowClosed(WindowEvent e) {
// TODO Auto-generated method stub
System.out.println("您关闭了窗口...");
}
/*
* 最小化窗口之后,自动调用此方法
*/
@Override
public void windowIconified(WindowEvent e) {
// TODO Auto-generated method stub
System.out.println("您把窗口最小化了....");
}
@Override
public void windowDeiconified(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowActivated(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowDeactivated(WindowEvent e) {
// TODO Auto-generated method stub
}
}
* WindowListener:窗口监听事件 ex:关闭、最小化...
* @author Administrator
*/
public class WindowListenerDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
JFrame frame = new JFrame("窗口程序");//创建一个带标题的窗口对象frame
frame.setVisible(true);//显示窗口
frame.setSize(600, 400);//设置窗口大小
frame.setLocationRelativeTo(null);//设置窗口居中
//frame.setDefaultCloseOperation(3);//点击X关闭程序 3为结束程序
frame.setLayout(null);//清空布局
MyWindowListener myWindowListener = new MyWindowListener();//创建一个窗口监听事件对象
frame.addWindowListener(myWindowListener);//添加myMouseListener鼠标监听事件
frame.dispose();//关闭程序
}
}
class MyWindowListener implements WindowListener{
@Override
public void windowOpened(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowClosing(WindowEvent e) {
// TODO Auto-generated method stub
}
/*
* 关闭窗口之后,自动调用此方法
*/
@Override
public void windowClosed(WindowEvent e) {
// TODO Auto-generated method stub
System.out.println("您关闭了窗口...");
}
/*
* 最小化窗口之后,自动调用此方法
*/
@Override
public void windowIconified(WindowEvent e) {
// TODO Auto-generated method stub
System.out.println("您把窗口最小化了....");
}
@Override
public void windowDeiconified(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowActivated(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowDeactivated(WindowEvent e) {
// TODO Auto-generated method stub
}
}
追问
就是这个窗体是windos自带的不是我们自己创建的 窗体该怎样去监听他的一个控件
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询