6.1 Java圖形用戶界面初步
幾種界面技術
AWT:重量級組件,依賴作業系統
SWING:輕量級組件,不依賴作業系統
SWT
6.1.1Java圖形用戶界面概述
圖形用戶概念:
Java的AWT和SWING
java.awt,java.swing
6.1.2AWT組件概述
java.awt包中主要類及組件類的繼承關系

AWT類包
(1)基本視窗類:Frame,Dialog
(2)基本GUI組件類:Button,Label,TextField,TextArea,Checkbox
(3)基本事件介面:ActionListener...
(4)界面布局控制類
(5)基本繪圖支持類
組件:基本,核心元素
容器:視窗(window)(可獨立存在)和面板(panel)(不可獨立存在)
視窗:框架(Frame),對話框(Dialog)
非容器:
創建視窗和基本組件:
import java.awt.*;
public class TestGUI extends Frame{//通過繼承Frame子類創建表單
public TestGUI()
{
this.setTitle("用戶登錄");//設定標題
this.setBackground(Color.red);//設定背景顏色
this.setLocation(300,300);//設定位置
this.setSize(400,600);//設定大小
this.setVisible(true);//設定可見
//添加組件
//設定布局管理,如沒有布局管理,組件會被覆寫
this.setLayout(new FlowLayout());//流式布局管理,從上到下依次排列
//Label
Label l1=new Label("用戶名");//Lable組件:顯示一行文本
this.add(l1);
//TextField
TextField tx=new TextField(10);//可輸入文本十行
this.add(tx);
Label l2=new Label("密碼");
this.add(l2);
TextField tx2=new TextField(10);//可輸入文本十行
this.add(tx2);
//Button
Button b1=new Button("登錄");//按鈕
this.add(b1);
Button b2=new Button("退出");
this.add(b2);
}
public static void main(String[] args) {
new TestGUI();
}
}
6.2事件處理
6.2.1AWT的委托事件模型
事件:event,一個狀態的改變或者活動的發生
事件類:視窗事件類,單擊事件類等
事件源:產生事件的組件
事件處理者:處理回應事件的物件,他是一個類,若處理某類事件,需該類事件的介面
事件監聽器,委托事件
import java.awt.*;
import java.awt.event.*;
class ButtonHandler implements ActionListener//定義監聽器,實作點擊事件的介面
{
public void actionPerformed(ActionEvent e)//ActionListener的方法
{
Frame f=new Frame();
f.setBackground(Color.red);
f.setSize(300,300);
f.setLocation(400,400);
f.setVisible(true);
System.out.println("按鈕點擊將在這里進行處理");
}
}
class ExitHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
System.exit(-1);
System.out.println("按鈕點擊將在這里進行處理");
}
}
//實作表單關閉
class WindowHandler implements WindowListener
{
//所有方法都需實作
public void windowActivated(WindowEvent e) {}
public void windowClosed(WindowEvent e) {}
public void windowClosing(WindowEvent e)
{
System.exit(-1);
}
public void windowDeactivated(WindowEvent e) {}
public void windowDeiconified(WindowEvent e) {}
public void windowIconified(WindowEvent e) {}
public void windowOpened(WindowEvent e) {}
}
//WindowListener空方法太多
class WindowHandler2 extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
System.exit(-1);
}
}
public class TestGUI extends Frame{//通過繼承Frame子類創建表單
public TestGUI()
{
this.setTitle("用戶登錄");//設定標題
this.setBackground(Color.red);//設定背景顏色
this.setLocation(300,300);//設定位置
this.setSize(400,600);//設定大小
this.setVisible(true);//設定可見
//添加組件
//設定布局管理,如沒有布局管理,組件會被覆寫
this.setLayout(new FlowLayout());//流式布局管理,從上到下依次排列
//Label
Label l1=new Label("用戶名");//Lable組件:顯示一行文本
this.add(l1);
//TextField
TextField tx=new TextField(10);//可輸入文本十行
this.add(tx);
Label l2=new Label("密碼");
this.add(l2);
TextField tx2=new TextField(10);//可輸入文本十行
this.add(tx2);
//Button
Button b1=new Button("登錄");//按鈕
this.add(b1);
ButtonHandler bh1=new ButtonHandler();//監聽器
b1.addActionListener(bh1);//委托事件源
Button b2=new Button("退出");
this.add(b2);
ExitHandler bh2=new ExitHandler();//監聽器
b2.addActionListener(bh2);//委托事件源
WindowHandler wh=new WindowHandler();//視窗事件的監聽器
this.addWindowListener(wh);
WindowHandler2 wh2=new WindowHandler2();//視窗事件的監聽器
this.addWindowListener(wh2);
}
public static void main(String[] args) {
new TestGUI();
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/86130.html
標籤:Eclipse
