Graphical User Interface(圖形用戶接口)。用圖形的方式,來顯示計算機操作的界面,這樣更方便更直觀。
Command line User Interface (命令行用戶接口)就是常見的Dos命令行操作。需要記憶一些常用的命令,操作不直觀。Java爲GUI提供的對象都存在java.Awt和javax.Swing兩個包中。
java.Awt:Abstract Window ToolKit (抽象窗口工具包),需要調用本地系統方法實現功能。屬重量級控件。javax.Swing:在AWT的基礎上,建立的一套圖形界面系統,其中提供了更多的組件,而且完全由Java實現。增強了移植性,屬輕量級控件。還有一種是IBM公司開發的Swt工具包
常見的佈局管理器:
FlowLayout(流式佈局管理器)從左到右的順序排列。Panel默認的佈局管理器。BorderLayout(邊界佈局管理器)東,南,西,北,中Frame默認的佈局管理器。GridLayout(網格佈局管理器)規則的矩陣CardLayout(卡片佈局管理器)選項卡GridBagLayout(網格包佈局管理器)非規則的矩陣座標式佈局管理器
創建圖形化界面的步驟:1,創建frame窗體。2,對窗體進行基本設置。比如大小,位置,佈局。3,定義組件。4,將組件通過窗體的add方法添加到窗體中。5,讓窗體顯示,通過setVisible(true)
import java.awt.*;
class AwtDemo
{
public static void main(String[] args)
{
//創建Frame窗體
Frame f = new Frame("我的窗體");
//對窗體進行基本設置:大小,位置,佈局等
f.setSize(500,400);
f.setLocation(400,400);
f.setLayout(new FlowLayout());
//**********************************
Button b = new Button("aniu");
f.add(b);
//**********************************
//讓窗體顯示
f.setVisible(true);
}
}
事件監聽機制
1,事件源(組件)2,事件(Event)3,監聽器(Listener)4,事件處理(引發事件後處理方式)
事件:每一個事件源都有自己特有的對應事件和共性事件。
監聽器:將可以觸發某一個事件的動作(不只一個動作)都已經封裝到了監聽器中。
人身體的條件反射可以很好的理解事件監聽機制。我們的大腦就是一個監聽器,時刻監聽着身體的每一部分,這時如果有錘子敲擊(事件源)膝蓋時,膝蓋部位的神經將會通過神經中樞向大腦傳遞信號(事件對象),大腦也會對此作出相應的迴應,踢腿(事件處理)
處理過程:首先我們需要定義一個監聽器來監聽動作,但是我們並沒有在Fram類中找到調用事件監聽器相關的方法,到Fram類的父類Window類中找,找到addWindowListener(WindowListener l )方法,該方法中需要傳入一個WindowListener,這是個接口,使用裏面的方法需要全部複寫,發現其實現子類WindowApdater類中已經把這些方法複寫了,所以在傳遞WindowListener時,可以使用 匿名內部類簡化書寫,並複寫WindowAdapter中使用的方法即可
/**
創建窗口
設置監聽器
*/
import java.awt.*;
import java.awt.event.*;
class CloseDemo
{
public static void main(String[] args)
{
Frame f = new Frame("演示窗口");
f.setSize(500,400);
f.setLocation(500,200);
f.setLayout(new FlowLayout());
Button b = new Button("關閉按鈕");
f.add(b);
//添加窗體監聽,因爲時間源是窗體
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
f.setVisible(true);
}
}
1,確定事件源(容器或組件)2,通過事件源對象的addXXXListener()方法將偵聽器註冊到該事件源上。3,該方法中接收XXXListener的子類對象,或者XXXListener的子類XXXAdapter的子類對象。4,一般用匿名內部類來表示。5,在覆蓋方法的時候,方法的參數一般是XXXEvent類型的變量接收。事件觸發後會把事件打包成對象傳遞給該變量。(其中包括事件源對象。通過getSource()或者,getComponent()獲取。)
1,窗體事件addWindowListener(WindowEvent e ) 適配器: WindowAdapter2,Action事件(按鈕事件)addActionListener(WindowEvent e) 沒有適配器3,鼠標事件addMouseListener(MouseEvent e ) 適配器:MouseAdapter4,鍵盤事件addKeyListener(KeyEvent e ) 適配器:KeyAdapter
監聽器都是接口,如果此接口中的方法超過3個,那麼這個監聽器肯定有與其對應的適配器,適配器中已經將監聽器中的所有方法全部複寫了,所以通常情況下不是實現監聽器來處理事件,而是使用匿名內部類 來創建適配器的對象。處理事件的功能通常在對應的Event事件對象中
/**
創建窗口
設置監聽器
*/
import java.awt.*;
import java.awt.event.*;
class CloseDemo
{
public static void main(String[] args)
{
Frame f = new Frame("演示窗口");
f.setSize(500,400);
f.setLocation(500,200);
f.setLayout(new FlowLayout());
Button b = new Button("關閉按鈕");
Button b1 = new Button("操作按鈕");
Button b2 = new Button("獲取鍵盤");
f.add(b);
f.add(b1);
f.add(b2);
//添加窗體監聽,因爲時間源是窗體
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
//按鈕監聽事件
b.addActionListener(new ActionListener()
{
public void actionPerformed (ActionEvent e)
{
System.exit(0);
}
});
//鼠標監聽事件
b1.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
System.out.println(e.getClickCount());
}
});
//鍵盤監聽事件
b2.addKeyListener(new KeyAdapter()
{
public void keyPressed(KeyEvent e)
{
System.out.println(e.getKeyChar());
}
});
f.setVisible(true);
}
}
對話框
先創建菜單條,再創建菜單,每一個菜單中建立菜單項。也可以菜單添加到菜單中,作爲子菜單。通過setMenuBar()方法,將菜單添加到Frame中。
/**
記事本
*/
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class TxtDemo
{
private Frame f;
private MenuBar bar;
private TextArea ta;
private Menu fileMenu;
private MenuItem openItem,saveItem,closeItem;
private FileDialog openDia,saveDia;
private File file;
TxtDemo()
{
init();
}
public void init()
{
f = new Frame("my window");
f.setBounds(300,100,650,600);
bar = new MenuBar();
ta = new TextArea();
fileMenu = new Menu("文件");
openItem = new MenuItem("打開");
saveItem = new MenuItem("保存");
closeItem = new MenuItem("退出");
fileMenu.add(openItem);
fileMenu.add(saveItem);
fileMenu.add(closeItem);
bar.add(fileMenu);
f.setMenuBar(bar);
openDia = new FileDialog(f,"打開",FileDialog.LOAD);
saveDia = new FileDialog(f,"保存",FileDialog.SAVE);
f.add(ta);
myEvent();
f.setVisible(true);
}
private void myEvent()
{
saveItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(file==null)
{
saveDia.setVisible(true);
String dirPath = saveDia.getDirectory();
String fileName = saveDia.getFile();
if(dirPath==null || fileName==null)
return ;
file = new File(dirPath,fileName);
}
try
{
BufferedWriter bufw = new BufferedWriter(new FileWriter(file));
String text = ta.getText();
bufw.write(text);
bufw.close();
}
catch (IOException ex)
{
throw new RuntimeException();
}
}
});
openItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
openDia.setVisible(true);
String dirPath = openDia.getDirectory();
String fileName = openDia.getFile();
if(dirPath==null || fileName==null)
return ;
ta.setText("");
file = new File(dirPath,fileName);
try
{
BufferedReader bufr = new BufferedReader(new FileReader(file));
String line = null;
while((line=bufr.readLine())!=null)
{
ta.append(line+"\r\n");
}
bufr.close();
}
catch (IOException ex)
{
throw new RuntimeException("讀取失敗");
}
}
});
closeItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
});
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
public static void main(String[] args)
{
new TxtDemo();
}
}
1,將多個類封裝到了一個包(package)中。2,定義一個jar包的配置信息。定義一個文件a.txt 。文件內容內容爲:Main-Class:(空格)包名.類名(回車)3,打jar包。jar -cvfm my.jar a.txt 包名4,通過winrar程序進行驗證,查看該jar的配置文件中是否有自定義的配置信息。5,通過工具--文件夾選項--文件類型--jar類型文件,通過高級,定義該jar類型文件的打開動作的關聯程序。jdk\bin\javaw.exe -jar6,雙擊