Java——Swing事件处理

 

import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.JFrame;

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-->取消窗口选中");
	}
	
}

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;

class myWindowEventHandle extends WindowAdapter{
	//此时可以根据自己的需要覆写方法
	public void windowClosing(WindowEvent e) {			//窗口关闭时触发,按下关闭按钮
		// TODO 自动生成的方法存根
		System.out.println("windowClosing-->窗口关闭");
		System.exit(1);
	}
}

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;

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);
	}
}

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;

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()+"\n");
	}

	@Override
	public void keyPressed(KeyEvent e) {			//键盘按下
		// TODO 自动生成的方法存根
		text.append("键盘:"+KeyEvent.getKeyText(e.getKeyCode())+"键按下\n");
	}

	@Override
	public void keyReleased(KeyEvent e) {			//键盘释放
		// TODO 自动生成的方法存根
		text.append("键盘:"+KeyEvent.getKeyText(e.getKeyCode())+"键松开\n");
	}
	
}

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;

//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()+"\n");
//	}
//
//	@Override
//	public void keyPressed(KeyEvent e) {			//键盘按下
//		// TODO 自动生成的方法存根
//		text.append("键盘:"+KeyEvent.getKeyText(e.getKeyCode())+"键按下\n");
//	}
//
//	@Override
//	public void keyReleased(KeyEvent e) {			//键盘释放
//		// TODO 自动生成的方法存根
//		text.append("键盘:"+KeyEvent.getKeyText(e.getKeyCode())+"键松开\n");
//	}
//	
//}

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()+"\n");
			}
		});
		super.setSize(310, 210);
		super.setVisible(true);
		super.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent arg0){
				System.exit(1);
			}
		});
	}
	
	
	
}

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;

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);
			}
		});
	}
	
	
}

public class MouseListener_demo {

	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		new MyMouseMotion();
	}

}

 

posted @ 2016-03-23 23:46  tonglin0325  阅读(364)  评论(0编辑  收藏  举报