------- android培训、java培训、期待与您交流! ----------
GUI(用户图形界面)
概述
1、GUI:GraphicalUser Interface,即图形用户界面,用于计算机与用户交互的一种方式。
2、计算机与用户交互的两种方式:GUI和CLI
GUI: Graphical User Interface,图形用户接口,用图形方式,来显示计算机操作界面,方便直观。
CLI: Command LineUser Interface,命令行用户接口,即常见的Dos命令行操作,须记住一些命令,操作不直观。
3、java也将这种界面封装为对象,其中的对象都放在了两个包中:java.Awt包和javax.Swing包。
java.Awt包:Abstract Window Toolkit,即抽象窗口工具包。要调用本地系统方法实现功能,属重量级控件。
javax.Swing包:在AWT的基础上建立的一套图形界面系统,其中提供了更多的组件,且完全由java实现,增强了移植性,属轻量级控件。
继承关系图
Container:为容器,是一个特殊的组件,该组件中可以通过add方法添加其他组件进来。
Container常用子类:Window Panel(面板,不能单独存在。)
Window常用子类:Frame Dialog
三、布局管理器
1、布局:容器中的组件排列方式
2、常见的布局管理器:
1)FlowLayout:流式布局管理器。从左向右排列,是Panel默认的布局管理器
2)BorderLayout:边界式布局管理器,东南西北中的排列方式,是Frame的默认布局管理器。如果窗体中只有一个组件,将会覆盖整个窗体。
3)GridLayout:网格式布局管理器,规则的矩阵
4)CardLayout:卡片式布局管理器,即选项卡
5)GridBayLayout:网格包布局管理器,非规则矩阵
3、如果存在多种布局方式,如何创建窗体界面呢?步骤:
1)先将窗体Frame进行大区域的划分,设置其布局管理器,加入面板Panel
2)将组件加入Panel,设置面板的布局管理器。
简单的窗体创建过程
1、创建Frame窗体:
Frame f = new Frame("my Frame");//可设置标题,即窗体名字
2、对窗体进行基本设置:如大小,位置,布局等:
f.setSize(int wight,int hight);//窗体大小设置
f.setLocation(int x,int y);//窗体显示位置设置,横纵坐标
f.setBounds(int x,int y,int wight,int hight),也可以直接用这个方法对大小和位置设置
f.setLayout(Layout layout),参数为指定的布局管理器,如FlowLayout
3、定义组件:
如Button b = new Button(“my Button”);//可设置组件的名称
4、将组件通过窗体的add方法添加到窗体中:
f.add(b);//将按钮组件添加进窗体
5、让窗体显示:
f.setVisible(boolean b);//通过设置参数是true还是false是否显示窗体
示例:
[java] view plaincopyprint?<SPAN style="FONT-SIZE: 14px">import java.awt.*;
class AwtFirstDemo
{
public static void main(String[] args)
{
//创建Frame窗体:
Frame f = new Frame("my Frame");//可设置标题
//对窗体进行基本设置:如大小,位置,布局等:
f.setSize(500,100);//窗体大小设置
f.setLocation(300,150);//窗体显示位置设置,横纵坐标
// f.setBounds(int x,int y,int wight,int hight);//也可以直接用这个方法对大小和位置设置
f.setLayout(new FlowLayout());//参数为指定的布局管理器,如FlowLayout
//定义组件:
Button b = new Button("my Button");//可设置组件的名称
//将组件通过窗体的add方法添加到窗体中:
f.add(b);//将按钮组件添加进窗体
//让窗体显示:
f.setVisible(true);//通过设置参数是true还是false是否显示窗体
}
}
</SPAN>
import java.awt.*;
class AwtFirstDemo
{
public static void main(String[] args)
{
//创建Frame窗体:
Frame f = new Frame("my Frame");//可设置标题
//对窗体进行基本设置:如大小,位置,布局等:
f.setSize(500,100);//窗体大小设置
f.setLocation(300,150);//窗体显示位置设置,横纵坐标
// f.setBounds(int x,int y,int wight,int hight);//也可以直接用这个方法对大小和位置设置
f.setLayout(new FlowLayout());//参数为指定的布局管理器,如FlowLayout
//定义组件:
Button b = new Button("my Button");//可设置组件的名称
//将组件通过窗体的add方法添加到窗体中:
f.add(b);//将按钮组件添加进窗体
//让窗体显示:
f.setVisible(true);//通过设置参数是true还是false是否显示窗体
}
}
事件监听机制
1、组成:
1)事件源(组件):awt或swing包中的那些图形界面组件
2)事件(Event):每一个事件源都有自己特有的对应事件和共性事件
3)监听器(Listener):将可触发某一事件的动作(不只一个动作),都封装到侦听器中。
4)事件处理:引发事件后的处理方式。
2、使用说明
组成的前三个在java中都已将定义好了,直接获取其对象来用即可,我们需要做的就是对产生的动作进行处理。
步骤:
1)确定事件源(容器或组件)。通过事件源对象的addXXXListener()方法将监听器注册到该事件源上。该方法中接收XXXListener的子类对象,或者XXXListener的子类XXXAdapter的子类对象。
2)一般用匿名内部类来表示。在覆盖方法的时候,方法的参数一般是XXXEvent类型的变量接收。
如:
[java] view plaincopyprint?f.addWindowlistener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);//表示关闭窗口
}
});
f.addWindowlistener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);//表示关闭窗口
}
});
说明:
1)事件触发后会把事件打包成对象传递给复写方法中参数的变量。(其中包括事件源对象。通过getSource()或者,getComponent()获取。)
2)若用子类实现WindowListener接口,就需要覆盖其中的7个方法,可只用到其中的关闭动作,其他动作未用到,但却必须重写全部。因为WindowLister的子类WindowAdapter(适配器)已经实现此接口,并覆盖了其中所有方法。那么只需继承WindowAdapter,覆盖需要的方法即可。
3)明确事件,并对事件进行处理,其实,添加什么监听器就需要添加什么事件。
示例:
import java.awt*;import java.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,400,500);设置坐标长宽
f.setLayout(new FlowLayout());设置布局方式,避免按钮很大。
but=new Button(“my button”);//初始化
f.add(but);将组建添加到frame中
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){sop(“tuichu”);System.exit();}
})
}main(){}
鼠标事件:
but.addMouseListener(new MouseAdapter(){
private intcount=1;
public voidmouseEntered(MouseEvent e){//复写方法,里面接受的是MouseEvent e
if(e.getClickCount()==2, 3) //双击动作,或三连击动作
sop(“鼠标就来了”+count++);}})
很好玩,处理的改变而改变。按钮有活动,但是鼠标的click会比按钮先执行,更具体。
键盘事件:
but.addKeyListener(newKeyAdapter(){//注册了一个键盘监听
public void keyPress(KeyEvent e){//KeyEvent封装了键盘。
if(e.isControlDown()&&e.getKeyCode()==KeyEvent.VK_ENTER){sop(“ele”);}//组合键
System.out.println(KeyEvent.getKeyText(e.getKeyCode())+”..”+e.getKeyCode());
System.out.println(e.getKeyChar()+”..”+e.getKeyCode());//键值和对应码}})
TextField tf=new TextField(20);创建组件文本框,设置长度为20个字。然后使用f.add(tf);添加到Frame 中。添加事件:tf.addKeyListener(newKeyAdapter(){
Publicvoid keyPressed(KeyEvent e){
intcode=e.getKeyCode();
if(!(code>=KeyEvent.VK_0&&code<=KeyEvent.VK_9)){
sop(code+”...是非法的”);
e.consume();//非法时,数值不会进入文本框。}}});
get.KeyChar()和get.KeyCode(),分别是获取字符,获取对应码。
/*模拟地址指向文件==列出指定目录的内容*/
class MyWindowDemo{
Private Framef;private TextField tf;privateButton but;private TextArea ta;
private Dialogd;privat Label lab;private Button okBut;
MyWindowDemo(){ init(); }
public void init(){
f=new Frame(“my window”);f.setBounds(300,100,600,500);f.setLayout(newFlowLayout());
tf=new TextField(60);but=new Button(“转到”);ta=new TextArea(25,70);
f.add(tf);f.add(but);f.add(ta);myEvent();f.setVisible(true);
d=new Dialog(f,”提示信息-self”,true);d.setBounds(400,200,200,150);
d.setLayout(new FlowLayout());lab=new Label();okBut=new Button(“确定”);
d.add(lab);d.add(okBut);
}public void myEvent(){
tf.addKeyListener(new KeyAdapter(){
Public void keyPressed(KeyWvent e){
If(e.getKeyCOde()==KeyEvent.VK_ENTER) showDir();
}})
okBut.addActionListener(new ActionListener(){
Public void actionPerformed(ActionEvent e){
d.setVisible(false);
}});
d.addWindowListener(new WindowAdapter(){
Public void windowClosing(WindowEvent e){
d.setVisible(false);
}});
but.addActionListener(new ActionListened(){
Public void actionPerformed(ActionEvent e){showDir();
}});
f.addWindowListener(new WindowAdapter(){
Public void windowClosing(WindowEvent e){
System.exit(0);
}});
}private voidshowDir(){
StringtextdirPath=tf.getText();//ta.setText(text);//System.out.pritln(text);
File dir=new File(dirPath);
If(dir.exists()&&dir.isDiretory()){
ta.setText(“”);
String[]names=dir.list();
For(Stringname:names){
ta.setTextappend(name+”\r\n”);
}}//tf.setText(“”);
else{
String info=”你输入的信息:”+dirPath+”是错误的。请确定!”;
lab.setText(info);
d.setVisible(true);}
}public staticvoid main(String[] args){
new MyWindowDemo();}}
/*制作菜单*/
class MyMenuDemo{
private Framef;private MenuBar mb;private Menu m,subMenu;
private MenuItemcloseItem,subItm;//声明引用
myMenuDemo(){init();}//构造
public voidinit(){
f=new Frame(“my window”);//地方
f.setBound(300,100,500,600);
f.setLayout(new FlowLayout());
mb=new MenuBar();m=new Menu(“文件”);
subMenu=new Menu(“子菜单”);//菜单有箭头,条目没有
subItem=new MenuItem(“子条目”);closeItem=new MenuItem(“退出”);//条目
subMenu.add(subItem);//子菜单生一个子条目
m.add(subMenu|subItem);m.add(closeItem);mb.add(m);
f.setMenuBar(mb);myEvent();f.setVisible(true);
}private voidmyEvent(){
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 voidmain(String[] args){
new MyMenuDemo();}}
/*文件的打开与保存*/
Class MyMenuTest{
private Framef;private MenuBar bar;private TextArea ta;private Menu fileMenu;
private MenuItemopenItem,saveItem,closeItem;private FileDialog openDia,saveDia;private File file;
MyMenuTest(){init();}
public voidinit(){
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);
sasveDia=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.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();
if(dirPath==null || fileName==null) return;
ta.setText(“”);
File file=new File(dirPath,fileName);
try{BufferedReader bufr=new BufferedReader(newFileReader(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();}}
应用
一、对话框:Dialog
何时需要产生对话框:此对象时需要在调用时,才创建对象。如:当在误操作的时候,就需要出现提示错误信息的对话框,此时才创建的这个对象。
示例:
对列出指定目录内容示例的功能增强。
[java] view plaincopyprint?/*
列出指定目录下的内容,当输入的路径不正确时,给出错误提示信息。
*/
import java.io.*;
import java.awt.*;
import java.awt.event.*;
class MyWindowDemo
{
//定义所需组件引用
private Frame f;
private Button but,bok;
private TextField tf;
private TextArea ta;
private Dialog d;
private Label lab;
//构造函数
MyWindowDemo()
{
init();
}
//窗体基本设置于功能实现
public void init()
{
//组件实例化
f=new Frame("我的Window");
but=new Button("跳转");
tf=new TextField(50);
ta=new TextArea(30,60);
//基本设置
f.setBounds(300,150,500,500);
f.setLayout(new FlowLayout());
//添加组件
f.add(tf);
f.add(but);
f.add(ta);
//窗体事件
myEvent();
//窗体显示
f.setVisible(true);
}
//注册事件
public void myEvent()
{
//窗体关闭功能
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
//“跳转”按钮事件
but.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
showFile();//列出目录内容在文本区中
}
});
//文本框键盘事件
tf.addKeyListener(new KeyAdapter()
{
public void keyPressed(KeyEvent e)
{
//如果键盘按下Enter键,就将目录内容显示在文本区中
if(e.getKeyCode()==KeyEvent.VK_ENTER)
showFile();
}
});
}
//目录内容显示在文本区中方法
private void showFile()
{
String path=tf.getText();//获取输入的路径
File dir=new File(path);//将路径封装成对象
//判断输入的路径是否存在,且是否是文件夹
if (dir.exists()&&dir.isDirectory())
{
ta.setText("");//清空文本区中的内容---------
String names[]=dir.list();//列出目录下的内容
//遍历
for (String name : names )
{
ta.append(name+"\r\n");//添加进文本区中
}
}
else
{
//对话框基本设置
d=new Dialog(f,"错误提示",true);
d.setBounds(400,200,280,150);
d.setLayout(new FlowLayout());
bok=new Button("确定");
lab=new Label();
//添加按钮和文本
d.add(bok);
d.add(lab);
//对话框关闭事件
d.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
d.setVisible(false);//退出对话框
}
});
//“确定”按钮事件
bok.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
d.setVisible(false);//按确认键,退出对话框
}
});
String info="您输入的路径:"+path+"是错误的,请重输!";
lab.setText(info);//设置标签文本内容
d.setVisible(true);//显示对话框
}
}
public static void main(String[] args)
{
//运行窗体
new MyWindowDemo();
}
}
/*
列出指定目录下的内容,当输入的路径不正确时,给出错误提示信息。
*/
import java.io.*;
import java.awt.*;
import java.awt.event.*;
class MyWindowDemo
{
//定义所需组件引用
private Frame f;
private Button but,bok;
private TextField tf;
private TextArea ta;
private Dialog d;
private Label lab;
//构造函数
MyWindowDemo()
{
init();
}
//窗体基本设置于功能实现
public void init()
{
//组件实例化
f=new Frame("我的Window");
but=new Button("跳转");
tf=new TextField(50);
ta=new TextArea(30,60);
//基本设置
f.setBounds(300,150,500,500);
f.setLayout(new FlowLayout());
//添加组件
f.add(tf);
f.add(but);
f.add(ta);
//窗体事件
myEvent();
//窗体显示
f.setVisible(true);
}
//注册事件
public void myEvent()
{
//窗体关闭功能
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
//“跳转”按钮事件
but.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
showFile();//列出目录内容在文本区中
}
});
//文本框键盘事件
tf.addKeyListener(new KeyAdapter()
{
public void keyPressed(KeyEvent e)
{
//如果键盘按下Enter键,就将目录内容显示在文本区中
if(e.getKeyCode()==KeyEvent.VK_ENTER)
showFile();
}
});
}
//目录内容显示在文本区中方法
private void showFile()
{
String path=tf.getText();//获取输入的路径
File dir=new File(path);//将路径封装成对象
//判断输入的路径是否存在,且是否是文件夹
if (dir.exists()&&dir.isDirectory())
{
ta.setText("");//清空文本区中的内容---------
String names[]=dir.list();//列出目录下的内容
//遍历
for (String name : names )
{
ta.append(name+"\r\n");//添加进文本区中
}
}
else
{
//对话框基本设置
d=new Dialog(f,"错误提示",true);
d.setBounds(400,200,280,150);
d.setLayout(new FlowLayout());
bok=new Button("确定");
lab=new Label();
//添加按钮和文本
d.add(bok);
d.add(lab);
//对话框关闭事件
d.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
d.setVisible(false);//退出对话框
}
});
//“确定”按钮事件
bok.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
d.setVisible(false);//按确认键,退出对话框
}
});
String info="您输入的路径:"+path+"是错误的,请重输!";
lab.setText(info);//设置标签文本内容
d.setVisible(true);//显示对话框
}
}
public static void main(String[] args)
{
//运行窗体
new MyWindowDemo();
}
}
二、菜单:Menu
1、菜单继承关系
2、说明
1)Menu:菜单,继承MenuItem;有右三角的图标存在,可添加Menu和MenuItem
2)MenuBar:菜单栏,可添加菜单和菜单条目。一般先创建菜单栏,再创建菜单。
3)MenuItem:菜单条目,也称菜单项,无右三角的图标存在,是最终的菜单项。
4)菜单的事件处理和组件一样,可以对类型为MenuItem和Menu的对象这个事件源添加活动监听ActionListener,并进行相关的事件处理。
5)通过setMenuBar()方法,将菜单添加到Frame中。
示例:
[java] view plaincopyprint?import java.awt.*;
import java.awt.event.*;
class MyMenuDemo
{
//定义组件引用
private Frame f;
private MenuBar mb;
private Menu m,subMenu;
private MenuItem closeItem,subItem;
//构造函数
MyMenuDemo()
{
init();
}
//窗体设置与功能实现
public 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("子菜单");//菜单下面的子菜单
subItem = new MenuItem("子条目");//子菜单包含的菜单条目
closeItem = new MenuItem("退出");//菜单包含的条目
//菜单添加菜单组件
subMenu.add(subItem);
m.add(subMenu);
m.add(closeItem);
mb.add(m);
//窗体添加菜单组件
f.setMenuBar(mb);
//窗体上事件
myEvent();
//窗体显示
f.setVisible(true);
}
private 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)
{
new MyMenuDemo();
}
}
import java.awt.*;
import java.awt.event.*;
class MyMenuDemo
{
//定义组件引用
private Frame f;
private MenuBar mb;
private Menu m,subMenu;
private MenuItem closeItem,subItem;
//构造函数
MyMenuDemo()
{
init();
}
//窗体设置与功能实现
public 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("子菜单");//菜单下面的子菜单
subItem = new MenuItem("子条目");//子菜单包含的菜单条目
closeItem = new MenuItem("退出");//菜单包含的条目
//菜单添加菜单组件
subMenu.add(subItem);
m.add(subMenu);
m.add(closeItem);
mb.add(m);
//窗体添加菜单组件
f.setMenuBar(mb);
//窗体上事件
myEvent();
//窗体显示
f.setVisible(true);
}
private 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)
{
new MyMenuDemo();
}
}
三、jar包双击执行
既然是图形化界面,就需要通过图形化界面的形式运行程序,而不是是用Dos命令行执行,那么如何通过双击程序就执行程序呢?这就需要将程序的class文件打包。
步骤如下:
1,打包。编译时:javac-d c:\myclassMyMenuTest.java 目录下会出现一个文件夹mymenu
2,使用jar:jar -cvf my.jarmymenu 错误是:加载清单属性失败。找不到主函数的类...
2,创建文本: 一,Main-Class:空格 mymenu.MyMenuTest回车固定格式
二,jar -cvfm my.jar 1.txt mymenu
3,jar文件注册:工具->文件夹选项->文件类型->高级->jar可设置。关联。