javaGUI

来源:互联网 发布:java multipartfile 编辑:程序博客网 时间:2024/05/21 06:36

------------- android培训、java培训、java博客、java学习型技术博客、期待与您交流! --------------

 




1: GUI(图形用户界面)        

 GUI

•Graphical User Interface(图形用户接口)。

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


CLI

•Command line User Interface (命令行用户接口)

•就是常见的Dos命令行操作。

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


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


2: Awt和Swing

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

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


3     继承关系图

             



4:  布局管理器      

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

常见的布局管理器:

FlowLayout(流式布局管理器)

                     从左到右的顺序排列。

                     Panel默认的布局管理器。

BorderLayout(边界布局管理器)

                     东,南,西,北,中

                     Frame默认的布局管理器

•GridLayout(网格布局管理器)

                     规则的矩阵

•CardLayout(卡片布局管理器)                 

                      CardLayout:卡片布局,面板重叠放置,只能看到一个,最先添加的会被显示出来,可以进行翻动
两种构造方法:
CardLayout() 
          创建一个间隙大小为 0 的新卡片布局。 
CardLayout(int hgap, int vgap) 
          创建一个具有指定的水平和垂直间隙的新卡片布局。 
          常用的方法:
previous(Container parent) 
          翻转到指定容器的前一张卡片。
          show(Container parent, String name) 
          翻转到已添加到此布局的具有指定 name 的组件
          next(Container parent) 
          翻转到指定容器的下一张卡片。
          first(Container parent) 
          翻转到容器的第一张卡片。
          last(Container parent) 
          翻转到容器的最后一张卡片。


•GridBagLayout(网格包布局管理器)

                    非规则的矩阵

                     GridBagLayout:增强版的网格布局,组件可以跨行跨列的进行布局。
          构造方法:
          GridBagLayout() 
          创建网格包布局管理器。
          注意:
          该布局管理器的具体实现需要借助GridBagConstraints类,利用GridBagConstraints类的属性对组件进行设置


5: 建立一个简单的窗体


Container常用子类:Window Panel(面板, 不能单独存在。)

Window常用子类:Frame Dialog

简单的窗体创建过程:

Frame f = new Frame(“my window”);f.setLayout(new FlowLayout());f.setSize(500,400);//设置窗体大小f.setLocation(300,200);//设置窗体出现在屏幕的位置f.setVisible(true);


6: 事件监听机制组成                 

事件源(组件)

事件(Event)

监听器(Listener)

事件处理(引发事件后处理方式)

7: 事件监听机制流程图




8: 事件监听机制

   

确定事件源(容器或组件)


通过事件源对象的addXXXListener()方法将侦听器注册到该 事件源上。


该方法中接收XXXListener的子类对象,或者XXXListener的 子类XXXAdapter的子类对象。


一般用匿名内部类来表示。


在覆盖方法的时候,方法的参数一般是XXXEvent类型的变量 接收。


事件触发后会把事件打包成对象传递给该变量。(其中包括 事件源对象。通过getSource()或者,getComponent()获 取。)



9: 菜单继承体系


10: 菜单

MenuBar,Menu,MenuItem

先创建菜单条,再创建菜单,每一个菜单 中建立菜单项。

也可以菜单添加到菜单中,作为子菜单。

通过setMenuBar()方法,将菜单添加到 Frame中。


11:   swing 实例:            

import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.event.*;class  SwingDemo{public static void main(String[] args) {JFrame f = new JFrame();f.setBounds(300,100,500,400);f.setLayout(new FlowLayout());JButton but = new JButton("我是一个按钮");f.add(but);f.addWindowListener(new WindowAdapter(){public void windowClosing(WindowEvent e){System.exit(0);}});f.setVisible(true);}}

12:   监听实例:

 

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();}}

   
13 菜单实例

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();}


------------- android培训、java培训、java博客、java学习型技术博客、期待与您交流! -------------

详情请查看:http://edu.csdn.net/heima/


原创粉丝点击