-------android培训、java培训、期待与您交流! ----------
一 GUI概述
GUI
Graphical User Interface(图形用户接口)。
用图形的方式,来显示计算机操作的界面,这样更方便更直观。
Java为GUI提供的对象都存在java.Awt和javax.Swing两个包中。
CLI
Command line User Interface (命令行用户接口)
就是常见的Dos命令行操作。
需要记忆一些常用的命令,操作不直观。
比如:创建文件夹,或者删除文件夹等
Awt与 Swing
java.Awt:Abstract Window ToolKit (抽象窗口工具包),需要调用本地系统方法实现功能。属重量级控件。
javax.Swing:在AWT的基础上,建立的一套图形界面系统,其中提供了更多的组件,而且完全由Java
实现。增强了移植性,属轻量级控件
二 继承关系
三 布局管理
容器中的组件的排放方式,就是布局。
常见的布局管理器:
FlowLayout(流式布局管理器)从左到右的顺序排列。Panel默认的布局管理器。
BorderLayout(边界布局管理器)东,南,西,北,中。Frame默认的布局管理器。
GridLayout(网格布局管理器)规则的矩阵
CardLayout(卡片布局管理器)选项卡
GridBagLayout(网格包布局管理器)非规则的矩阵
四 创建图形化界面
简单的窗体创建过程Container常用子类:Window Panel(面板,不能单独存在。)
Window常用子类:Frame Dialog
1创建frame窗体
2对窗体进行基本设置(大小,位置,布局)
3定义组件
4将组件通过窗体的add方法添加到窗体中
5让窗体显示,通过setVisible(true)
import java.awt.*; class AwtDemo { public static void main(String[] args) { Frame f = new Frame("MY AWT"); f.setSize(500,400); f.setLocation(300,200); f.setLayout(new FlowLayout()); Button b = new Button("按钮"); f.add(b); f.setVisible(true); } }
五 事件监听机制
1事件监听机制组成
事件源(组件)
事件(Event)
监听器(Listener)
事件处理(引发事件后处理方式)
2事件监听机制流程图
3事件监听机制
确定事件源(容器或组件)
通过事件源对象的addXXXListener()方法将侦听器注册到该事件源上。
该方法中接收XXXListener的子类对象,或者XXXListener的子类XXXAdapter的子类对象。
一般用匿名内部类来表示。
在覆盖方法的时候,方法的参数一般是XXXEvent类型的变量接收。
事件触发后会把事件打包成对象传递给该变量。(其中包括事件源对象。通过getSource()或者,getComponent()获取。)
六 Action事件、窗体事件、鼠标事件、键盘事件
import java.awt.*; import java.awt.event.*; class FrameButtonDemo { //定义该图形中所需组件的引用 private Frame f; private Button but; private TextField tf; FrameButtonDemo() { init();//单独封装一个方法,可以被覆盖。也可以都写在这里 } //对图形化界面进行初始化 public void init() { //创建frame窗体 f = new Frame("my frame"); //对frame进行基本设置 f.setBounds(300,100,600,500); f.setLayout(new FlowLayout()); //定义组件 tf = new TextField(20); but = new Button("my button"); //将组件添加到frame窗体中 f.add(tf); f.add(but); //加载窗体上的事件 myEvent(); //显示窗体 f.setVisible(true); } //添加事件 public void myEvent() { //窗体事件 /* 关闭窗体 WindowListener覆盖了7个方法,可是只用到了关闭的动作 其他动作都没有用到,可是却必须复写 因为WindowListener的子类WindowAdapter已经实现了WindowListener接口 并覆盖了其中的所有方法,那么只要继承自Windowadapter覆盖需要的方法即可 */ f.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } public void windowActivated(WindowEvent e) { System.out.println("我活了"); } public void windowOpened(WindowEvent e) { System.out.println("我被打开了"); } }); //Action事件 /* 让按钮具备退出功能 按钮就是事件源,那么选择哪个监听器呢?通过关闭窗体事例了解到, 想要知道那个组件具备什么样的特有监听器,需要查看该组件对象的 功能。通过查看api,发现按钮支持的一个特有监听器是addActionListener */ but.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.out.println("按钮关的"); //System.exit(0); } }); //鼠标事件 but.addMouseListener(new MouseAdapter() { private int count = 1; private int clickCount = 1; public void mouseEntered(java.awt.event.MouseEvent e) { System.out.println("鼠标进入到该组件"+count++); } //注意,鼠标的点击动作比按钮的点击动作先执行 public void mouseClicked(java.awt.event.MouseEvent e) { //if(e.getClickCount()==2) //System.out.println("鼠标双击该组件"+clickCount++); System.out.println("鼠标单击该组件"+clickCount++); } }); //键盘事件1 but.addKeyListener(new KeyAdapter() { public void keyPressed(KeyEvent e) { //返回与此事件中的键关联的整数 keyCode。 //if(e.getKeyCode()==KeyEvent.VK_SPACE) //System.exit(0); //System.out.println(KeyEvent.getKeyText(e.getKeyCode())+"..."+e.getKeyCode()); if(e.isControlDown()&&e.getKeyCode()==KeyEvent.VK_ENTER) System.out.println("组合键"); } }); //键盘事件2 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 FrameButtonDemo(); } }
七 常见需求
需求1 打开指定目标内容
import java.awt.*; import java.awt.event.*; import java.io.*; class MyWindowDemo { //窗体 private Frame f; //按钮 private Button but; //文本框,文本区域 private TextArea ta; private TextField tf; //对话框,标签 private Dialog d;//这个应该定义到方法里,优化内存, 不应该窗体一创建就产生 private Label lab; private Button okBut; MyWindowDemo() { init(); } public void init() { f = new Frame("my window"); f.setBounds(300,100,500,400); f.setLayout(new FlowLayout()); tf = new TextField(50); but = new Button("转到"); ta = new TextArea(20,60);//行,列 d = new Dialog(f,"提示信息-self",true);//true表示除了对话框其他的都不能操作,反之 d.setBounds(400,200,240,150); d.setLayout(new FlowLayout()); lab = new Label(); okBut = new Button("确定"); f.add(tf); f.add(but); f.add(ta); d.add(lab); d.add(okBut); 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); } }); but.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { showDir(); } }); f.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); tf.addKeyListener(new KeyAdapter() { public void keyPressed(KeyEvent e) { if(e.getKeyCode()==KeyEvent.VK_ENTER) showDir(); } }); } 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(); } }
需求2 记事本打开文件保存文件(菜单的建立)
MenuBar,Menu,MenuItem
先创建菜单条,再创建菜单,每一个菜单中建立菜单项。
也可以菜单添加到菜单中,作为子菜单。
通过setMenuBar()方法,将菜单添加到Frame中。
/* 1,将day22中的class文件存到d:\myclass(带着包编译) D:\myjava\day22>javac -d d:\myclass MyMenuTest.java 2,退到d:\myclass D:\myjava\day22>cd\ D:\>cd myclass D:\myclass> 3,给jar包添加配置信息 在d:\myclass创建一个文本文档1.txt,写入Main-Class: mymenu.MyMenuTest加回车,然后保存 3,创建jar包 //D:\myclass>jar -cvf my.jar mymenu D:\myclass>jar -cvfm my.jar 1.txt mymenu */ //打开文件保存文件 package mymenu; import java.awt.*; import java.awt.event.*; import java.io.*; public class MenuTest { private Frame f; private MenuBar bar; private Menu fileMenu; private MenuItem openItem,saveItem,closeItem; private TextArea ta; private FileDialog openDia,saveDia; private Label lab; private Button okBut; private File file; MenuTest() { init(); } public void init() { f = new Frame("my window"); f.setBounds(300,100,650,600); //f.setLayout(new FlowLayout());//默认边界布局 bar = new MenuBar(); fileMenu = new Menu("文件"); openItem = new MenuItem("打开"); saveItem = new MenuItem("保存"); closeItem = new MenuItem("退出"); openDia = new FileDialog(f,"我要打开",FileDialog.LOAD);//默认就是LOAD saveDia = new FileDialog(f,"我要保存",FileDialog.SAVE); ta = new TextArea(); f.setMenuBar(bar); bar.add(fileMenu); fileMenu.add(openItem); fileMenu.add(saveItem); fileMenu.add(closeItem); 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.close(); } catch (IOException ex) { throw new RuntimeException("写入失败"); } } }); closeItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.exit(0); } }); 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+"\r\n"); } bufr.close(); } catch (IOException ex) { throw new RuntimeException("读取失败"); } } }); f.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); } public static void main(String[] args) { new MenuTest(); } }
------- android培训 、 java培训 、期待与您交流! ----------