JAVA基礎-GUI
Java也提供圖像化編程
圖形化
GUI(圖形用戶界面)
GUI
1 Graphical User Interface(圖形用戶接口)
2 用圖形的方式,來顯示計算機操作的界面,這樣更方便更直觀
CLI
1 Command line User Interface (命令行用戶接口)
2 就是常見的Dos命令行操作
3 需要記憶一些常用的命令,操作不直觀
Java為GUI提供的對象都存在java.Awt和javax.Swing兩個包中
Awt和Swing
java.Awt:Abstract Window ToolKit(抽象窗口 工具包),需要調用本地系統方法實現功能。屬重量級控件
javax.Swing:在AWT的基礎上,建立的一套圖形界面系統,其中提供了更多的組件,而且完全由Java實現。增強了移植性,屬
輕量級控件
繼承關系圖
Container:為容器,是一個特殊的組件,該組件中可以通過add方法添加其他組件進來
布局管理器
容器中的組件的排放方式,就是布局
常見的布局管理器:
FlowLayout(流式布局管理器)從左到右的順序排列Panel默認的布局管理器
BorderLayout(邊界布局管理器)東,南,西,北,中Frame默認的布局管理器
GridLayout(網格布局管理器)規則的矩陣
CardLayout(卡片布局管理器)選項卡
GridBagLayout(網格包布局管理器)非規則的矩陣
建立一個簡單的窗體
Container常用子類:Window Panel(面板,不能單獨存在)
Window常用子類:Frame Dialog
簡單的窗體創建過程:
Frame f = new Frame('my window');f.setLayout(new FlowLayout());f.setSize(500,400);//設置窗體大小f.setLocation(300,200);//設置窗體出現在屏幕的位置f.setVisible(true); //設置窗口可見性
事件監聽
事件監聽機制組成
事件源(組件):就是awt包或者swing包中的那些圖形界面組件
事件(Event):每一個事件源都有自己特有的對應事件和共性事件
監聽器(Listener):將可以觸發某一個事件的動作(不只一個動作)都已經封裝到了監聽器中
事件處理(引發事件后處理方式)
事件監聽機制流程圖
事件監聽機制
1 確定事件源(容器或組件)
2 通過事件源對象的addXXXListener()方法將偵聽器注冊到該事件源上
3 該方法中接收XXXListener的子類對象,或者XXXListener的子類XXXAdapter的子類對象
4 一般用匿名內部類來表示
5 在覆蓋方法的時候,方法的參數一般是XXXEvent類型的變量接收
6 事件觸發后會把事件打包成對象傳遞給該變量(其中包括事件源對象。通過getSource()或者getComponent()獲?。?/p>
import java.awt.*;import java.awt.event.*;import java.io.*;class Test {private Frame f;private TextField tf;private Button but;private TextArea ta;private Dialog d;private Label lab;private Button okBut;Test() {init();}public void init() {f = new Frame('my window');f.setBounds(300,100,600,500);f.setLayout(new FlowLayout());tf = new TextField(60);but = new Button('轉到');ta = new TextArea(25,70);d = new Dialog(f,'提示信息-self',true);d.setBounds(400,200,240,150);d.setLayout(new FlowLayout());lab = new Label();okBut = new Button('確定');d.add(lab);d.add(okBut);f.add(tf);f.add(but);f.add(ta);myEvent();f.setVisible(true);}private void myEvent() {okBut.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {d.setVisible(false);}});d.addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent e) {d.setVisible(false);}});tf.addKeyListener(new KeyAdapter() {public void keyPressed(KeyEvent e) {if(e.getKeyCode()==KeyEvent.VK_ENTER)showDir();}});but.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {showDir();}});f.addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent e) {System.exit(0);}});}private void showDir() {String dirPath = tf.getText();File dir = new File(dirPath);if(dir.exists() && dir.isDirectory()) {ta.setText('');String[] names = dir.list();for(String name : names) {ta.append(name+'rn');}} else {String info = '輸入信息錯誤,請重輸';lab.setText(info);d.setVisible(true);}}public static void main(String[] args) {new Test();}}
菜單
概述
MenuBar,Menu,MenuItem先創建菜單條,再創建菜單,每一個菜單 中建立菜單項也可以菜單添加到菜單中,作為子菜單通過setMenuBar()方法,將菜單添加到Frame中
菜單繼承體系
代碼示例
import java.awt.*;import java.awt.event.*;class Test {private Frame f;private MenuBar mb;private Menu m,subMenu;private MenuItem closeItem,subItem;Test(){init();} public void init(){f = new Frame('my window');f.setBounds(300,100,500,600);f.setLayout(new FlowLayout());mb = new MenuBar();m = new Menu('文件');subMenu = new Menu('子菜單');subItem = new MenuItem('子條目');closeItem = new MenuItem('退出');subMenu.add(subItem);m.add(subMenu);m.add(closeItem);mb.add(m);f.setMenuBar(mb);myEvent();f.setVisible(true);}private void myEvent() {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 Test();}}
可執行Jar包
1 將多個類封裝到了一個包(package)中。
2 定義一個jar包的配置信息。
3 定義一個文件a.txt,文件內容內容為:Main-Class:(空格)包名.類名(回車)
4 打jar包。jar -cvfm my.jar a.txt 包名
5 通過winrar程序進行驗證,查看該jar的配置文件中是否有自定義的配置信息
6 通過工具?文件夾選項?文件類型?jar類型文件,通過高級,定義該jar類型文件的打開動作的關聯程序jdkbinjavaw.exe -jar
package mymenu;import java.awt.*;import java.awt.event.*;import java.io.*;public class Test {private Frame f;private MenuBar bar;private TextArea ta;private Menu fileMenu;private MenuItem openItem,saveItem,closeItem;private FileDialog openDia,saveDia;private File file;Test() {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.flush();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+'rn');}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 Test();}}
以上就是JAVA基礎-GUI的詳細內容,更多關于JAVA GUI的資料請關注好吧啦網其它相關文章!
相關文章: