黑马程序员——Java基础--GUI

来源:互联网 发布:阿里云官方论坛 编辑:程序博客网 时间:2024/05/20 10:52

-----------android培训java培训java学习型技术博客、期待与您交流!------------

GUI(用户图形界面)

一、概述

        计算机与用户交互的两种方式一种就是我们之前学习的CLI命令行用户接口,英文名叫Command LineUser Interface,即常见的Dos命令行操作。而另一种就是今天我要总结的GUI又叫图形用户接口,英文名叫Graphical User Interface简单的说就是用图形的方法来显示计算机的操作界面,为此,java给我们提供了java.Awt包和javax.Swing两个包。

        java.Awt包:Abstract Window Toolkit,即抽象窗口工具包。要调用本地系统方法实现功能,属重量级控件。

        javax.Swing包:在AWT的基础上建立的一套图形界面系统,其中提供了更多的组件,且完全由java实现,增强了移植性,属轻量级控件。

二、继承关系

Container常用子类:Window   Panel(面板,不能单独存在。)

Window常用子类:Frame  Dialog


三、布局管理

1、创建Frame窗体:

        Frame f = new Frame("my Frame");//可设置标题,即窗体名字

2、对窗体进行基本设置:如大小,位置,布局等:

        f.setSize(int wight,int hight);//窗体大小设置

        f.setLocation(int x,int y);//窗体显示位置设置,横纵坐标

        f.setBounds(int x,int y,int wight,int hight),也可以直接用这个方法对大小和位置设置

        f.setLayout(Layout layout),参数为指定的布局管理器,如FlowLayout

3、定义组件:

       Button b = new Button(“my Button”);//可设置组件的名称

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

        f.add(b);//将按钮组件添加进窗体

5、让窗体显示:

        f.setVisible(boolean b);//通过设置参数是true还是false是否显示窗体

举例:

package cn.itheima.gui;import java.awt.Button;import java.awt.FlowLayout;import java.awt.Frame;/* * 简单的窗体创建 *  */public class GuiDemo {public static void main(String[] args) {//创建窗体对象Frame f = new Frame("操作练习");//窗体大小设置f.setSize(200, 200);//窗体显示位置设置,横纵坐标f.setLocation(2, 2);//设置布局方式f.setLayout(new FlowLayout());//定义一个组件及名称Button b = new Button("点我");//将按钮组件添加进窗体f.add(b);//通过设置参数是true还是false是否显示窗体f.setVisible(true);}}

四、事件监听机制

1事件源(组件):就是awtswing包中的那些图形界面组件

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

3监听器(Listener):将可触发某一事件的动作(不只一个动作),都封装到侦听器中。

4事件处理:负责处理事件的具体实现

事件监听机制的处理流程图:

常见事件的处理机制:

        事件源为鼠标,对应的事件类是MouseEvent,包括具体的事件:鼠标按下、松开、右键等,其相应的事件监听接口是MouseListener。同样的当事件源为窗口的时候。对应的事件类和事件监听接口分别是WindowEventWindowListener

        在这个实例中,为了简化编程,JDK为我们提供了简化编程的处理,我们只要实现了接口的所有方法就行了,不用处理。

五、常见的事件及其处理

1、

package cn.itheima.gui;import java.awt.Button;import java.awt.FlowLayout;import java.awt.Frame;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;/* 需求:让窗体的关闭功能实现       让按钮也具备退出程序的功能 */public class GuiDemo2 {private Frame f;private Button b;//构造方法 GuiDemo2(){init();}//窗体创建与功能实现 public void init(){//实例化组件 f = new Frame("我的窗口");f.setBounds(200, 200, 200, 200);//设置大小和位置f.setLayout(new FlowLayout());b = new Button("点我");f.add(b);//加载窗体上事件myEvent();//显示窗体f.setVisible(true);}//注册事件 private void myEvent(){f.addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent e) {System.exit(0);}});//让按钮具备退出程序的功能  /*         按钮就是事件源。         那么选择哪个监听器呢?         通过关闭窗体示例了解到,想要知道哪个组件具备什么样的特有监听器。         需要查看该组件对象的功能。         通过查阅button的描述。发现按钮支持一个特有监听addActionListener。 */b.addActionListener(new ActionListener() {private int count = 1;public void actionPerformed(ActionEvent e) {Button b = (Button)e.getSource();Frame f = (Frame)b.getParent();f.add(new Button("button-"+count++));f.validate();}});}public static void main(String[] args) {new GuiDemo2();}}

2、

package cn.itheima.gui;import java.awt.*;import java.awt.event.*;/* * 共性事件:键盘事件和鼠标事件  * 需求:在窗体中列出指定目录内容*/public class MouseAndKeyEvent {//全局变量的界面组件引用 private Frame f;private Button but;private TextField tf;private TextArea ta;//构造方法MouseAndKeyEvent() {init();}//窗体创建与功能实现public void init() {//组件实例化f = new Frame("我的窗口");f.setBounds(300, 100, 600, 500);f.setLayout(new FlowLayout());tf = new TextField(20);ta =new TextArea(30,50);but = new Button("点我");//添加组件f.add(tf);f.add(but);f.add(ta);//窗体事件myEvent();//窗体显示f.setVisible(true);}//注册事件private void myEvent() {//窗体关闭功能f.addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent e) {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("您输入的数字非法,请重数。。。");e.consume();//不显示输入的字符}}});// 给But添加一个键盘监听。but.addKeyListener(new KeyAdapter() {public void keyPressed(KeyEvent e) {//捕获同时按下ctrl+entryif (e.isControlDown() && e.getKeyCode() == KeyEvent.VK_ENTER)System.exit(0);System.out.println("ctrl+enter is run");System.out.println(KeyEvent.getKeyText(e.getKeyCode()) + "...."+ e.getKeyCode());}});//鼠标活动事件but.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {System.out.println("按钮是ok的");}});//鼠标事件but.addMouseListener(new MouseAdapter() {private int count = 1;private int clickCount = 1;public void mouseEntered(MouseEvent e) {System.out.println("鼠标进入到该组件" + count++);}public void mouseClicked(MouseEvent e) {if (e.getClickCount() == 2)System.out.println("双击动作" + clickCount++);}});}public static void main(String[] args) {new MouseAndKeyEvent();}}

3、

package cn.itheima.gui;import java.awt.*;import java.awt.event.*;import java.io.*;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("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 + "\r\n");}} else {String info = "您输入的信息:" + dirPath + "是错误的。请重输";lab.setText(info);d.setVisible(true);}}public static void main(String[] args) {new MyWindowDemo();}}

六、菜单创建

package cn.itheima.gui;import java.awt.*;import java.awt.event.*;public class MyMenuDemo {private Frame f;private MenuBar mb;private Menu m, subMenu;private MenuItem closeItem, subItem;MyMenuDemo() {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 MyMenuDemo();}}

-----------android培训java培训、java学习型技术博客、期待与您交流!------------


0 0