Java基础<十七>--->GUI(图形用户界面)

来源:互联网 发布:爱知日语和杉木 编辑:程序博客网 时间:2024/05/04 01:31

第一  GUI概述

一、概述:
1、GUI:Graphical User Interface,即图形用户界面,用于计算机与用户交互的一种方式
2、计算机与用户交互的形式:GUI和CLI
      GUI: Graphical User Interface,图形用户接口,用图形方式,来显示计算机操作界面,方便直观。
      CLI: Command Line User Interface,命令行用户接口,即常见的Dos命令行操作,须记住一些命令,不直观。
3、java也将这种界面封装为对象,其中的对象都放在了两个包中:java.Awt包和javax.Swing包。
java.Awt包:Abstract Window Toolkit,即抽象窗口工具箱。要调用本地系统方法实现功能,属重量级控件。
javax.Swing包:在AWT的基础上建立的一套图形界面系统,其中提供了更多的组件,且完全有java实现,增强了移植性,属轻量级控件。
二、Awt的继承体现


三、布局管理器:
1、布局:容器中的组件排列方式
2、常见的布局管理器:
1)FlowLayout:流式布局管理器,从左向右排列,是Panel默认的布局管理器
2)BorderLayout:边界式布局管理器,东南西北中的排列方式,是Frame的默认布局管理器
3)GridLayout:网格式布局管理器,规则的矩阵
4)CardLayout:卡片式布局管理器,即选项卡
5)GridBayLayout:网格包布局管理器,非规则矩阵
3、如果存在多种布局方式,如何创建窗体界面呢?步骤:
1)先将窗体Frame进行大区域的划分,设置其布局管理器,加入面板Panel
2)将组建加入Panel,设置面板的布局管理器
四、创建图形化界面思路:
1、创建Frame容器:Frame f = new Frame("my Frame");可设置标题
2、对窗体进行设置:如大小,位置,布局等。
      f.setSize(int wight,int hight);,设置宽高
      f.setLocation(int x,int y),设置横纵坐标;
      f.setBounds(int wight,int hight,int x,int y),者直接用这个对大小和位置设置
      f.setLayout(Layout layout),参数为指定的布局管理器,如FlowLayout。默认的是BorderLayout布局
3、定义组件:如Button b = new Button(“Button”),可设置组件的名称
4、将组件通过窗体的add方法添加到窗体中,f.add(b);
5、让窗体显示,通过setVisible(boolean b),通过设置参数是true还是false是否显示窗体
看实例:
public class AwtDemo {public static void main(String[] args) {Frame f = new Frame("my awt");// 开启另外一个线程,具备默认的布局,是边界布局f.setSize(500, 200);// 设置大小,宽、高f.setVisible(true);// 设置窗体可见f.setLocation(300, 200);// 设置本地位置,也就是窗体初始化位置f.setLayout(new FlowLayout());// 设置窗体布局为流式布局Button button = new Button("我是一个按钮");// 建立一个按钮f.add(button);// 将按钮和窗体相关联// 给窗体加事件监听器,只要监听器方法超过三个的,都有适配器,而Button的ActionListener就是少数没有适配器的监听器f.addWindowListener(new WindowAdapter() {@Overridepublic void windowOpened(WindowEvent e) {System.out.println(e);System.out.println("opened");}@Overridepublic void windowClosing(WindowEvent e) {System.out.println(e);System.out.println("closing");System.exit(0);}@Overridepublic void windowClosed(WindowEvent e) {System.out.println(e);System.out.println("closed");}@Overridepublic void windowActivated(WindowEvent e) {System.out.println(e);System.out.println("activated");}});}}


五、事件监听机制
(一)的特点:
1、事件源(组件):awt或swing包中的那些图形界面组件
2、事件(Event):每一个事件源都有自己特有的对应的时间和共性事件
3、监听器(Listener):将可触发某一事件的动作(不止一个),都封装到监听器中。注意,是动作封装到监听器中。
4、事件处理:引发事件后的处理方式。

(二)说明:
前三个在java中都已将定义好了,直接获取其对象来用即可,我们需要做的就是对产生的动作进行处理,步骤:
1、明确监听器(Frame)将监听器注册到上面,通过方法:addWindowListener(WindowListener w)
2、注意:若用子类实现WindowListener接口,就需要覆盖其中的7个方法,可只用到其中的关闭动作,其他动作未用到,就必须重写全部,因为WindowLister的子类WindowAdapter以实现此接口,并覆盖了其中所有方法,则只需继承WindowAdapter,覆盖需要的方法即可。
3、明确事件,并对事件进行处理,其实,添加什么监听器就需要添加什么事件。
看实例:
/* * 简单的优化 */public class FrameDemo {// 定义该图形中所需的组件的引用。private Frame f;//窗体private Button btn;//按钮private TextField tf;FrameDemo() {init();}// 初始化public void init() {//初始化组件f = new Frame("my frame");btn = new Button("关闭窗体按钮");tf = new TextField(20);//创建文本框,并指定大小// 对frame进行基本参数设定f.setBounds(200, 200, 500, 400);// 设置距离x,y和宽高f.setLayout(new FlowLayout());// 设置为流式布局f.add(btn);// 按钮关联窗体f.add(tf);//文本框关联窗体f.setVisible(true);// 显示窗体setEvent();// 加载事件}// 设定事件private void setEvent() {// 窗体事件f.addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent e) {System.exit(0);}});// 按钮事件btn.addActionListener(new ActionListener() {// private int count = 1;@Overridepublic void actionPerformed(ActionEvent e) {System.out.println("button action event");// System.out.println("按钮关闭窗体");// System.exit(0);// Button b = (Button) e.getSource();// Frame f1 = (Frame) b.getParent();//// f1.add(new Button("button-" + count++));// f1.validate();}});// 鼠标事件,一般的控件都具备// 给按钮添加鼠标事件btn.addMouseListener(new MouseAdapter() {int count = 1;int mousecount = 1;@Overridepublic void mouseClicked(MouseEvent e) {System.out.println("点击鼠标" + mousecount++);if (e.getClickCount() == 2) {System.out.println("双击事件");}}@Overridepublic void mouseEntered(MouseEvent e) {System.out.println("鼠标进入到该组件" + count++);}});// 给按钮添加键盘事件btn.addKeyListener(new KeyAdapter() {public void keyPressed(KeyEvent e) {// System.out.println(KeyEvent.getKeyText(e.getKeyCode()) +// "...."// + e.getKeyChar() + "...." + e.getKeyCode());if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {// 单键System.exit(0);}// 组合键if (e.isControlDown() && e.getKeyCode() == KeyEvent.VK_ENTER) {System.exit(0);}}});//给文本框添加键盘事件,限制输入tf.addKeyListener(new KeyAdapter() {public void keyPressed(KeyEvent e){int code = e.getKeyCode();if(!(code >= KeyEvent.VK_0 && code <= KeyEvent.VK_9)){System.out.println(code + "输入非法");e.consume();//改变事件的默认处理方式}}});}public static void main(String[] args) {new FrameDemo();}}

第二  其他组件

一、Dialog:
1、概述:Dialog 是一个带标题和边界的顶层窗口,边界一般用于从用户处获得某种形式的输入。也就是对话框,当用户操作不当等情况,系统需要告诉用户时,就用这种形式。当然它也是窗体,所以需要设置大小,显示与否等。
看实例,这个里面就有用到Dialog:
import java.awt.Button;import java.awt.Dialog;import java.awt.FlowLayout;import java.awt.Frame;import java.awt.Label;import java.awt.TextArea;import java.awt.TextField;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.File;/* * 图形化界面,一个类似目录查找界面 */public class MywindowDemo {// 创建主窗体// 创建窗体private Frame frame;// 文本框private TextField textField;// 按钮private Button button;// 文本区域private TextArea textArea;// 创建对话框窗体// 创建对话框private Dialog dialog;// 对话框文本private Label label;// 对话框的确定按钮private Button okbutton;// 对象初始化MywindowDemo() {init();}// 初始化设定public void init() {// 创建主窗体frame = new Frame("主窗体");// 设置主窗体大小属性frame.setBounds(200, 200, 600, 500);// 设置主窗体布局为流式布局frame.setLayout(new FlowLayout());// 创建一个有具体列大小的文本框textField = new TextField(40);// 创建主窗体按钮button = new Button("转到");// 创建文本区域textArea = new TextArea(30, 70);// 添加属性到主窗体frame.add(textField);frame.add(button);frame.add(textArea);// 设置事件mainEvent();// 设置主窗体显示frame.setVisible(true);}private void mainEvent() {// 设定主窗体关闭事件frame.addWindowListener(new WindowAdapter() {@Overridepublic void windowClosing(WindowEvent e) {System.exit(0);}});// 设置文本框中enter键的作用textField.addKeyListener(new KeyAdapter() {public void keyPressed(KeyEvent e) {if (e.getKeyCode() == KeyEvent.VK_ENTER) {showDir();}}});// 设置”转到“按钮事件button.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {showDir();}});}// 设置对话框中所以的事件private void minorEvent() {// 设置对话框的关闭事件dialog.addWindowListener(new WindowAdapter() {@Overridepublic void windowClosing(WindowEvent e) {dialog.setVisible(false);// 让对话框不显示}});// 设置确定按钮事件okbutton.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {dialog.setVisible(false);// 让对话框不显示}});// 设置确定按钮enter事件okbutton.addKeyListener(new KeyAdapter() {@Overridepublic void keyPressed(KeyEvent e) {if (e.getKeyCode() == KeyEvent.VK_ENTER) {dialog.setVisible(false);// 让对话框不显示}}});}// 显示目录的方法private void showDir() {String dirPath = textField.getText();// 得到输入的路径File file = new File(dirPath);// 把路径封装到File对象中// 判断是否存在,已经是否是目录,提高代码的健壮性if (file.exists() && file.isDirectory()) {textArea.setText("");// 情况文本区域String[] names = file.list();for (String name : names) {textArea.append(name + "\r\n");}} else {// 创建对话框窗体dialog = new Dialog(frame, "提示信息-self", true);// 设置对话框属性dialog.setBounds(100, 200, 300, 100);// 设置对话框布局dialog.setLayout(new FlowLayout());// 创建对话框文本label = new Label();// 创建确定按钮okbutton = new Button("确定");String info = "你输入的信息" + dirPath + "有误";label.setText(info);// 关联控件dialog.add(label);dialog.add(okbutton);// 加载事件minorEvent();// 设置显示dialog.setVisible(true);}}public static void main(String[] args) {new MywindowDemo();}}

二、菜单:Menu

1、Menu:菜单,Menu extends MenuItem;有右三角的图标存在,可添加Menu和MenuItem,也就是可以添加条目和菜单,当添加菜单时也就有了子菜单,也就有了小三角。

2、MenuBar:菜单栏,可添加菜单和条目

3、MenuItem:条目,及菜单项目,无右三角的图标存在,是最终的菜单项。

4、菜单的事件处理和组件一样,可以对类型为MenuItem和Menu的对象这个事件源添加活动监听ActionListener,并进行相关的事件处理。

看实例:
/* * 实现菜单 */public class MenuDemo {private Frame f;private MenuBar menuBar;// MenuBar菜单条,下面放菜单menu// menu菜单下即可以放menu也可以放menuitem,放menu默认会有个箭头,默认是会有子菜单的private Menu mainMenu, secondMenu;private MenuItem item1, item2;// menuitem菜单中最终条目MenuDemo() {init();}public void init() {f = new Frame("my window");f.setBounds(200, 100, 400, 500);f.setLayout(new FlowLayout());menuBar = new MenuBar();mainMenu = new Menu("文件");secondMenu = new Menu("含子菜单");item1 = new MenuItem("关闭");item2 = new MenuItem("第二级子菜单");f.setMenuBar(menuBar);menuBar.add(mainMenu);mainMenu.add(secondMenu);mainMenu.add(item1);secondMenu.add(item2);myEvent();f.setVisible(true);}public void myEvent() {f.addWindowListener(new WindowAdapter() {@Overridepublic void windowClosing(WindowEvent e) {System.exit(0);}});item1.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {System.exit(0);}});}public static void main(String[] args) {new MenuDemo();}}

第三  练习

一、jar包双击执行:

既然是图形化界面,就需要通过图形化界面的形式运行程序,而不是是用Dos命令行执行,那么如何通过双击程序就执行程序呢?这就需要将程序的class文件打包,步骤如下:

1、首先要在java文件中导入一个包,没有则需创建一个包,如package mymenu;也就是给java文件分配一个目录

2、生成包:通过编译javac -d c:\myclass MenuTest.java,此时则在c盘下的myclass文件夹下生成了所有的.class文件

3、在此目录下新建一个文件,如1.txt或者其他任意名称任意扩展名的文件都可,然后在其中编辑固定的格式:“Main-Class: mymenu.MenuDemo”,只写引号中的内容。注意:需要需要在冒号后有一个空格,在文件末尾要回车。

4、编译:jar -cvfm my.jar 1.txt mymenu即可。如果想添加其他信息,则直接编译jar即可得出相应的命令

5、此时双击即可执行。

说明:

1)在固定格式中:

a.如果无空格:在编译的时候,就会报IO异常,提示无效的头字段,即invalid header field。这说明1.txt在被IO流读取。

b.如果无回车:在列表清单.MF中不会加入相应的加载主类的信息,也就是说配置清单的属性主类名称不会加载进清单中,也就不会执行。

2)jar文件必须在系统中注册,才能运行。注册方法如下:

A.对于XP系统:

   a.打开任意对话框,在菜单栏点击工具按钮,选择文件夹选项

   b.选择新建--->扩展名,将扩展名设置为jar,确定

   c.选择高级,可更改图标,然后点击新建,命名为open,

   d.在用于可执行应用程序中,点浏览,将jdk下的bin的整个文件路径添加进来,并在路径后添加-jar即可。

B.对于win7系统:

   a.改变打开方式:右击.jar文件,点击打开方式,选择默认程序为jdk下bin中的javaw.exe应用程序。

   b.修改关联程序的注册表:打开注册表(win+r),找到注册表路径\HKEY_CLASSES_ROOT\Aplications\javaw.exe\shell\open\command下的字符串值,右击点修改,将值改为:"C:\Program Files\Java\jre6\bin\javaw.exe" -jar "%1"其中-jar前的路径就是javaw.exe的路径。保存

   c.双击即可执行jar程序,如果仍不能执行,则需下载最新版的jdk。

示例:
/* * 简易记事本 */public class MenuTest {private Frame f;private MenuBar menuBar;private Menu menu;private MenuItem openItem, saveItem, closeItem;private TextArea textArea;private FileDialog openDia, saveDia;// 保存文件的对话框private File file;MenuTest() {init();}public void init() {f = new Frame("my frame");// 初始化窗口menuBar = new MenuBar();// 初始化菜单栏menu = new Menu("文件");// 初始化文件菜单openItem = new MenuItem("打开");// 初始化打开条目saveItem = new MenuItem("保存");// 初始化保存条目closeItem = new MenuItem("关闭");// 初始化关闭条目textArea = new TextArea();// 初始化文本区域openDia = new FileDialog(f, "我要打开", FileDialog.LOAD);// 打开,对话框的模式saveDia = new FileDialog(f, "我要保存", FileDialog.SAVE);// 保存,对话框的模式// 设置属性f.setBounds(100, 100, 600, 600);// 关联控件f.setMenuBar(menuBar);menuBar.add(menu);menu.add(openItem);menu.add(saveItem);menu.add(closeItem);f.add(textArea);// 加载事件myEvent();// 设置显示f.setVisible(true);}/* * 时间处理方法 */private void myEvent() {// 设置窗体的关闭事件f.addWindowListener(new WindowAdapter() {@Overridepublic void windowClosing(WindowEvent e) {System.exit(0);}});// 设置菜单中的关闭事件closeItem.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {System.exit(0);}});// 设置菜单中打开事件openItem.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {openDia.setVisible(true);// 设置打开对话框显示// 获取目录String dirPath = openDia.getDirectory();// 获取文件名String name = openDia.getFile();// 点击对话栏的“取消”按钮,设定if (dirPath == null || name == null) {return;}// 清空文本区域textArea.setText("");// 把获取到的文件封装到File对象中file = new File(dirPath, name);// 开始将文件中的内容读取到文本区域中BufferedReader bfr = null;try {bfr = new BufferedReader(new FileReader(file));// 建立读取流String line = "";while ((line = bfr.readLine()) != null) {textArea.append(line + "\r\n");}} catch (FileNotFoundException e1) {new RuntimeException("文件不存在");} catch (IOException e1) {new RuntimeException("读取文件失败");} finally {if (bfr != null) {try {bfr.close();} catch (IOException e1) {new RuntimeException("关闭资源失败");}}}}});// 设置保存事件saveItem.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {if (file == null) {saveDia.setVisible(true);// 设置保存对话框显示// 设置保存路径String dirPath = saveDia.getDirectory();// 得到目录String name = saveDia.getFile();// 得到文件// 点击取消按钮if (dirPath == null || name == null) {return;}file = new File(dirPath, name);}// 开始保存文件BufferedWriter bfw = null;try {bfw = new BufferedWriter(new FileWriter(file));// 建立写入流String text = textArea.getText();// 得到需要写入文件的内容bfw.write(text);// 写内容bfw.flush();// 刷新缓冲区} catch (IOException e1) {new RuntimeException("写入文件失败");} finally {if (bfw != null) {try {bfw.close();} catch (IOException e1) {new RuntimeException("关闭资源失败");}}}}});}public static void main(String[] args) {new MenuTest();}}