黑马程序员——学习日记之--GUI图形界面学习总结
来源:互联网 发布:c语言面向对象是什么 编辑:程序博客网 时间:2024/06/05 19:43
——- android培训、java培训、期待与您交流! ———-
GUI : Graphical User Interface (图形用户接口)。
用图形的形式显示计算机操作的界面,更直观。
CLI: Command Line User Interface (命令行用户接口)。
常见的DOS命令行操作,不直观。
java为GUI提供的对象都在 java.Awt 和 java.Swing 两个包中。
java.Awt : 抽象窗口工具包:Abstract window ToolKit。
需要调用本地系统的方法实现功能,重量级控件(依赖系统)。
java.Swing : 在Awt的基础上建立的一套图形界面系统,它提供了更多组件,且完全由java实现。
增强了移植性,属轻量级控件(系统依赖性很低)。
组件关系简表:
布局:
FlowLayout : 流式布局,Panel默认的布局,默认居中,从左往右排列。
BorderLayout :边界布局,Frame默认的布局,需要指定边界,否则全部居中填充显示。
CardLayout :卡片式布局,选项卡形式的布局。
GridLayout :网格布局,将面板划分成规则的矩阵排列,每一小格可容纳一个组件。
GridBagLayout :网格包布局,不太规则的网格布局,有的组件会占用多个网格的形式。
注:坐标式布局最方便,没有布局上的约束,只要指定坐标就可以达到其他布局能达到的效果。
一:Component及其子类介绍
Component :组件。
具有图形表示能力的组件对象。常用方法:
String getName() //获取组件的名称void setName(String s) //设置名称int getHeight() //获取组件的高度int getWidth() //获取组件的宽度void setSize(int x,int y) //设置窗体横纵长度void setLocation(int x,int y) //设置组件的左上角距左边和上边位置。void setBounds(x,y,w,h) //设置组件的大小和位置void setVisible(boolean b) //设置是否可见Font getFont() //获取组件的字体void setFont(Font f) //设置字体boolean isShowing() //组件是否在屏幕上显示boolean isVisible() //如果组件是可见的,返回truevoid addKeyListener(KeyListener kl) //对此对象添加键盘监听器void addMouseListener(MouseListener ml) //对此对象添加鼠标监听器
Button :按钮。
Component 的子类。构造方法:
Button() //空标签的按钮Button(String s) //指定标签的按钮
常用方法:
void addActionListener(ActionListener al) //对按钮对象添加动作监听器 String getLabel() //获取按钮标签 void setLabel(String s) //设置按钮标签
Label :标签。
Component 的子类。构造方法:
Label() //构造空标签Label(String s) //构造指定文本的标签
常用方法:
String getText() //获取标签文本void setText(String s) //设置标签文本
Checkbox :复选框。
Component 的子类。常用方法:
String getLabel() //获取复选框的标签文本void setLabel(String s) //设置复选框的标签文本
TextComponent :文本组件。
它有两个子类,
(1) TextArea :文本域。构造方法:
TextArea() //构造新文本域TextArea(String s) //构造指定初始字符串的文本域TextArea(int row,int col) //构造指定行列数的文本域TextArea(String s,row,col) //构造指定初始字符串和行列数的文本域
常用方法:
void append(String s) //对文本域添加字符串void insert(String s,int pos) //在文本域的指定位置插入字符串void setColumns(int col) //设置文本域列数void setRows(int row) //设置文本域行数int getColumns() //获取文本域列数int getRows() //获取文本域行数
(2) TextField :文本框。
单行文本框组件。构造方法:
TextField() //构造默认文本框TextField(int len) //构造指定列数的文本框TextField(String s) //构造包含指定内容的文本框TextField(String s,int len) //构造包含指定内容的固定长度文本框
常用方法:
int getColumns() //获取文本框的列数void setColumns(int col) //设置文本框的列数void setText(String s) //设置文本框的内容void addActionListener(ActionListener al) //给文本框对象添加动作监听器
Container : 组件容器。
它是 Component 的子类。常用方法:
Component add(Component c) //添加一个组件Component add(Component c,int index) //指定位置添加组件void setLayout(LayoutManager lm) //设置布局
二:Container 的子类介绍
Panel :面板。
应用程序可以将其他组件放在面板提供的空间内,默认 FlowLayout 布局。构造方法:
Panel() //使用默认的布局管理器创建新面板Panel(Layout) //使用指定布局创建面板
Window :窗体。
没有边界和菜单栏的窗体,默认 BorderLayout 。常用方法 :
addWindowListener(WindowListener wl) //对窗体添加监听器
三:Window 的子类介绍
Frame :框架。
带有标题和边框的框架,默认 BorderLayout 。
它是 Window 的子类构造方法:
Frame() //构造一个最初不可见的窗体实例。Frame(String title) //构造一个指定标题的最初不可见的窗体示例。
常用方法:
String getTitle() //获取框架标题
Dialog :对话框。
默认布局 BorderLayout 。构造方法:
Dialog(Dialog dl) //指定所有者为对话框的对话框Dialog(Dialog dl,title) //指定所有者和标题的对话框Dialog(Dialog dl,title,true) //指定所有者和标题的对话框,模式为阻止切换窗体模式。Dialog(Frame f) //指定所有者为框架的对话框Dialog(Frame f,title) //指定所有者和标题的对话框Dialog(Frame f,title,true) //指定所有者和标题的对话框,模式为阻止切换窗体模式。Dialog(Window w) //指定所有者为窗体的对话框Dialog(Window w,title) //指定所有者和标题的对话框Dialog(Window w,title,true) //指定所有者和标题的对话框,模式为阻止切换窗体模式。
注: Dialog中的模式,是指在不对对话框进行制定操作的情况下,是否禁止用户对其他窗体进行操作。
设置为true代表禁止用户在未确认时进行其他操作。常用方法:
String getTitle() //获取标题void setTitle(String s) //设置标题void setVisible(boolean b) //设置对话框可见
Dialog 常用的子类
FileDialog :文件对话框。
FileDialog 有两种模式,读取文件和保存文件:
FileDialog.LOAD , FileDialog.SAVE 。
默认模式是 FileDialog.LOAD ,代表读取一个文件。构造方法:
FileDialog(Frame f) //创建指定所有者的文件对话框FileDialog(Frame f,String title) //创建指定所有者和标题的文件对话框FileDialog(Frame f,title,mode) //创建指定标题,所有者,模式的文件对话框FileDialog(Dialog log) //创建指定所有者的文件对话框FileDialog(Dialog log,String title) //创建指定标题和所有者的文件对话框FileDialog(Dialog log,title,mode) //创建指定标题,所有者,模式的文件对话框
常用方法:
String getFile() //获取文件对话框选定的文件,String getDirectory() //获取选定文件的目录。
四:事件监听机制
是指设置一定的处理方式对可能发生的特定事件进行专门处理。
监听器通过对事件源进行监听,如果发生了指定的事件,就调用事件处理方式对此事件进行处理。
WindowListener :窗体监听器接口。
接口中所有方法都是抽象的,要处理窗体事件,可以用它的适配器: WindowAdapter 。
适配器只为了方便创建监听对象,这样可以不用全部复写接口中的方法。举例:窗口事件监听器监听窗口激活、打开、关闭事件。
import java.awt.*;import java.awt.event.*;class Demo { public static void main(String [] args) { Frame f=new Frame("我的窗体"); f.setSize(700,400); f.setLocation(300,200); f.setLayout(new FlowLayout()); Button b=new Button("我的按钮"); f.add(b); f.addWindowListener(new WindowAdapter(){ //接收窗口事件的抽象适配器类,但没有抽象方法 public void windowActivated(WindowEvent e) { //窗口前置或打开监听事件 System.out.println("窗口激活了"); } public void windowOpened(WindowEvent e) { //窗口打开监听事件 System.out.println("窗口打开了"); } public void windowClosing(WindowEvent e) { //关闭窗口监听事件 System.out.println("窗口关闭了"); System.exit(0); } }); f.setVisible(true); }}
// 窗体监听的代码优化:图形界面和窗体事件的分离
import java.awt.*;import java.awt.event.*;class Demo { private Frame f; private Button b; Demo() { init(); //定义函数包装代码。 } public void init() { f=new Frame("My Frame!"); f.setBounds(300,100,700,400); f.setLayout(new FlowLayout()); b=new Button("My Button!"); f.add(b); myEvent(); f.setVisible(true); } public void myEvent() { //事件监听 f.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e) { System.exit(0); } }); } public static void main(String [] args) { new Demo(); }}
ActionListener :动作监听器接口。
接受动作的监听器,接口中只有一个方法,所以不需要适配器,直接复写 actionPerformed方法。使用时直接添加事件监听,
button.addWindowListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.out.println("按钮动作"); }});
MouseListener:鼠标监听器接口 。
此接口中所有方法都是抽象的,要处理鼠标事件,可以用它的适配器: MouseAdapter 。import java.awt.*;import java.awt.event.*;class Demo { private Frame f; private Button b; Demo () { init(); } public void init() { f=new Frame("My Frame!"); f.setBounds(300,100,700,400); f.setLayout(new FlowLayout()); b=new Button("My Button!"); f.add(b); myEvent(); f.setVisible(true); } public void myEvent() { b.addMouseListener(new MouseAdapter(){ public void mouseClicked(MouseEvent e) { //对鼠标点击事件进行处理 if (e.getClickCount()==2) System.out.println("shuangji"); } public void mouseEntered(MouseEvent e) { //对鼠标进入事件进行处理 System.out.println("鼠标进入"); } public void mouseExited(MouseEvent e) { //对鼠标离开事件进行处理 System.out.println("鼠标离开"); } }); } public static void main(String [] args) { new Demo(); }}
注意:对按钮添加动作监听和鼠标监听有点区别,
动作监听器只要监听到按钮有动作,就会触发事件,鼠标监听是监听鼠标动作。
判断是否双击时,两次点击时间间隔必须小于系统设置的鼠标点击间隔,否则认为是两次单击。KeyListener :键盘监听器接口。
此接口含有适配器: KeyAdapter 。对按钮添加键盘监听,
button.addKeyListener(new KeyAdapter(){ public void keyPressed(KeyEvent e) { //打印键盘键值和对应的键盘码。 int code=e.getKeyCode(); System.out.println(KeyEvent.getKeyText(code)+":"+code); if (e.getKeyCode()==KeyEvent.VK_ESCAPE) //当输入某个指定的键时,执行动作。 System.out.println("esc键退出"); if(e.isControlDown() && e.getKeyCode()==KeyEvent.VK_ENTER) System.out.println("ctrl+Enter键退出"); }});
对文本框添加键盘监听,监听键盘录入数据。
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(); //不按照默认处理方式处理事件。 } }});
注意:这里调用consume方法,代表如果执行此方法就不往文本框中写入数据。
举例:做一个图形化界面,输入路径,按转到按钮可以在文本域显示目录下所有文件列表。
import java.awt.*;import java.awt.event.*;import java.io.*;class Demo { private Frame f; private Button b; private Button logb; private TextField text; private TextArea area; private Dialog log; private Label lab; Demo() { init(); } public void init() { //设置框架外观 f=new Frame("My-Frame!"); f.setBounds(300,200,700,400); f.setLayout(new FlowLayout()); //设置框架要素 text=new TextField(30); b=new Button("My-Button!"); area=new TextArea(20,50); //框架添加组件 f.add(text); f.add(b); f.add(area); //事件监听 myEvent(); //设置显示框架 f.setVisible(true); } private void myEvent() { f.addWindowListener(new WindowAdapter() { //框架关闭监听 public void windowClosing(WindowEvent e) { System.exit(0); } }); b.addActionListener(new ActionListener() { //按钮动作监听 public void actionPerformed(ActionEvent e) { showDir(); } }); text.addKeyListener(new KeyAdapter() { //文本框键盘监听 public void keyPressed(KeyEvent e) { if (e.getKeyCode()==KeyEvent.VK_ENTER) showDir(); } }); } public void logEvent() { //局部事件监听 log.addWindowListener(new WindowAdapter() { //对话框关闭监听 public void windowClosing(WindowEvent e) { log.setVisible(false); } }); logb.addActionListener(new ActionListener() { //对话框按钮动作监听 public void actionPerformed(ActionEvent e) { log.setVisible(false); } }); } private void showDir() { //在文本域显示列表 area.setText(""); String dir=text.getText(); File file=new File(dir); if (file.exists() && file.isDirectory()) { String[] name=file.list(); for (String s : name) { area.append(s+"\r\n"); } } else { //Dialog对话框只有在发生错误时才创建对象 log=new Dialog(f,"提示对话框",true); log.setBounds(500,400,300,200); log.setLayout(new FlowLayout()); lab=new Label("路径-->"+dir+" 错误,请检查路径是否正确,然后重试! "); logb=new Button("OK!"); log.add(lab); log.add(logb); logEvent(); log.setVisible(true); } } public static void main(String [] args) { //主函数 new Demo(); }}
五:GUI中的菜单及其示例
GUI中的菜单是给图形界面添加菜单内容。
菜单组件: MenuComponent ,继承自 Object 。
它有两个子类: MenuBar 和 MenuItem 。
Menu 是 MenuItem 的子类。
简单关系就是:
MenuBar包含Menu,Menu包含MenuItem。
Menu也可以包含Menu。
这里不详细介绍它们的方法,仅通过示例演示其方法的使用。
举例:对框架添加一个菜单列表。
import java.awt.*;import java.awt.event.*;class Demo { private Frame f; private MenuBar bar; //定义一个菜单栏 private Menu menu; //定义一个菜单 private MenuItem item; //定义一个菜单列表 Demo() { init(); } public void init() { f=new Frame("My-Frame!"); f.setBounds(300,100,700,400); f.setLayout(new FlowLayout()); bar=new MenuBar(); //创建菜单栏对象 menu=new Menu("选项"); //创建指定名字的菜单 item=new MenuItem("退出"); //创建指定名字的菜单列表 menu.add(item); //将菜单列表添加进菜单 bar.add(menu); //将菜单添加进菜单栏 f.setMenuBar(bar); //设置框架的菜单栏 myEvent(); //设置简单的监听事件 f.setVisible(true); } public void myEvent() { f.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); item.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.exit(0); } }); } public static void main(String [] args) { new Demo(); }}
GUI工具编写一个简单的记事本程序。
该记事本程序包含简单的菜单:文件、编辑、工具、帮助。
打开文件时可以显示在文本域,可以保存对文件的修改,并可以双击执行。
package myNote;import java.awt.*;import java.awt.event.*;import java.io.*;class MyNote{ private Frame f; private MenuBar bar; private Menu fileM,createM,toolM,helpM; private MenuItem openMI,saveMI,quitMI,fileMI,infoMI,webMI; private TextArea area; private FileDialog openLog,saveLog; private File file; MyNote() //构造函数,初始化对象 { init(); } public void init() //框架封装成函数 { f=new Frame("My-Frame!"); f.setBounds(300,100,700,500); //设置菜单 bar=new MenuBar(); fileM=new Menu("文件"); createM=new Menu("新建"); toolM=new Menu("工具"); helpM=new Menu("帮助"); openMI=new MenuItem("打开"); saveMI=new MenuItem("保存"); quitMI=new MenuItem("退出"); fileMI=new MenuItem("文件"); infoMI=new MenuItem("信息"); webMI=new MenuItem("网站"); //设置菜单结构 f.setMenuBar(bar); bar.add(fileM); bar.add(toolM); bar.add(helpM); fileM.add(createM); fileM.add(openMI); fileM.add(saveMI); fileM.add(quitMI); createM.add(fileMI); toolM.add(infoMI); helpM.add(webMI); area=new TextArea(); f.add(area); //以边界布局形式将文本域填充整个框架 myEvent(); f.setVisible(true); openLog=new FileDialog(f,"打开文件",FileDialog.LOAD); //设置打开文件对话框 saveLog=new FileDialog(f,"保存文件",FileDialog.SAVE); //设置保存文件对话框 } public void myEvent() //将多个事件监听封装成函数 { f.addWindowListener(new WindowAdapter() { //框架关闭按钮事件 public void windowClosing(WindowEvent e) { System.exit(0); } }); openMI.addActionListener(new ActionListener() { //打开菜单事件 public void actionPerformed(ActionEvent ae) { openLog.setVisible(true); String dir=openLog.getDirectory(); String name=openLog.getFile(); if (dir==null || name==null) return; area.setText(""); file=new File(dir,name); BufferedReader br=null; try { br=new BufferedReader(new FileReader(file)); String line=null; while ((line=br.readLine())!=null) { area.append(line+"\r\n"); } } catch (IOException e) { throw new RuntimeException("读取失败"); } finally { if (br!=null) try { br.close(); } catch (IOException e) { throw new RuntimeException("读取关闭失败"); } } } }); saveMI.addActionListener(new ActionListener() { //保存菜单事件 public void actionPerformed(ActionEvent ae) { if (file==null) { saveLog.setVisible(true); String dir=saveLog.getDirectory(); String name=saveLog.getFile(); if (dir==null || name==null) return; file=new File(dir,name); } BufferedWriter bw=null; try { bw=new BufferedWriter(new FileWriter(file)); bw.write(area.getText()); } catch (IOException e) { throw new RuntimeException("写入失败"); } finally { if (bw!=null) try { bw.close(); } catch (IOException e) { throw new RuntimeException("写入关闭失败"); } } } }); quitMI.addActionListener(new ActionListener() { //退出菜单事件 public void actionPerformed(ActionEvent e) { System.exit(0); } }); } public static void main(String [] args) //这是主函数 { new MyNote(); }}
注意:
此类可以通过jar命令压缩成可执行的jar文件。需要手动指定主函数。
- 黑马程序员——学习日记之--GUI图形界面学习总结
- 黑马程序员——学习日记之GUI
- 黑马程序员——学习日记之--多线程学习总结
- 黑马程序员——Java学习笔记之⑥——“图形界面GUI”
- 黑马程序员————学习日记【12】 【GUI】
- 黑马程序员——学习日记之--面向对象总结
- 黑马程序员——学习日记之--正则表达式总结
- 黑马程序员--Java学习日记之GUI&网络编程
- 黑马程序员-学习日记17( GUI )
- 黑马程序员——黑马学习日志之十六 GUI
- 黑马程序员——基础学习(十四)多线程(Thread)和图形界面编程(GUI)
- 黑马程序员——java图形界面GUI
- 黑马程序员——学习日记之--IO流学习总结
- 黑马程序员--.Net学习日记——HTML总结
- 黑马程序员--.Net学习日记——HTML总结
- 黑马程序员------Java基础学习------用户图形界面GUI
- 黑马程序员---(学习日记——GUI及IO)流设计实现记事本程序
- 黑马程序员——学习日记之--集合框架知识总结
- IOS如何让App运行的时候不进入锁屏状态
- 关于Oracle数据库如何查询非当前用户名命名空间下的表不需要写ower直接from 表名
- ORA-00257 归档程序错误,在释放之前仅限于内部连接
- 谈 C++ 中的 new/delete 和 new[]/delete[]
- android ScrollView嵌套Listview
- 黑马程序员——学习日记之--GUI图形界面学习总结
- UML类图几种关系的总结
- Handler Runnable与Thread的区别详解
- AngularJS中的Promise
- Linux中7个用来浏览网页和下载文件的命令
- 使用 QEMU 进行嵌入式系统开发
- Android开发—随笔
- [Java]集合架构分析
- 延迟初始化 beans