Java中的GUI概述

来源:互联网 发布:第二个爸爸知乎 编辑:程序博客网 时间:2024/05/21 09:49

    其实,事实上,Java中的GUI并不算是学习的重点,任何培训机构都不会花时间去讲GUI的东西。因为一般来说进公司之后,很少回去做一些 B/S 架构的东西,都是与 JavaWeb 有关的 C/S 架构的工程。但是,既然学习了Java,还是要懂得这方面的知识,至少技多不压身。所以,今天就来谈谈这两天学到的东西。


GUI 概述:

    GUI

    Graphical User InterFace(图形用户接口)

    用图形的方式,来显示计算机操作的界面,更方便更直观

    与其相对的

    CLI

    Command Line User Interface(命令行用户接口)

    常见的Dos命令行操作

    需要记忆一些常用的命令,操作不直观

 

 Java为GUI提供的对象都存在java.Awt 和javax.Swing 两个包中。

Java.Awt:Abstract Window ToolKit (抽象窗口工具包),需要调用本地系统方法实现功能(依赖于操作系统),属于重量级控件;

Javax.Swing:在AWT 的基础上,建立的一套图形界面系统,其中提供了更多的组件,而且完全由Java实现(不依赖于操作系统)。增强了移植性,属于轻量级控件。

SWT:Eclipse 下的图形用户界面,基于AWT。


图形化界面包中的组件:


布局管理器:

    容器中的组件的排放方式,就是布局。常见的布局管理器有:

FlowLayout(流式布局管理器)

——从左到右的顺序排列;

Panal默认的布局管理器。

BorderLayout(边界布局管理器)

——东(Eastern),西(Western),南(South),北(North),中(Certain);

——Frame默认的布局管理器。

GridLayout(网格布局管理器)

——规则的矩阵

CardLayout(卡片布局管理器)

—— 选项卡

GridBagLayout(网格包布局管理器)

——非规则的矩阵

 

图形化界面由另外的线程来执行,并非主线程。


创建图形化界面:

1.创建Frame窗体;

2.对窗体进行基本设置(大小,位置,布局);

3.定义组件;

4.将组件通过窗体的add方法添加到窗体中;

5.让窗体显示,通过setVisible(true)。

 

事件监听机制的特点:

1.事件源(awt包或者swing包中的那些图形界面组件);

2.事件(每一个事件源都有自己特有的对应事件和共性事件);

3.监听器(将可以触发某一个事件的动作(不止一个动作)都已经封装到了监听器中)

以上三者,在Java中都已经定义好了,直接获取其对象就可以了

4.事件处理

WindowListener接口 –-> WindowAdapter 实现类

 

要把事件与图形化界面的组件分离开来。

 

键盘与鼠标事件:

Component类下有方法:

      addKeyListener(KeyListener l)

      addMouseListener(MouseListenerl)

鼠标事件:接口MouseListener --->类MouseAdapter


事件源--->事件--->监听器 --->要操作谁

 

键盘事件:接口KeyListener --->类 KeyAdapter

           类KeyEvent --->父类InputEvent

KeyEvent中的方法:

两个程序

下面这个程序用到了窗体的基本组件,主要功能是查询所输入的文件夹下面的文件目录,如果输入有误弹出警示窗口。

import java.awt.*;import java.awt.event.*;import java.io.File;//查询本机电脑目录下的文件夹信息public class MyWindowDemo {private Frame f;private TextField tf;private Button but;private TextArea ta;//弹出消息private Dialog d;private Label lab;private Button okBut;MyWindowDemo(){//初始化init();}public void init(){//窗口f = new Frame("Z 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,"提示信息",true);d.setBounds(400, 200, 240, 180);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(){//事件d.addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent e){d.setVisible(false);}});okBut.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {d.setVisible(false);}});tf.addKeyListener(new KeyAdapter() {public void keyPressed(KeyEvent e){if(e.getKeyCode()==KeyEvent.VK_ENTER){showDir();}}});but.addActionListener(new ActionListener() {@Overridepublic 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+"\r\n");}}else {String info = "您输入的信息:"+dirPath+"是错误的";lab.setText(info);d.setVisible(true);}}public static void main(String[] args) {new MyWindowDemo();}}

下面这个程序是关于菜单栏的操作,主要功能是相当于一个简单的文本编辑器,可以打开,编辑文件

import java.awt.*;import java.awt.event.*;import java.io.*;//文本编辑器public class MyMenuDemo {private Frame f;private MenuBar bar;//菜单栏(包含菜单或条目)private Menu fileMenu;//菜单(包含条目或菜单)private MenuItem openItem, saveItem, closeItem;//条目private TextArea ta;private FileDialog openDia, saveDia;private File file;MyMenuDemo(){init();}public void init(){f = new Frame("My Window");f.setBounds(300, 100, 600, 720);//f.setLayout(new FlowLayout());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() {@Overridepublic void actionPerformed(ActionEvent e) {if(file==null){saveDia.setVisible(true);String dirPath = openDia.getDirectory();String fileName = openDia.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 e1) {throw new RuntimeException();}}});openItem.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {openDia.setVisible(true);String dirPath = openDia.getDirectory();String fileName = openDia.getFile();System.out.println(dirPath+"-->"+fileName);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 e1) {throw new RuntimeException("读取失败");}}});closeItem.addActionListener(new ActionListener() {@Overridepublic 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 MyMenuDemo();}}





0 0
原创粉丝点击