黑马程序员-基础-GUI图形用户界面

来源:互联网 发布:程序员专用壁纸 编辑:程序博客网 时间:2024/06/04 18:31

------- android培训、java培训、期待与您交流! ----------

第一节GUI概念

第一:概念

计算机和用户交互的形式:GUICLI

1:GUI:Graphical User Interface(图形用户接口)

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

2:CLI:Command line User Interface(命令行用户接口)
就是常见的Dos命令行操作。需要记忆一些常见的命令,操作不直观

举例:比如:创建文件夹,或者删除文件夹等
java为GUI提供的对象都存在java.Awt和javax.String两个包中
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窗体。
Frame f=new Frame("my awt");//可设置标题,my awt就是标题
2,对窗体进行基本设置。
比如大小,位置,布局。
f.setSize(400, 400);//设置组件的长500,高400
f.setLocation(300, 200);//设置窗口组件的在屏幕的位置长200.高300
f.setLayout(new FlowLayout());//设置布局是流式布局管理,不设置的话,Frame窗体默认是边界布局管理器
3,定义组件。
Button b=new Button("我是一个按钮");//可设置组件名称。如:我是一个按钮

4,将组件通过窗体的add方法添加到窗体中。
f.add(b);
5,让窗体显示,通过setVisible(true)//true显示组件窗体,false不显示组件窗体

代码事例如下:
*/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.addWindowListener(new WindowAdapter(){public void windowClosing(WindowEvent e){System.out.println("我关");System.exit(0);}public void windowActivated(WindowEvent e) {System.out.println("我活了。");}public void windowOpened(WindowEvent e) {System.out.println("我被打开了,hahahhahah");}});f.setVisible(true);//System.out.println("Hello World!");}}/*class MyWin implements WindowListener{//覆盖7个方法。可以我只用到了关闭的动作。//其他动作都没有用到,可是却必须复写。}//因为WindowListener的子类WindowAdapter已经实现了WindowListener接口。//并覆盖了其中的所有方法。那么我只要继承自Windowadapter覆盖我需要的方法即可。class MyWin extends WindowAdapter{public void windowClosing(WindowEvent e){//System.out.println("window closing---"+e.toString());System.exit(0);}}*/


第五:事件监听机制组成
1,事件源(组件)就是awt包或者swing包中的那些图形界面组件
2,事件(Event)每一个事件源都有自己特有的对应和共性事件
3,监听器(Listener)将可以触发某一个事件的动作(不止一个动作)都已经封装到了监听器中。
事件处理(引发事件后处理方式)

以上三者,在java中都已经定义好了。直接获取其对象来用就可以了。
我们要做的事情是,就是对产生的动作进行处理。
步骤:
1,明确监听器(Frame)将监听器注册到上面,通过方法addWindowListener(WindowListener w)
2,注意:若用子类实现WindowListener接口,就需要覆盖七个方法,可是只用到其中一个关闭方法,其他方法未用到,就必须重写全部。所以可以用WindowListener的子类
WindowAdapter实现接口,覆盖需要的方法就可以。
3,明确事件,进行处理。其实就是添加什么监听器就要添加什么事件。
事例:鼠标和键盘事件
import java.awt.*;import java.awt.event.*;class MouseAndKeyEvent {private Frame f;private Button but;private TextField tf;MouseAndKeyEvent(){init();}public void init(){f = new Frame("my frame");f.setBounds(300,100,600,500);f.setLayout(new FlowLayout());tf = new TextField(20);but = new Button("my button");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(KeyEvent e){int code = e.getKeyCode();if(!(code>=KeyEvent.VK_0 && code<=KeyEvent.VK_9)){System.out.println(code+".....是非法的");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("action 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();}}

第二节GUI应用

第一:对话框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;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();}}

第二:菜单Menu


MenuBar 菜单栏:可以添加菜单和条目
Menu: 菜单,有右三角图标存在,可以添加Menu和MenuItem
MenuItem 条目,无右三角图标存在是最终的菜单项
先创建菜单栏,再创建菜单,每一个菜单中建立菜单条目
也可以菜单添加到菜单中,作为子菜单。
通过setMenuBar()方法,将菜单栏添加到Frame中。
事例如下
package day22;import java.awt.*;import java.awt.event.*;public class MyMenuDemo {/** * @param args */private Frame f;private MenuBar mb;private Menu m,subMenu;private MenuItem closeItem,subItem;MyMenuDemo(){init();}private 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("编程");closeItem=new MenuItem("退出");subItem=new MenuItem("方法");subMenu.add(subItem);//subMenu.add(closeItem);m.add(subItem);m.add(closeItem);mb.add(m);mb.add(subMenu);f.setMenuBar(mb);myEvent();f.setVisible(true);}public 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) {// TODO Auto-generated method stubnew MyMenuDemo();}}


打开文件保存文件jar包双击打开方法代码事例如下:(具体操作可查看22天视频最后三篇)
package mymenu;import java.awt.*;import java.awt.event.*;import java.io.*;public class MyMenuTest{private Frame f;private MenuBar bar;private TextArea ta;private Menu fileMenu;private MenuItem openItem,saveItem,closeItem;private FileDialog openDia,saveDia;private File file;MyMenuTest(){init();}public void init(){f = new Frame("my window");f.setBounds(300,100,650,600);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 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.flush();bufw.close();}catch (IOException ex){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 MyMenuTest();}}/*如何制作可以双击执行的jar包呢?1,将多个类封装到了一个包(package)中。2,定义一个jar包的配置信息。定义一个文件a.txt 。文件内容内容为:Main-Class:(空格)包名.类名(回车)3,打jar包。jar -cvfm my.jar a.txt 包名4,通过winrar程序进行验证,查看该jar的配置文件中是否有自定义的配置信息。5,通过工具--文件夹选项--文件类型--jar类型文件,通过高级,定义该jar类型文件的打开动作的关联程序。jdk\bin\javaw.exe -jar6,双击试试!。哦了。*/

jar包如何才能双击执行?

既然是图形化界面,就需要通过图形化界面的形式运行程序,而不是是用Dos命令行执行,那么如何通过双击程序就执行程序呢?这就需要将程序的class文件打包,步骤如下:

1、首先要在java文件中导入一个包,没有则需创建一个包,如package mymenu;

2、生成包:通过编译javac -d c:\myclass MyMenu.java,此时则在c盘下的myclass文件夹下生成了所有的.class文件

3、在此目录下新建一个文件,如1.txt或者其他任意名称任意扩展名的文件都可,然后在其中编辑固定的格式:“Main-Class: mymenu.MenuDemo”,只写引号中的内容。需要需要在冒号后有一个空格,在文件末尾要回车。

4、编译:jar -cvfm my.jar 1.txt mymenu即可。如果想添加其他信息,则直接编译jar即可得出相应的命令

5、此时双击即可执行。

说明:

1)在固定格式中:

a.如果无空格:在编译的时候,就会报IO异常,提示无效的头字段,即invalid header field。这说明1.txt在被IO流读取。

b.如果无回车:在列表清单.MF中不会加入相应的加载主类的信息,也就是说配置清单的属性主类名称不会加载进清单中,也就不会执行。

2)jar文件必须在系统中注册,才能运行。注册方法如下:

A.对于XP系统:

   a.打开任意对话框,在菜单栏点击工具按钮,选择文件夹选项

   b.选择新建--->扩展名,将扩展名设置为jar,确定

   c.选择高级,可更改图标,然后点击新建,命名为open,

   d.在用于可执行应用程序中,点浏览,将jdk下的bin的整个文件路径添加进来,并在路径后添加-jar即可。

B.对于win7系统:

   a.改变打开方式:右击.jar文件,点击打开方式,选择默认程序为jdk下bin中的javaw.exe应用程序。

   b.修改关联程序的注册表:打开注册表(win+r),找到注册表路径\HKEY_CLASSES_ROOT\Aplications\javaw.exe\shell\open\command下的字符串值,右击点修改,将值改为:"C:\Program Files\Java\jre6\bin\javaw.exe" -jar "%1"其中-jar前的路径就是javaw.exe的路径。保存

   c.双击即可执行jar程序,如果仍不能执行,则需下载最新版的jdk。








原创粉丝点击