GUI(图形用户界面)

来源:互联网 发布:程序员都有什么app 编辑:程序博客网 时间:2024/05/17 04:17

 

GUI(图形用户界面)

Gui  Graphical User Interface(图形用户接口)

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

 CLI command line user interface(命令行用户接口)

就是常见的dos命令行操作

需要记忆一些常用的命令,操作补直观

举例比如创建文件夹,或删除文件夹等

java为GUI提供的对象都存在java.AWT和javax.Swing两个包中。AWT和Swing SWT

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

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

GUI中继承关系图:

Container:为容器,是一个特殊的组件,该组件中可以通过add方法添加其他组件进来。

Checkbox,复选框

TextComponent 文本框

TextArea 文本区域

Container容器的意思

Window 窗口

Frame框架窗体 Dialog 对话框-> FileDialog文件对话框

Panel 面板

布局管理器

 容器中的组件的排放方式,就是布局

常见的布局管理器:

FlowLayout(流式布局管理器) : 从左到右的顺序排序, Panel默认的布局管理器

BorderLayout(边界布局管理器):东南西北中

Frame:默认的布局管理器

GridLayout(网格布局管理器): 规则的矩阵

GardLayout(卡片布局管理器):选项卡

GridBagLayout(网格包布局管理器) :非规则的矩阵

事件监听机制组成:事件源(组件)、事件(Event)、监听器(Listener)、事件处理(引发事件后处理方式。

事件监听机制流程图:

用户对组件的操作,就是一个事件,那么产生事件的组件就是事件源。接收并处理事件,与用户进行交互的行为就是事件处理器。这些处理方式都封装在监听器中。

就如同开密码锁,为了安全,密码锁上安装了报警装置,只要锁被砸就会把锁事件通知保安。那么保安就有相应的处理方式。如果锁没有被砸,而是密码输入错误,那么报警装置也会将锁事件通知保安,保安也会处理方式。 

那么锁就是事件源,报警装置就是监听器,可以用来监听引发事件的动作。但必须要注册到锁上,否则锁被砸保安是不知道的。对于每一种动作都有不同的处理方式。

Container 常用子类:Window Panel(面板,不能单独存在)
 Window常用子类:Frame Dialog
 创建图形化界面
1,创建Frame窗体
2,对窗体进行基本设置 比如大小 ,位置,布局
3,定义组件
4,将组件通过窗体的add方法添加到窗体中
5,让窗体显示,通过setVisible(true);
事件监听机制的特点:
1,事件源(组件)
2,事件(Event)

3,监听器(Listener)
4,事件处理(引发事件后处理方式)
事件源:就是AWT包或Swing包中的那些图形界面组件
事件:每一个事件源都有自己特有的对应事件和共性事件

监听器:将可以触发某一个事件的动作(不止一个动作)都已经封装到了监听器中
以上三者,在java中都已经定义好了,直接获取其对象来用就可以了。我们要做的事件是,就是对产生的动作进行处理。 

import java.awt.*;import java.awt.event.*;public class AwtDemo {public static void main(String[] args) {            Frame  f=new Frame("my awt");//默认是边界布局,              f.setSize(500,100);//设置窗体大小              f.setLocation(300,200);//设置窗体出现在屏幕的位置。              f.setLayout(new FlowLayout());            //f.setVisible(true);            Button b=new Button("我是一个按钮");            f.add(b);            f.addWindowListener(new MyWin());//添加指定窗口监听器,以从此窗口中等同于下句。              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("我被打开了,哈哈哈哈哈");         } });      f.setVisible(true);  }}/*class MyWin implements WindowListener      {//覆盖7个方法,可是我只用到了关闭的动作,其他动作都没有用到,可是却必须复写。       }*/     //因为windowlistener的子类windowAdapter已经实现了windowlistener接口     //并覆盖了其中的所有方法,那么我只能继承自windowadpter覆盖我需要的方法即可class MyWin extends WindowAdapter{     public void windowClosing(WindowEvent e)      { System.exit(0); System.out.println("window closing----"+e.toString());      }}/*interface Lis     {      void close();     }abstract class WinLis implements Lis//创建对象没意义,抽象类中没有一个方法,    {     public void close(){}   }*/

Frame示例:

import java.awt.*;import java.awt.event.*;public class FrameDemo {//定义该图形中所需的组件的引用,private Frame f;private Button but;private TextField tf;FrameDemo(){init();}public void init(){f=new Frame("my frame");//对frame进行基本设置f.setBounds(300, 200, 500, 400);//移动组件并调整其大小。f.setLayout(new FlowLayout());tf=new TextField(20);but=new Button("my button");//将组件添加到frame中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);       }         });//让按钮具备退出程序的功能/*按钮就是事件源 * 那么选择那个监听器呢? *通过关闭窗体示例了解到,想要知道哪个组件具备什么样的特有监听器 *需要查看该组件对象的功能。 *通过查阅Button的描述,发现按钮支持一个特有监听addActionListener *ActionListener没有适配器。只要方法超过三个的都有适配器。 **//*but.addActionListener(new ActionListener()   {public void actionPerformed(ActionEvent e){System.out.println("退出,按钮干的");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){System.out.println(e.getKeyChar()+"..."+e.getKeyCode());                            //if(e.getKeyCode()==KeyEvent.VK_ENTER/*VK_ESCAPE*/)if(e.isControlDown()&&e.getKeyCode()==KeyEvent.VK_ENTER)//组合键 ctrl+回车System.exit(0);//System.out.println(KeyEvent.getKeyText(e.getKeyCode())+"..."+e.getKeyCode());}});}public static void main(String[] args) {  new FrameDemo();}}

鼠标键盘事件

import java.awt.*;import java.awt.event.*;public class MouseAndKeyEvent {/** * void consume()使用此事件,以便不会按照默认的方式由产生此事件的源代码来处理此事件。 * @param args */private Frame f;private Button but;private TextField tf;MouseAndKeyEvent(){init();}public void init(){f=new Frame("my frame");//对frame进行基本设置f.setBounds(300, 200, 500, 400);f.setLayout(new FlowLayout());tf=new TextField(20);but=new Button("my button");//将组件添加到frame中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.getKeyCode()==KeyEvent.VK_ESCAPE)if(e.isControlDown()&&e.getKeyCode()==KeyEvent.VK_ENTER)//组合键System.out.println("ctrl+enter is run");System.exit(0);System.out.println(e.getKeyChar()+"......."+KeyEvent.getKeyText(e.getKeyCode()));}});       /*but.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){System.out.println("action to");}});*/but.addMouseListener(new MouseAdapter()//添加指定的鼠标侦听器,以接收发自此组件的鼠标移动事件。{  private int clickCount=1;private  int count=1;public void mouseEntered(MouseEvent e)//鼠标进入到组件上时调用。{System.out.println("欢迎来到组件事件"+count++);}public void mouseClicked(MouseEvent e){ if(e.getClickCount()==2)System.out.println("双点击动作"+clickCount++);                           //System.out.println("点击动作"+clickCount++);}});}public static void main(String[] args) {new  MouseAndKeyEvent();}}

练习:

在文本框中输入目录,点击转到按钮,将该目录中的文件与文件夹名称列在下面的文本区域中。

例图:

import java.awt.*;import java.awt.event.*;import java.io.File;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, 500, 600);f.setLayout(new FlowLayout());tf=new TextField(30);but=new Button("转到");ta=new TextArea(25,70);d=new Dialog(f,"提示信息--self",true);//d本身是一个窗体。lab=new Label();okBut=new Button("确定");d.setBounds(400,300,400,100);d.setLayout(new FlowLayout());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");//将给定文本追加到文本区的当前文本。                                             //ta.setText(name+"\r\n");  }  }  else  {        String info="您输入的信息:"+dirPath+"是错误的,请重输";           lab.setText(info);  d.setVisible(true);  }                           //ta.setText("你错误了");/*String text=tf.getText(); ta.setText(text);*//*tf.setText("");System.out.println(text);*/}public static void main(String[] args) {new MyWindowDemo();}}


记事本

import java.awt.*;import java.awt.event.*;import java.io.*;public class MyMenuDemo {/**MenuBar类封装绑定到框架的菜单栏的平台概念,为了将该菜单栏与Frame对象关联,可以调用该框架的setMenuBar方法。 *打开文件,保存文件。 *FileDialog类显示一个对话框窗口,用户可以从中选择文件。 *public FileDialog(Frame parent,String title,int mode) *mode-对话框的模式,可以是FileDialog.LOAD打开或FileDialog.SAVE保存 * @param args */private Frame f;private MenuBar bar;private Menu FileMenu,subMenu;private MenuItem closeItem,subItem,openItem,saveItem;private FileDialog openDia,savDia;private TextArea ta;private File file;MyMenuDemo(){init();}public void init(){f=new Frame("my menu");f.setBounds(300,200,650,600);                  //f.setLayout(new FlowLayout());bar=new MenuBar();ta=new TextArea();FileMenu=new Menu("文件");subMenu=new Menu("子菜单");subItem=new MenuItem("子条目");closeItem=new MenuItem("退出");openItem=new MenuItem("打开");saveItem=new MenuItem("保存");FileMenu.add(openItem);FileMenu.add(saveItem);FileMenu.add(closeItem);FileMenu.add(subMenu);subMenu.add(subItem);bar.add(FileMenu);f.setMenuBar(bar);openDia=new FileDialog(f,"我要打开",FileDialog.LOAD);savDia= new FileDialog(f,"我要保存",FileDialog.SAVE);f.add(ta);myEvent();f.setVisible(true);}private void myEvent(){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 e2) {throw new RuntimeException("读取失败");}}});saveItem.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){if(file==null){savDia.setVisible(true);  String dirPath=savDia.getDirectory();  String fileName=savDia.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();}}});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();}}

《《《《————————————————————》》》》

打jar包

javac mymenudemo.java

javac d c:\myclass mymenutest.java

 c:

cd\

cd myclass

jar -cvf  my.jar mymenu

 jar-cvfm my.jar 1.txt mymenu
《《《《———————————————————》》》》

 

       ———加油!濛濛在努力中。。。———      濛濛

 

原创粉丝点击