AWT(Abstract Windows Toolkit),抽象窗口工具包,SUN公司提供的用于圖形界面編程(GUI)的類庫(kù)?;镜腁WT庫(kù)處理用戶界面元素的方法是把這些元素的創(chuàng)建和行為委托給每個(gè)目標(biāo)平臺(tái)上(Windows、Unix、Macintosh等)的本地GUI工具進(jìn)行處理。例如:如果我們使用AWT在一個(gè)Java窗口放置一個(gè)按鈕,那么實(shí)際上是用的是一個(gè)具有本地外觀和感覺(jué)的按鈕。這樣,從理論上來(lái)說(shuō),我們說(shuō)編寫的圖形界面程序能運(yùn)行在任何平臺(tái)上,做到了圖形界面程序的跨平臺(tái)運(yùn)行
Component是Java中所有圖形界面組件的一個(gè)基類
Container是Component的子類,一個(gè)容器(Container)是可以容納其他組件的組件
Window繼承了Container,A Window object is a top-level window with no borders and no menubar.
Frame繼承自Window,A Frame is a top-level window with a title and a border。要產(chǎn)生一個(gè)圖形界面的程序,首先就要產(chǎn)生一個(gè)框架窗口
布局管理器
容器里組件的位置和大小是由布局管理器來(lái)決定的。容器對(duì)布局管理器的特定實(shí)例保持一個(gè)引用。當(dāng)容器需要定位一個(gè)組件時(shí),它將調(diào)用布局管理器來(lái)完成。當(dāng)決定一個(gè)組件的大小時(shí),也是如此
在AWT中,給我們提供了五種布局管理器:BorderLayout(缺省)、FlowLayout、GridLayout、CardLayout、GridBagLauout
AWT事件模型
Events(事件):描述發(fā)生了什么的對(duì)象
Event source(事件源):事件的產(chǎn)生器
Event handlers(事件處理器):接收事件對(duì)象、解釋事件對(duì)象并處理用戶交互的方法
委托模型:事件監(jiān)聽(tīng)器是實(shí)現(xiàn)了監(jiān)聽(tīng)器接口(java.awt.event中的WindowListenter)的類。一個(gè)監(jiān)聽(tīng)器對(duì)象是一個(gè)實(shí)現(xiàn)了專門的監(jiān)聽(tīng)器接口的類的實(shí)例
import java.awt.*;
import java.awt.event.*;
public class MyFrame
{
public static void main(String[] args)
{
Frame f=new Frame("llilac");//Frame(String title):Constructs a new, initially invisible Frame object with the specified title.
f.setSize(600,400);//public void setSize(int width,int height):Resizes this component so that it has width width and height height.
f.setLocation(100,100);//左上角是計(jì)算機(jī)屏幕的原點(diǎn)public void setLocation(int x,int y):Moves this component to a new location. The top-left corner of the new location is specified by the x and y parameters in the coordinate space of this component‘s parent.
f.setBackground(Color.GREEN);//public void setBackground(Color c):Sets the background color of this component.
//f.setLayout(new BorderLayout(10,10));//缺省布局管理器BorderLayout
//f.setLayout(new FlowLayout(FlowLayout.LEFT));
f.setLayout(new GridLayout(3,2,10,10));//3行2列,垂直水平間隙為10
Button btn1=new Button("hi");//Button(String label):Constructs a Button with the specified label.
Button btn2=new Button("hello");
Button btn3=new Button("Nice");
Button btn4=new Button("to");
Button btn5=new Button("metting");
f.add(btn1,"North");//void add(Component comp, Object constraints):Adds the specified component to the end of this container.
f.add(btn2,"South");
f.add(btn3,"West");
f.add(btn4,"East");
f.add(btn5,"Center");//要在f.show()前面
//f.addWindowListener(new MyWindowListener());//注冊(cè)一個(gè)事件監(jiān)聽(tīng)器
//f.addWindowListener(new YouWindowListener());
f.addWindowListener(new WindowAdapter()//用匿名的內(nèi)部類,因?yàn)閃indowAdapter是抽象類,所以加上{}進(jìn)行實(shí)現(xiàn)。采用此種方法時(shí),不再需要MyWindowListener類和YouWindowListener類
{
public void windowClosing(WindowEvent e) //Invoked when the user attempts to close the window from the window‘s system menu.
{
System.exit(0);
}
});
f.show();//繼承自Window,public void show():Makes the Window visible. If the Window and/or its owner are not yet displayable, both are made displayable. The Window will be validated prior to being made visible. If the Window is already visible, this will bring the Window to the front.還有一個(gè)方法,是繼承自Component,public void setVisible(boolean b):Shows or hides this component depending on the value of parameter b. 但這個(gè)方法只能讓窗口可見(jiàn),不能讓窗口到最前面
}
}
class MyWindowListener implements WindowListener//定義了一個(gè)事件監(jiān)聽(tīng)器類
{
public void windowActivated(WindowEvent e)
{
}
public void windowClosed(WindowEvent e)//用來(lái)對(duì)窗口已經(jīng)關(guān)閉之后的事件進(jìn)行響應(yīng)Invoked when a window has been closed as the result of calling dispose on the window.
{
}
public void windowClosing(WindowEvent e) //Invoked when the user attempts to close the window from the window‘s system menu.
{
System.exit(0);
}
public void windowDeactivated(WindowEvent e)
{
}
public void windowDeiconified(WindowEvent e)
{
}
public void windowIconified(WindowEvent e)
{
}
public void windowOpened(WindowEvent e)
{
}
}//如果用的是實(shí)現(xiàn)接口的話,就要實(shí)現(xiàn)接口中所有的類。這樣很麻煩。于是Java提供了一種類叫適配器類,接口WindowListener對(duì)應(yīng)的適配器類是WindowAdapter,它實(shí)現(xiàn)了WindowListener的所有方法,但都是空實(shí)現(xiàn)。
class YouWindowListener extends WindowAdapter//Java把適配器類WindowAdapter聲明為abstract,因?yàn)樗鼘?duì)接口方法的實(shí)現(xiàn)都是空實(shí)現(xiàn),所以直接實(shí)例化適配器的一個(gè)類是沒(méi)有意義的,所以Java把他聲明為了abstract,以防用戶直接實(shí)例化。我們只要直接從適配器類WindowAdapter派生一個(gè)子類就行了
{
public void windowClosing(WindowEvent e) //Invoked when the user attempts to close the window from the window‘s system menu.
{
System.exit(0);
}
}
-----------------------------------------------------------------------------
//實(shí)現(xiàn)Win記事本部分功能
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class HisFrame//創(chuàng)建菜單:菜單欄(MenuBar類)-菜單(Menu類)-菜單項(xiàng)(MenuItem類)
{
public static void main(String[] args)
{
final Frame f=new Frame("
http://llilac");
f.setSize(600,400);
f.setLocation(100,100);
/*構(gòu)造文本域,只能存放單行文本
TextField tf=new TextField(20);
f.add(tf,"North");
*/
final TextArea tf=new TextArea();
f.add(tf);
f.addWindowListener(new WindowAdapter()//窗口監(jiān)聽(tīng)器
{
public void sindowClosing(WindowEvent e)
{
System.exit(0);
}
});
MenuBar mb=new MenuBar();
Menu m1=new Menu("File");
Menu m2=new Menu("Edit");
MenuItem mi1=new MenuItem("New");
MenuItem mi2=new MenuItem("open");
mi2.addActionListener(new ActionListener()//打開(kāi)文件功能
{
public void actionPerformed(ActionEvent e)
{
FileDialog fd=new FileDialog(f,"Open File Dialog",FileDialog.LOAD);//public FileDialog(Frame parent,String title,int mode):parent - the owner of the dialog;title - the title of the dialog;mode - the mode of the dialog; either FileDialog.LOAD or FileDialog.SAVE
fd.show();
/*把文件的內(nèi)容讀進(jìn)來(lái),此時(shí)只能讀入當(dāng)前目錄下的文件,因?yàn)間etFile()知識(shí)返回文件名,構(gòu)建輸入流后它只在當(dāng)前目錄下查找,所以要想讀取其它目錄的文件,要給出路經(jīng)
String strFile=fd.getFile();
if(strFile!=null)
{
try
{
FileInputStream fis=new FileInputStream(strFile);
byte[] buf=new byte[10*1024];
int len=fis.read(buf);
tf.append(new String(buf,0,len));
fis.close();
}
catch (Exception ex)
{ex.printStackTrace();}
}
*/
//把文件的內(nèi)容讀進(jìn)來(lái),此時(shí)可以讀取其它目錄的文件,因?yàn)榻o出了路經(jīng)
String strFile=fd.getDirectory()+fd.getFile();//獲取的是完整的路徑名
if(strFile!=null)
{
try
{
FileInputStream fis=new FileInputStream(strFile);
byte[] buf=new byte[10*1024];
int len=fis.read(buf);
tf.append(new String(buf,0,len));
fis.close();
}
catch (Exception ex)
{ex.printStackTrace();}
}
}
});
MenuItem mi3=new MenuItem("save");
MenuItem mi4=new MenuItem("exit");
mi4.addActionListener(new ActionListener()//退出功能
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
});
MenuItem mi5=new MenuItem("copy");
MenuItem mi6=new MenuItem("paste");
m1.add(mi1);
m1.add(mi2);
m1.add(mi3);
m1.add(mi4);
m2.add(mi5);
m2.add(mi6);
mb.add(m1);
mb.add(m2);
f.setMenuBar(mb);
f.show();
}
}
--------------------------------------------------------------------------------
java.awt包中還有一些表較常用的組件,比如choice—下拉列表框;Checkbox—復(fù)選框;Label—標(biāo)簽
Java基礎(chǔ)類
JFC(Java foundation Classes):Java基礎(chǔ)類,是關(guān)于GUI組件和服務(wù)的完整集合,主要包括5個(gè)API:AWT、Java2D、Accessiblility、Drag&Drop、Swing。JFC提供了幫助開(kāi)發(fā)人員設(shè)計(jì)復(fù)雜應(yīng)用程序的一整套應(yīng)用程序開(kāi)發(fā)包
Java2D是一圖形API,它為Java應(yīng)用程序提供了一套高級(jí)的有關(guān)二維(2D)圖形圖像處理的類,Java2D API擴(kuò)展了java.awt和java.awt.image類,并提供了豐富的繪圖風(fēng)格,定義了復(fù)雜圖形的機(jī)制和精心調(diào)節(jié)繪制過(guò)程的方法和類。這些API使得獨(dú)立于平臺(tái)的圖形應(yīng)用程序的開(kāi)發(fā)更加簡(jiǎn)便
Accessiblility API提供了一套高級(jí)工具,用以輔助開(kāi)發(fā)使用非傳統(tǒng)輸入和輸出的應(yīng)用程序。它提供了一個(gè)輔助的技術(shù)接口,如:屏幕閱讀器,屏幕放大器,聽(tīng)覺(jué)文本閱讀器(語(yǔ)音處理)等等
Drag&Drop技術(shù)提供了Java和本地應(yīng)用程序之間的互操作性,用來(lái)在Java應(yīng)用程序和不支持Java技術(shù)的應(yīng)用程序之間交換數(shù)據(jù)
JFC模塊的重點(diǎn)在Swing。Swing用來(lái)進(jìn)行基于窗口的應(yīng)用程序開(kāi)發(fā),它提供了一套豐富的組件和工作框架,以指定GUI如何獨(dú)立于平臺(tái)地展現(xiàn)其視覺(jué)效果
javax.Swing中的組件都是以JComponent這個(gè)類為基類的
import javax.swing.*;
public class SwingTest
{
public static void main(String[] args)
{
JFrame jf=new JFrame("llilac");
jf.setSize(600,400);
jf.setLocation(100,100);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//public static final int EXIT_ON_CLOSE:The exit application default window close operation.
JButton btn=new JButton("l");
jf.getContentPane().add(btn,"West");//首先獲取一個(gè)內(nèi)容面板,然后將組件增加到內(nèi)容面板上
jf.show();
}