---------------------- ASP.Net+Unity开发、.Net培训、期待与您交流! --------------------
1、GUI概述
GUI(图形用户界面)
Graphical UserInterface(图形用户接口)。用图形的方式,来显示计算机操作的界面,这样更方便更直观。
CLI:Command Line User Interface(命令行用户接口),就是常见的dos命令行操作。需要记忆一些常用的命令,操作不直观。
Java为GUI提供的对象都存在java.awt和javax.swing两个包中。
Awt与Swing
java.Awt:Abstract Window ToolKit(抽象窗口工具包),需要调用本地系统方法实现功能,属于重量级控件。
javax.Swing:在AWT的基础上,简历的一套图形界面系统,其中提供了更多的组件,而且完全有Java实现。增强了移植性,属轻量级控件。
轻量级与重量级的区别?
主要是与系统组件的关联大不大,需要调用本地系统方法去实现功能的属于重量级控件;直接调用自身功能的组件属于轻量级控件,与系统平台依耐性不大,增强了可移植性。
继承关系图
Component:构件、组件
--|Button:按钮
--|Label:标签
--|Checkbox:复选框
--|TextComponent:文本组件
--|TextArea:单行文本
--|TextField:多行文本
--|Container:容器
--|Window:窗口
--|Frame:框架
--|Dialog:对话框
--|FileDialog:文件对话框
--|Panel:面板
Container为容器,是一个特殊的组件,该组件中可以通过add方法添加其他组件进来。
2、GUI-布局
布局管理器
容器中组件的排放方式,就是布局。
常见的布局管理器:
FlowLayout(流式布局管理器):从左到右的顺序排列,Panel默认的布局管理器。
BorderLayout(边界式布局管理器):东,南,西,北,中,Frame默认的布局管理器。
GridLayout(网格布局管理器):规则的矩阵
CardLayout(卡片布局管理器):选项卡
GridBagLayout(网格包布局管理器):非规则的矩阵。
3、GUI-Frame
事件监听机制组成
事件源(组件)、时间(Event)、监听器(Listener)、时间处理(引发事件后处理方式)
WindowAdapter:接收窗口事件的抽象适配器类,此类中的方法为空,此类存在的目的是方便创建监听器对象。
创建图形化界面:
1.创建frame窗体。
2.对窗体进行基本设置,比如大小,位置,布局等。
3.定义组件。
4.将组件通过窗体的add方法添加到窗体中。
5.让窗体显示,通过setVisible(true)实现
4、GUI-事件监听机制
事件监听机制的特点:
1.事件源;
2.事件;
3.监听器;
4.事件处理。
事件源:就是awt包或者swing包中的那些图形界面组件。
事件:每一个事件源都有自己特有的对应事件和共性事件。
监听器:将可以出发某一个事件的动作(不止一个动作)都已经封装到了监听器中。
以上三者,在java中已经定义好了,直接获取其对象来用就可以了。我们要做得事情就是对产生的动作进行处理。
WindowAdapter 接收窗口事件的抽象适配器类,此类中的方法为空,此类存在的目的是方便创建监听器对象。
- import java.awt.*;
- //必须导入事件包
- import java.awt.event.*;
- class AwtDemo1 {
- public static void main(String[] args) {
- Frame f = new Frame("测试窗体");//创建窗体
- f.setSize(500,400);//设置窗体大小
- f.setLocation(300,200);//设置窗体位置
- f.setLayout(new FlowLayout());//设置窗体布局
- Button b = new Button("提交");
- f.add(b);
- //添加监听器
- f.addWindowListener(newWindowAdapter(){
- public void windowClosing(WindowEvent e){
- System.exit(0);
- }
- public void windowActivated(WindowEvent e){
- System.out.println("我活了");
- }
- public void windowOpened(WindowEvent e){
- System.out.println("我被打开了,哈哈");
- }
- });
- f.setVisible(true);
- }
- }
- /*
- class MyWinimplements WindowListener
- {
- //覆盖7个方法,可是只需用到关闭动作,其他动作用不到,但却必须复写
- }
- */
- //因为windowListener的子类WindowAdapter已经实现了WindowListener接口。
- //并覆盖了其中的所有方法,那么我只要继承自windowAdapter覆盖我需要的方法即可。
- class MyWin extends WindowAdapter{ //继承WindowAdapter
- public void windowClosing(WindowEvent e){
- //System.out.println("windowclosing-----"+e.toString());
- System.exit(0);//退出程序
- }
- }
5、GUI—Action事件
将事件与图形化界面分离出来
void setBounds(intx, int y, int width, int height) 移动组件并调整其大小
- import java.awt.*;
- importjava.awt.event.*;
- class FrameDemo{
- //定义该图形中所需的组件的引用,方便整个作用域
- private Frame f;
- private Button but;
- FrameDemo(){
- init();
- }
- public void init(){
- f = new Frame("my frame");
- //对frame进行基本设置。
- f.setBounds(300,100,600,500);//x,y,w,l
- f.setLayout(new FlowLayout());//设置窗体布局方式
- but = new Button("mybutton");
- //将组件添加到frame中
- f.add(but);
- //加载窗体上的事件
- myEvent();
- //显示窗体
- f.setVisible(true);
- }
- private void myEvent(){
- f.addWindowListener(new WindowAdapter(){
- public void windowClosing(WindowEvent e){
- System.exit(0);
- }
- });
- /*
- 让按钮具备退出程序的功能
- 按钮就是事件源,那么选择哪个监听器呢?
- 通过关闭窗体事例了解到,想要知道哪个组件具备什么样的特有监听器,需要
- 查看该组件对象的功能。
- 通过查阅button的描述,发现按钮支持一个特有监听addActionListener。
- */
- but.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent e){
- System.out.println("退出,我干的");
- System.exit(0);
- }
- });
- }
- public static void main(String[] args) {
- new FrameDemo();
- }
- }
6、GUI—鼠标键盘事件
consume:使用此事件,以便不会按照默认的方式由产生此事件的源代码来处理此事件。
booleanisControlDown() :返回Control修饰符在此事件上时为down,即ctrl+组合键,判断ctrl键是否被按下
代码演示:
- import java.awt.*;
- importjava.awt.event.*;
- classMouseAndKeyEvent {
- private Frame f;
- private Button but;
- private TextField tf;
- MouseAndKeyEvent(){
- init();
- }
- public void init(){
- f = new Frame("myframe");
- f.setBounds(300,100,400,300);
- f.setLayout(new FlowLayout());
- tf = new TextField(20);
- but = new Button("mybutton");
- f.add(tf);
- f.add(but);
- 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(KeyEvente){
- int code =e.getKeyCode();
- if(!(code>=KeyEvent.VK_0&& code<=KeyEvent.VK_9)){
- System.out.println(KeyEvent.getKeyText(e.getKeyCode())+"..."+"是非法字符");
- e.consume();
- }
- }
- });
- //给but添加一个键盘监听
- but.addKeyListener(new KeyAdapter(){
- public void keyPressed(KeyEvent e){
- if(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("actionok");
- }
- });
- 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();
- }
- }
7、练习:列出制定目录内容
void append(Stringstr) :将给定文本追加到文本区的当前文本。
Dialog(Dialogowner, String title, boolean modal): 构造一个最初不可见的Dialog,它带有指定的所有者Dialog、标题和模式。
代码演示:
- import java.awt.*;
- import java.awt.event.*;
- import java.io.*;
- class MyWindowDemo{
- private Frame f;
- private TextField tf;
- private Button but;
- private TextArea ta;//文本区域rows:行,columns:列
- private Dialog d;
- private Label lab;//对话框里面不能直接创建文本
- private Button okBut;
- MyWindowDemo(){
- init();
- }
- public void init(){
- f = new Frame("mywindow");
- 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);//如果模式为true,则只能操作当前窗口;反之则均可操作。
- d.setBounds(400,200,300,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);
- }
- });
- <span style="white-space:pre"> </span>//发现一点不足的是,在弹出的对话框中可以再次使用键盘监听机制使其直接利用键盘让对话框消失。
- <span style="white-space:pre"> </span> okBut.addKeyListener(new KeyAdapter(){
- public void keyPressed(KeyEvent e){
- if(e.getKeyCode()==KeyEvent.VK_ENTER)
- 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){
- /*
- String text =tf.getText();
- ta.setText(text);//将输入框中(TextField)的文字添加到输入文本框中(TextArea)
- //System.out.println(text);
- tf.setText("");//清空输入框中(TextField)的内容
- */
- showDir();
- }
- });
- f.addWindowListener(new WindowAdapter(){
- public void windowClosing(WindowEvente){
- 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+"有误,请重新输入";
- <span style="white-space:pre"> </span>//将文本添加到对话框中
- lab.setText(info);
- d.setVisible(true);
- }
- }
- public static void main(String[] args) {
- new MyWindowDemo();
- }
- }
8、GUI—菜单
public classFileDailog extends Dialog :FileDialog类显示一个对话框窗口,用户可以从中选择文件。由于它是一个模式对话框,当应用程序调用其show方法来显示对话框时,它将阻塞其余应用程序,知道用户选择一个文件。
publicFileDialog(Frame parent,String title,int node) :创建一个具有指定标题的文件对话框窗口,用于加载或保存文件。如果node的值为LOAD,那么文件对话框将查找药读取的文件,所显示的文件时当前目录中的文件。如果node的值为SAVE,则文件对话框将查找药写入文件的位置。
代码演示:
- import java.awt.*;
- importjava.awt.event.*;
- import java.io.*;
- class MyMenuDemo1{
- private Frame f;
- private MenuBar bar;
- private TextArea ta;
- private Menu fileMenu;
- private MenuItem openItem,saveItem,closeItem;
- private FileDialog openDia,saveDia;
- private File file;
- MyMenuDemo1(){
- init();
- }
- public void init(){
- f = new Frame("mywindow");
- f.setBounds(300,100,500,500);
- //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(){
- public void actionPerformed(ActionEvent ex){
- if(file==null){
- saveDia.setVisible(true);
- StringdirPath = saveDia.getDirectory();
- StringfileName = 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 e){
- throw new RuntimeException("写入文件失败");
- }
- }
- });
- //打开文件
- openItem.addActionListener(new ActionListener(){
- public 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 ex){
- throw new RuntimeException("文件读取失败");
- }
- }
- });
- //退出程序
- 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 MyMenuDemo1();
- }
- }
9、如何制作jar包
1、javac -d C:\myclass MyMenu.java
javac -d 路径程序名.java
2、进入路径myclass后
jar -cvfmymenu.jar mymenu
jar -cvf 需要命名的jar包 包名
3、如何给jar包写入相关信息?
1.任意创建一个文本文件,然后写入Main-Class: mymenu.MyMenuTest
大写Main-Class: (换行)包名.程序名(换行)
2.在DOS状态下输入
jar -cvfm my.jar1.txt mymenu
jar -cvfm jar文件需要操作的文件 包名
jar在DOS命令下的操作:
-c 创建新的归档文件
-t 列出归档目录
-x 从档案中提取指定的 (或所有) 文件
-u 更新现有的归档文件
-v 在标准输出中生成详细输出
-f 指定归档文件名
-m 包含指定清单文件中的清单信息
-e 为捆绑到可执行 jar文件的独立应用程序指定应用程序入口点
-0 仅存储; 不使用情况任何 ZIP 压缩
-M 不创建条目的清单文件
-i 为指定的 jar 文件生成索引信息
-C 更改为指定的目录并包含其中的文件
小结:在给jar包添加配置信息的时候,Main-Class:后一定需要加空格,其次就是包名.类名后一定记得换行,否则程序无法识别
---------------------- ASP.Net+Unity开发、.Net培训、期待与您交流! --------------------