AWT
- AWT(Abstract Window Toolkit)包括了很多類的接口,用於Java Application的GUI(Graphics User Interface圖形用戶界面)編程。
- GUI的各種元素(如:窗口,按鈕,文本框)由Java類實現。
- 使用AWT所涉及的類一般在 java.awt 包及其子包中。
- Container 和 Component 是AWT中的兩個和心態。
Component & Container
- Java的圖形用戶界面的最基本組成部分是Component, Component類及其子類的對象用來描述以圖形化的方式顯示在屏幕上並能與用戶進行交互的GUI元素,例如一個按鈕,一個標簽等。
- 一般的Component對象不能獨立地顯示出來,必須將“放在”某一的Container對象中才可以顯示出來。
- Container是Component子類,Container子類對象可以“容納”別的Component對象。
- Container對象可以使用方法 add(...) 向其中添加其他Component對象。
- Container是Component的子類,因此Container對象也可以被當作Component對象添加到其他Container對象中。
兩種常用的Container:
- Window:其對象表示自由停泊的頂級窗口。
- Panel:其對象可作為容納其他Component對象,但不能獨立存在,必須添加到其他Container中(如Window 或 Applet)。
Frame
- Frame是Window的子類,由Frame或其子類創建的對象為一個窗體。
- Frame的常用構造方法:
Frame() Frame(String s) //創建標題欄為字符串s的窗口
示例1:
import java.awt.*; public class test{ public static void main( String args[]) { Frame f = new Frame("My First Test"); f.setLocation(300, 300); f.setSize( 170,100); f.setBackground( Color.blue); f.setResizable(false); f.setVisible( true); } }
示例2:
import java.awt.*; public class test { public static void main(String args[]) { MyFrame f1 = new MyFrame(100,100,200,200,Color.BLUE); MyFrame f2 = new MyFrame(300,100,200,200,Color.YELLOW); MyFrame f3 = new MyFrame(100,300,200,200,Color.GREEN); MyFrame f4 = new MyFrame(300,300,200,200,Color.MAGENTA); } } class MyFrame extends Frame{ static int id = 0; MyFrame(int x,int y,int w,int h,Color color){ super("MyFrame " + (++id)); setBackground(color); setLayout(null); setBounds(x,y,w,h); setVisible(true); } }
Panel
- Panel對象可以看成可以容納Component的空間。
- Panel對象可以擁有自己的布局管理器。
- Panel類擁有從父類繼承來的
示例1:
import java.awt.*; public class TestPanel { public static void main(String args[]) { Frame f = new Frame("Java Frame with Panel"); Panel p = new Panel(null); f.setLayout(null); f.setBounds(300,300,500,500); f.setBackground(new Color(0,0,102)); p.setBounds(50,50,400,400); p.setBackground(new Color(204,204,255)); f.add(p); f.setVisible(true); } }
示例2:
import java.awt.*; public class test{ public static void main( String args[]) { new MyFrame2("MyFrameWithPanel",300,300,400,300); } } class MyFrame2 extends Frame{ private Panel p1,p2,p3,p4; MyFrame2(String s,int x,int y,int w,int h){ super(s); setLayout(null); p1 = new Panel(null); p2 = new Panel(null); p3 = new Panel(null); p4 = new Panel(null); p1.setBounds(0,0,w/2,h/2); p2.setBounds(0,h/2,w/2,h/2); p3.setBounds(w/2,0,w/2,h/2); p4.setBounds(w/2,h/2,w/2,h/2); p1.setBackground(Color.BLUE); p2.setBackground(Color.GREEN); p3.setBackground(Color.YELLOW); p4.setBackground(Color.MAGENTA); add(p1);add(p2);add(p3);add(p4); setBounds(x,y,w,h); setVisible(true); } }
事件監聽
Button事件監聽
示例:
import java.awt.*; import java.awt.event.*; public class test { public static void main(String args[]) { Frame f = new Frame("Test"); Button b = new Button("Press Me!"); Monitor bh = new Monitor(); b.addActionListener(bh); f.add(b,BorderLayout.CENTER); f.pack(); //調整此窗口大小,以適應其組件的首先大小和布局 f.setVisible(true); } } class Monitor implements ActionListener { public void actionPerformed(ActionEvent e) { System.out.println("a button has been pressed"); } }
TextField類
- java.awt.TextFieldl類用來創建文本框對象。
- TextField有如下常用方法:
TextField事件監聽
- TextField對象可能發生Action(光標在文本框內敲回車)事件。與該事件對應的事件類是 java.awt.event.ActionListener接口的類的對象。ActionListener接口定義有方法: public void actionPerformed(ActionEvent e)
- 實現該接口的類主要在該方法中添加處理事件(Action)的語句。
- 使用 addActionListener(ActionListener I) 方法為 TextField 對象注冊一個 ActionListener 對象,當 TextField 對象發生 Action 時,會生成一個 ActionEvent 對象,該對象作為參數傳遞給 ActionListener 對象的 actionPerformer 方法在方法中可以獲取該對象的信息,並做相應的處理。
import java.awt.*; import java.awt.event.*; public class TFPassword { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub new TFFrame2(); } } class TFFrame2 extends Frame { TFFrame2() { TextField tf = new TextField(); add(tf); tf.addActionListener(new TFActionListener2()); tf.setEchoChar('*'); pack(); setVisible(true); } } class TFActionListener2 implements ActionListener { public void actionPerformed(ActionEvent e) { TextField tf = (TextField)e.getSource(); System.out.println(tf.getText()); tf.setText(""); } }
內部類
好處:
- 可以方便的訪問包裝類的成員。
- 可以更清楚的組織邏輯,防止不應該被其他類訪問的類進行訪問。
何時使用:
- 該類不允許或不需要其他類訪問時。
Graphics類 Paint方法
- 每個 Component 都有一個 paint(Graphics g)用於實現繪圖目的,每次重畫該 Component 時都自動調用 paint 方法。
- Graphics 類中提供了許多繪圖方法,具體查詢API。
示例:
import java.awt.*; public class TestPaint { public static void main(String[] args) { new PaintFrame().launchFrame(); } } class PaintFrame extends Frame { public void launchFrame() { setBounds(200,200,640,480); setVisible(true); } public void paint(Graphics g) { Color c = g.getColor(); g.setColor(Color.red); g.fillOval(50, 50, 30, 30); g.setColor(Color.green); g.fillRect(80,80,40,40); g.setColor(c); } }
鼠標事件適配器
- 抽象類 java.awt.event.MouseAdapter 實現了 MouseListener 接口,可以使用其子類作為 MouseEvent接口,可以使用其子類作為 MouseEvent 的監聽器,只要重寫其相應的方法即可。
- 對於其他的監聽器,也有對應的適配器。
- 使用適配器可以避免監聽器類定義沒有必要的空方法。
- GUI/MyMouseAdapter.java 鼠標適配器
- repaint-update()-paint();
Window事件
- Window事件所對應的事件類為WindowEvent,所對應的事件監聽接口為WindowListener。
- WindowListener定義的方法有:
- 與WindowListener對應的適配器為 WindowAdapter。