1. 程式人生 > >java隨筆——GUI

java隨筆——GUI

//AWT事件處理
import java.awt.event.*;
import java.awt.*;
public class Main
{
	static public void main(String[] args)
	{
		Frame f=new Frame("我的窗體!");//建立視窗
		f.setSize(400,300);
		f.setLocation(300,200);
		f.setVisible(true);//視窗視覺化
		//監聽器
		MyWindowListener mw=new MyWindowListener();
		f.addWindowListener(mw);
		//匿名內部類實現
		Button btn=new Button("EXIT");
		f.add(btn);
		btn.addMouseListener(new MouseListener() {
			public void mouseClicked(MouseEvent e)
			{
				System.exit(0);
			}
		});
	}
}
class MyWindowListener implements WindowListener
{
	public void windowClosing(WindowEvent e)
	{
		Window window=e.getWindow();
		window.setVisible(false);
		window.dispose();
	}
}
//窗體事件
import java.awt.*;
import java.awt.event.*;
public class Main
{
	static public void main(String[] args)
	{
		final Frame f=new Frame();
		f.setSize(400,300);
		f.setLocation(300, 200);
		f.setVisible(true);
		f.addWindowListener(new WindowListener() {
			public void windowOpened(WindowEvent e)
			{
				System.out.println("窗體開啟事件");
			}
			public void windowIconified(WindowEvent e)
			{
				System.out.println("窗體圖示化事件");
			}
			public void windowDeiconified(WindowEvent e)
			{
				System.out.println("取消窗體圖示化事件");
			}
			public void windowDeactivated(WindowEvent e)
			{
				System.out.println("窗體停用事件");
			}
			public void windowClosing(WindowEvent e)
			{
				System.out.println("窗體正在關閉事件");
//				Window w=e.getWindow();
//				w.setVisible(false);
//				w.dispose();
			}
			public void windowClosed(WindowEvent e)
			{
				System.out.println("窗體關閉事件");
			}
			public void windowActivated(WindowEvent e)
			{
				System.out.println("窗體啟用事件");
			}
		});
	}
}
/*
 * 窗體的開啟,關閉,縮小有對應的事件
 */
//滑鼠事件
import java.awt.*;
import java.awt.event.*;
public class Main
{
	static public void main(String[] args)
	{
		final Frame f=new Frame();
		f.setLayout(new BorderLayout());
		f.setSize(400, 300);
		f.setLocation(300,200);
		f.setVisible(true);
		Button but=new Button("Button");
		f.add(but);
		but.addMouseListener(new MouseListener()
		{
			public void mouseReleased(MouseEvent e)
			{
				System.out.println("滑鼠放開事件");
			}
			public void mousePressed(MouseEvent e)
			{
				System.out.println("滑鼠按下事件");
			}
			public void mouseExited(MouseEvent e)
			{
				System.out.println("滑鼠移出按鈕區域事件");
			}
			public void mouseEntered(MouseEvent e)
			{
				System.out.println("滑鼠進入按鈕區域事件");
			}
			public void mouseClicked(MouseEvent e)
			{
				System.out.println("滑鼠完成點選事件");
			}
		});
	}
}
/*
 * 還有滑鼠左擊事件,右擊事件,中擊事件等。
 */
public class Main
{
	static public void main(String[] args)
	{
		Frame f=new Frame("KeyEvent");
		f.setLayout(new FlowLayout());
		f.setSize(400,200);
		f.setLocation(300, 200); 
		TextField tf=new TextField(30);
		f.add(tf);
		f.setVisible(true);
		tf.addKeyListener(new KeyAdapter()
		{
			public void KeyPressed(KeyEvent e)
			{
				int KeyCode=e.getKeyCode();
				String s=KeyEvent.getKeyText(keyCode);
				System.out.print("輸入的內容為:"+s+",");
				System.out.println("對應的值為:"+"KeyCode");
			}
		});
	}
}