GUI
Graphical User Interface(圖形用戶接口)。
用圖形的方式,來顯示計算機操做的界面,這樣更方便更直觀。java
Java爲GUI提供的對象都存在java.Awt和javax.Swing兩個包中。ide
Awt與 Swing
java.Awt:Abstract Window ToolKit (抽象窗口工具包),須要調用本地系統方法實現功能。屬重量級控件。
javax.Swing:在AWT的基礎上,創建的一套圖形界面系統,其中提供了更多的組件,並且徹底由Java實現。加強了移植性,屬輕量級控件工具
。佈局
常見的佈局管理器:
FlowLayout(流式佈局管理器)
從左到右的順序排列。
Panel默認的佈局管理器。
BorderLayout(邊界佈局管理器)
東,南,西,北,中
Frame默認的佈局管理器。
GridLayout(網格佈局管理器)
規則的矩陣
CardLayout(卡片佈局管理器)
選項卡
GridBagLayout(網格包佈局管理器)
非規則的矩陣字體
簡單的窗體建立過程: Frame f = new Frame(「my window」); f.setLayout(new FlowLayout()); //設置佈局管理器 f.setSize(500,400); //設置窗體大小 f.setLocation(300,200); //設置窗體出如今屏幕的位置 f.setIconImage(Toolkit.getDefaultToolkit().createImage("qq.png")); Button button = new Button("我是一個按鈕"); f.add(button); f.setVisible(true);
事件監聽機制ui
肯定事件源(容器或組件)
經過事件源對象的addXXXListener()方法將偵聽器註冊到該事件源上。
該方法中接收XXXListener的子類對象,或者XXXListener的子類XXXAdapter的子類對象。
通常用匿名內部類來表示。
在覆蓋方法的時候,方法的參數通常是XXXEvent類型的變量接收。
事件觸發後會把事件打包成對象傳遞給該變量。(其中包括事件源對象。經過getSource()或者,getComponent()獲取。)this
窗口事件關閉窗體spa
Frame f = new Frame("個人窗體"); //事件源是窗體,把監聽器註冊到事件源上 //事件對象傳遞給監聽器 f.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { //退出虛擬機,關閉窗口 System.exit(0); } });
窗口中按鈕關閉窗體orm
Button button = new Button("我是按鈕"); //事件源是按鈕,給按鈕註冊監聽器 button2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.exit(0); } });
鼠標監聽器(MouseListener)對象
void mouseClicked(MouseEvent e)
鼠標按鍵在組件上單擊(按下並釋放)時調用。
void mouseEntered(MouseEvent e)
鼠標進入到組件上時調用。
void mouseExited(MouseEvent e)
鼠標離開組件時調用。
void mousePressed(MouseEvent e)
鼠標按鍵在組件上按下時調用。
void mouseReleased(MouseEvent e)
鼠標按鈕在組件上釋放時調用。
簡單的記事本程序
import java.awt.FileDialog; import java.awt.Font; import java.awt.Frame; import java.awt.Menu; import java.awt.MenuBar; import java.awt.MenuItem; import java.awt.TextArea; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileWriter; import java.io.IOException; public class Notepad extends Frame{ //簡單的記事本程序 private static final long serialVersionUID = 1L; private MenuItem open; private MenuItem save; private MenuItem quit; private TextArea ta; public Notepad() { init(); menubar(); textArea(); event(); } private void init() { this.setSize(300, 400); //設置窗體大小 this.setLocation(500, 100); //設置窗體的位置 this.setVisible(true); //設置窗體顯示 } private void menubar() { MenuBar mb = new MenuBar(); //建立菜單欄 Menu file = new Menu("文件"); //建立菜單 open = new MenuItem("打開"); save = new MenuItem("保存"); quit = new MenuItem("退出"); this.setMenuBar(mb); //將菜單欄添加到窗體上 mb.add(file); //將菜單添加到菜單欄上 file.add(open); //將菜單項添加到菜單上 file.add(save); file.add(quit); } private void textArea() { ta = new TextArea(); ta.setFont(new Font("Courier New", Font.PLAIN, 20)); //設置字體(字體名字,字體樣式,字體大小) this.add(ta); //將文本區域添加到窗體上 } private void event() { this.addWindowListener(new WindowAdapter() { //關閉窗體 @Override public void windowClosing(WindowEvent e) { System.exit(0); } }); open.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { open(); } }); save.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { save(); } }); quit.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { System.exit(0); } }); ta.addKeyListener(new KeyAdapter() { @Override public void keyReleased(KeyEvent e) { //鍵盤釋放 if(e.getKeyCode() == KeyEvent.VK_O && e.isControlDown()) open(); } }); ta.addKeyListener(new KeyAdapter() { @Override public void keyReleased(KeyEvent e) { if(e.getKeyCode() == KeyEvent.VK_S && e.isControlDown()) save(); } }); } private void open() { FileDialog fd = new FileDialog(this, "打開", FileDialog.LOAD); //建立對話框對象,並指定模式 fd.setVisible(true); //顯示對話框 String dir = fd.getDirectory(); //獲取對話框的目錄 String file = fd.getFile(); //獲取對話框的文件名 if(dir != null && file != null){ loadFile(new File(dir,file)); //讀取文件 } } private void loadFile(File file) { try ( FileInputStream fis = new FileInputStream(file); //讀文件的讀取流 ByteArrayOutputStream baos = new ByteArrayOutputStream(); ){ int len; byte[] arr = new byte[1024]; while((len = fis.read(arr))!= -1) { baos.write(arr, 0, len); } byte[] arr2 = baos.toByteArray(); String message = new String(arr2); ta.setText(message); } catch (IOException e) { e.printStackTrace(); } } private void save() { FileDialog fd = new FileDialog(this, "保存", FileDialog.SAVE); fd.setVisible(true); String dir = fd.getDirectory(); String file = fd.getFile(); if(dir != null && file != null) { saveFile(new File(dir,file)); } } private void saveFile(File file) { try( FileWriter fw = new FileWriter(file); //將文本的內容寫到指定的文件上 ){ String message = ta.getText(); //獲取文本區域中的內容 fw.write(message); //將文本區域的內容寫出去 }catch(IOException e) { } } public static void main(String[] args) { new Notepad(); } }