zoukankan      html  css  js  c++  java
  • Java——Swing事件处理

    import java.awt.event.WindowEvent;
    import java.awt.event.WindowListener;
    
    import javax.swing.JFrame;
    
    //=================================================
    // File Name       :	MyWindowListener_demo
    //------------------------------------------------------------------------------
    // Author          :	Common
    
    
    class MyWindowEventHandle implements WindowListener{	//实现窗口监听
    
    	@Override
    	public void windowOpened(WindowEvent e) {			//窗口打开时触发
    		// TODO 自动生成的方法存根
    		System.out.println("windowOpened-->窗口被打开");
    	}
    
    	@Override
    	public void windowClosing(WindowEvent e) {			//窗口关闭时触发,按下关闭按钮
    		// TODO 自动生成的方法存根
    		System.out.println("windowClosing-->窗口关闭");
    	}
    
    	@Override
    	public void windowClosed(WindowEvent e) {			//窗口被关闭时触发
    		// TODO 自动生成的方法存根
    		System.out.println("windowClosed-->窗口被关闭");
    	}
    
    	@Override
    	public void windowIconified(WindowEvent e) {		//窗口最小化时触发
    		// TODO 自动生成的方法存根
    		System.out.println("windowIconified-->窗口最小化");
    	}
    
    	@Override
    	public void windowDeiconified(WindowEvent e) {		//窗口从最小化恢复
    		// TODO 自动生成的方法存根
    		System.out.println("windowDeiconified-->窗口从最小化恢复");
    	}
    
    	@Override
    	public void windowActivated(WindowEvent e) {			//设置为非活动窗口是触发
    		// TODO 自动生成的方法存根
    		System.out.println("windowActivated-->窗口被选中");
    	}
    
    	@Override
    	public void windowDeactivated(WindowEvent e) {		//设置为活动窗口是触发
    		// TODO 自动生成的方法存根
    		System.out.println("windowDeactivated-->取消窗口选中");
    	}
    	
    }
    
    
    //主类
    //Function        : 	MyWindowListener_demo
    public class MyWindowListener_demo {
    
    	public static void main(String[] args) {
    		// TODO 自动生成的方法存根
    		JFrame f = new JFrame("Swing窗口");							//实例化窗体对象
    		f.addWindowListener(new MyWindowEventHandle());
    		f.setSize(440, 320);  			//设置窗体
    		f.setLocation(300,200);		//设置显示位置
    		f.setVisible(true);
    	}
    
    }
    

    import java.awt.event.WindowEvent;
    import java.awt.event.WindowListener;
    
    import javax.swing.JFrame;
    
    //=================================================
    // File Name       :	WindowAdapter
    //------------------------------------------------------------------------------
    // Author          :	Common
    
    
    // 类名:myWindowEventHandle
    // 属性:
    // 方法:
    class myWindowEventHandle extends WindowAdapter{
    	//此时可以根据自己的需要覆写方法
    	public void windowClosing(WindowEvent e) {			//窗口关闭时触发,按下关闭按钮
    		// TODO 自动生成的方法存根
    		System.out.println("windowClosing-->窗口关闭");
    		System.exit(1);
    	}
    }
    
    
    
    //主类
    //Function        : 	WindowAdapter
    public class WindowAdapter {
    
    	public static void main(String[] args) {
    		// TODO 自动生成的方法存根
    		JFrame f = new JFrame("Swing窗口");							//实例化窗体对象
    		f.addWindowListener(new myWindowEventHandle());
    //		f.addWindowListener(new WindowAdapter(){
    //			
    //			public void windowClosing(WindowEvent e) {			//窗口关闭时触发,按下关闭按钮
    //				// TODO 自动生成的方法存根
    //				System.out.println("windowClosing-->窗口关闭");
    //				System.exit(1);
    //			}
    //		});
    		
    		f.setSize(440, 320);  			//设置窗体
    		f.setLocation(300,200);		//设置显示位置
    		f.setVisible(true);
    	}
    
    }
    

    import java.awt.Font;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    
    //=================================================
    // File Name       :	ActionListener_demo
    //------------------------------------------------------------------------------
    // Author          :	Common
    
    
    // 类名:myWindowEventHandle
    // 属性:
    // 方法:
    class ActionHandle{
    	private JFrame frame = new JFrame("窗口");			//定义一个窗口对象
    	private JButton but = new JButton("显示按钮");		//定义一个按钮
    	private JLabel lab = new JLabel();									//定义一个标签
    	private JTextField jtf = new JTextField(10);					//定义一个文本域
    	private JPanel pan  = new JPanel();
    	public ActionHandle(){
    		Font font = new Font("Serief",Font.ITALIC+Font.BOLD,28);
    		lab.setFont(font);
    		lab.setText("设置显示的文字");
    		but.addActionListener(new ActionListener(){			//采用匿名内部类
    			public void actionPerformed(ActionEvent arg0){
    				if(arg0.getSource() == but){										//判断触发源是否是标签
    					lab.setText(jtf.getText());										//将文本文字设置到标签
    				}
    			}
    		});
    		
    		frame.addWindowListener(new WindowAdapter(){		//加入动作监听
    			public void windowClosing(WindowEvent e) {			//窗口关闭时触发,按下关闭按钮
    			// TODO 自动生成的方法存根
    			System.out.println("windowClosing-->窗口关闭");
    			System.exit(1);
    			}
    		});
    		frame.setLayout(new GridLayout(2,1));		//定义窗体布局,2行1列
    		pan.setLayout(new GridLayout(1,2));			//定义窗体布局,1行2列
    		pan.add(jtf);
    		pan.add(but);
    		frame.add(pan);
    		frame.add(lab);
    		frame.pack();
    		frame.setVisible(true);
    	}
    }
    
    
    //主类
    //Function        : 	ActionListener_demo
    public class ActionListener_demo {
    
    	public static void main(String[] args) {
    		// TODO 自动生成的方法存根
    		new ActionHandle();
    	}
    
    }
    

    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    
    import javax.swing.JFrame;
    import javax.swing.JScrollPane;
    import javax.swing.JTextArea;
    
    //=================================================
    // File Name       :	KeyListener_demo
    //------------------------------------------------------------------------------
    // Author          :	Common
    
    
    // 类名:myWindowEventHandle
    // 属性:
    // 方法:
    class MyKeyHandle extends JFrame implements KeyListener{
    	
    	private JTextArea text = new JTextArea();		//
    	
    	public MyKeyHandle(){
    		super.setTitle("键盘");
    		JScrollPane scr = new JScrollPane(text);		//加入滚动条
    		scr.setBounds(5, 5, 300, 200);
    		super.add(scr);
    		text.addKeyListener(this);
    		super.setSize(310, 210);
    		super.setVisible(true);
    	}
    	
    	@Override
    	public void keyTyped(KeyEvent e) {				//输入内容
    		// TODO 自动生成的方法存根
    		text.append("输入的内容是:"+e.getKeyChar()+"
    ");
    	}
    
    	@Override
    	public void keyPressed(KeyEvent e) {			//键盘按下
    		// TODO 自动生成的方法存根
    		text.append("键盘:"+KeyEvent.getKeyText(e.getKeyCode())+"键按下
    ");
    	}
    
    	@Override
    	public void keyReleased(KeyEvent e) {			//键盘释放
    		// TODO 自动生成的方法存根
    		text.append("键盘:"+KeyEvent.getKeyText(e.getKeyCode())+"键松开
    ");
    	}
    	
    }
    
    
    //主类
    //Function        : 	KeyListener_demo
    public class KeyListener_demo {
    
    	public static void main(String[] args) {
    		// TODO 自动生成的方法存根
    		new MyKeyHandle();
    	}
    
    }
    

     使用适配器

    import java.awt.event.KeyAdapter;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    
    import javax.swing.JFrame;
    import javax.swing.JScrollPane;
    import javax.swing.JTextArea;
    
    //=================================================
    // File Name       :	KeyListener_demo
    //------------------------------------------------------------------------------
    // Author          :	Common
    
    
    // 类名:myWindowEventHandle
    // 属性:
    // 方法:
    //class MyKeyHandle extends JFrame implements KeyListener{
    //	
    //	private JTextArea text = new JTextArea();		//
    //	
    //	public MyKeyHandle(){
    //		super.setTitle("键盘");
    //		JScrollPane scr = new JScrollPane(text);		//加入滚动条
    //		scr.setBounds(5, 5, 300, 200);
    //		super.add(scr);
    //		text.addKeyListener(this);
    //		super.setSize(310, 210);
    //		super.setVisible(true);
    //	}
    //	
    //	@Override
    //	public void keyTyped(KeyEvent e) {				//输入内容
    //		// TODO 自动生成的方法存根
    //		text.append("输入的内容是:"+e.getKeyChar()+"
    ");
    //	}
    //
    //	@Override
    //	public void keyPressed(KeyEvent e) {			//键盘按下
    //		// TODO 自动生成的方法存根
    //		text.append("键盘:"+KeyEvent.getKeyText(e.getKeyCode())+"键按下
    ");
    //	}
    //
    //	@Override
    //	public void keyReleased(KeyEvent e) {			//键盘释放
    //		// TODO 自动生成的方法存根
    //		text.append("键盘:"+KeyEvent.getKeyText(e.getKeyCode())+"键松开
    ");
    //	}
    //	
    //}
    
    class MyKeyHandle extends JFrame{
    	
    	private JTextArea text = new JTextArea();		//
    	
    	public MyKeyHandle(){
    		super.setTitle("键盘");
    		JScrollPane scr = new JScrollPane(text);		//加入滚动条
    		scr.setBounds(5, 5, 300, 200);
    		super.add(scr);
    		text.addKeyListener(new KeyAdapter() {
    			
    			@Override
    			public void keyTyped(KeyEvent e) {
    				// TODO 自动生成的方法存根
    				text.append("输入的内容是:"+e.getKeyChar()+"
    ");
    			}
    		});
    		super.setSize(310, 210);
    		super.setVisible(true);
    		super.addWindowListener(new WindowAdapter() {
    			public void windowClosing(WindowEvent arg0){
    				System.exit(1);
    			}
    		});
    	}
    	
    	
    	
    }
    
    
    //主类
    //Function        : 	KeyListener_demo
    public class KeyListener_demo {
    
    	public static void main(String[] args) {
    		// TODO 自动生成的方法存根
    		new MyKeyHandle();
    	}
    
    }
    

     

    import java.awt.event.MouseEvent;
    import java.awt.event.MouseMotionListener;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    
    import javax.swing.JFrame;
    
    //=================================================
    // File Name       :	MouseListener_demo
    //------------------------------------------------------------------------------
    // Author          :	Common
    
    
    // 类名:myWindowEventHandle
    // 属性:
    // 方法:
    class MyMouseMotion extends JFrame{
    	public MyMouseMotion(){
    		super.setTitle("键盘");
    	
    		super.addMouseMotionListener(new MouseMotionListener() {
    			
    			@Override
    			public void mouseMoved(MouseEvent e) {
    				// TODO 自动生成的方法存根
    				System.out.println("鼠标移动到窗体");
    			}
    			
    			@Override
    			public void mouseDragged(MouseEvent e) {
    				// TODO 自动生成的方法存根
    				System.out.println("鼠标拖拽到窗体:X"+e.getX()+",Y"+e.getY());
    			}
    		}
    
    				
    		);
    		super.setSize(310, 210);
    		super.setVisible(true);
    		super.addWindowListener(new WindowAdapter(){
    			public void windowClosing(WindowEvent e) {			//窗口关闭时触发,按下关闭按钮
    			// TODO 自动生成的方法存根
    			System.out.println("windowClosing-->窗口关闭");
    			System.exit(1);
    			}
    		});
    	}
    	
    	
    }
    
    
    //主类
    //Function        : 	MouseListener_demo
    public class MouseListener_demo {
    
    	public static void main(String[] args) {
    		// TODO 自动生成的方法存根
    		new MyMouseMotion();
    	}
    
    }
    
  • 相关阅读:
    day23,xml 和面向对象
    day22,ConfigParser,subprocess,xlrd三个模块
    re的总结
    day20,日志和正则表达式
    day19,序列化与反序列化总结,和其它的有些模块
    字符串的内建函数
    字符串和编码
    python解释器分类
    git &github 快速入门
    do export method of oracle all database tables with dmp files.
  • 原文地址:https://www.cnblogs.com/tonglin0325/p/5313747.html
Copyright © 2011-2022 走看看