一,概念:
1, Graphical User Interface(图形用户接口)用图形的方式,来显示计算机操作的界面,这样更方便更直观。
2, 用户与计算机交互方式有俩种:除了GUI之外还有CLI(命令行用户接口),例如Dos命令行操作,操作不直观看。
3, java为GUI提供的对象都存在java.Awt和javax.Swing俩个包中。
java.Awt: Abstract Window Toolkit(抽象窗口工具包),需要调用本地系统方法
实现功能,属于重量级控件。
javax.Swing:在AWT的基础上,建立的一套图形界面系统,其中提供了更多的组件,而且
完全由java实现。增强了移植性,属轻量级控件。
二,GUI继承关系图:
三,布局管理器
1,容器中的组件的排放方式,就是布局。
, 2,常见的布局管理器:
a,FlowLayout(流式布局管理器)*从左到右边的顺序排列。*Panel默认的布局管理器。
注:添加组件,默认添加在窗口中间。
b,BorderLayout(边界布局管理器)*东,南,西,北,中 *Frame默认的布局管理器。
注:添加组件,默认是窗口全局大小,添加新组件覆盖原组件。
c,GredLayout(网格布局管理器)*规则的矩阵。
注:如计算器按键格式。
d,FardLayout(卡片布局管理器)*选项卡。
注:如window右键属性中的格式。
e,GridBagLayout(网格包布局管理器)*非规则的矩阵。
注:如计算器案件,但不一定占用一个格子。
3, 界面由多种布局完成情况:
a,先设置窗体Frame的布局,在窗体中添加panel面板。
b,在不同的panel面板中设置不同的布局。
四,窗体的简单创建方法
1,创建一个Fram窗体
Frame f = new Frame("窗体名称“);
2,窗体的基本显示信息设置
f.setSize(500,400);//设置窗体大小,500代表长度(横向),400代表宽度(纵向)
f.setLocation(300,200);//设置本地位置,300代表横向距离,200代表纵向位置。
f.setBounds(500,400,300,200);//将上面俩个步骤综合成一步
f.setLayout(Layout l); //设置布局管理器
3, 组件的创建
Button b = new Button(”组件名称“);//定义组件。
4,将组件添加到窗体中
f.add(b);//将组件添加到窗体中
5,让窗体显示
f.setVisible(true);//参数true与false代表是否显示窗体。
示例:
五,事件监听机制
1基本组成:a,事件源(组件):awt或者swing包中的图形界面组件。
b,事件(event):每一个事件都有自己特有的对应事件和共性事件
c,监听器(Listener):将可能触发某一事件的动作(不只一个动作),都封装到监听器中
d,事件处理(引发事件后的处理方式):将所监听到的时间对象进行处理。
2,事件监听机制流程图
例子:博物馆中的密码锁(事件源)密码锁上的报警装置(监听器)盗贼砸撬锁(外部动作)
锁被外部动作破坏(事件对象) 监听保安出去阻止盗贼(事件处理方式)。
我们要做的就是对产生的事件进行处理。
步骤:
a,通过事件源(容器或组件)的方法addxxxListener在事件源上添加监听器,xxxListener的子类或者xxxListener子类 xxxAdapter的子类。()
b,(一般是匿名内部类)覆写事件监听器中的方法。(方法参数一般为xxxEvent)
注:1,xxxEvent是被打包的事件。 2,WindowListener接口中有7个方法,为了不必复写掉这些方法,所以添加继承其的子类windowsAdapter(适配器),复写掉自己所需要的方法即可。
示例:
<span style="font-size:14px;">package Date419;
import java.awt.Button;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class AwtEventDemo {
public static void main(String[]args)
{
new AwtEventDemo();
//System.out.println();
}
private Frame f=null;
private Button b =null;
AwtEventDemo()
{
init();
}
public void init()
{
f = new Frame("我的窗体");//创建一个名叫我的窗体的窗体
f.setSize(500,400);//设置窗体大小
f.setLocation(300,200);//设置窗体位置
//f.setBounds(500,400,300,200);//综合上面俩项
b = new Button("我的按钮");//创建一个名叫我的按钮的按钮
f.add(b);//添加按钮
f.addWindowListener(new WindowAdapter()//在窗体f中添加一个监听器,
{
public void windowClosing(WindowEvent e)//复写WindowAdapter的方法windowClosing,将事件打包成对象传递进去。
{
System.exit(0);//事件发生即会自动调用这个方法。
}
});
b.addActionListener(new ActionListener()//在按钮b上添加监听器,
{
public void actionPerformed(ActionEvent e)//复写掉点击方法 并传入被监听器打包成的对象
{
System.exit(0);//处理方式
}
});
f.setVisible(true);//通过加载boolean值来确定显示窗体f
}
}</span>
练习:鼠标键盘事件
<span style="font-size:12px;">package Date419;
import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
public class KeyAndMouseEvent {
//创建组件引用(全局)
private Frame f; //窗体
private Button but; //按钮
private TextField tf; //文本框
private TextArea ta; //文本
//构造函数
KeyAndMouseEvent()
{
init();
}
//窗体创建与功能实现
public void init()
{
//组件实例化
f=new Frame("my fame");
but=new Button("跳转");
tf=new TextField(50);
ta=new TextArea(30,58);
//设置窗体
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)
{
System.out.println("按钮在动");
}
});
*/
but.addMouseListener(new MouseAdapter() //鼠标监听
{
//int count=0;
public void mouseClicked(MouseEvent e) //
{
// if(e.getClickCount()==2) //getClickCount,获取鼠标点击次数
// System.out.println("鼠标双击按钮");
// System.out.println("鼠标单击按钮");
//System.exit(0);
showFile();//显示到文本区中
}
/*
public void mouseEntered(MouseEvent e) //鼠标放入按钮去监听
{
System.out.println("鼠标放进入按钮范围"+(++count)+"次");
}
*/
});
/*
//按钮键盘事件
but.addKeyListener(new KeyAdapter() //键盘监听事件,
{
public void keyPressed(KeyEvent e)
{
//捕获同时按下ctrl+entry
if(e.isControlDown()&&e.getKeyCode()==KeyEvent.VK_ENTER)
System.out.println("Ctrl+Enter.....is Down");
//System.out.println(e.getKeyText(e.getKeyCode())+"---"+e.getKeyCode());
}
});
*/
//文本框键盘事件
tf.addKeyListener(new KeyAdapter() //文本框监听事件
{
public void keyPressed(KeyEvent e)
{
/*
//判断输入字符是否是数字
if(e.getKeyCode()<KeyEvent.VK_0||e.getKeyCode()>KeyEvent.VK_9)
{
System.out.println("你输入的数字非法,请重数");
e.consume();//不显示输入的字符
}
*/
if(e.getKeyCode()==KeyEvent.VK_ENTER)
showFile();//将目录下的内容显示到文本区中
}
});
}
//将路径下的目录或文件列出
private void showFile()
{
String path=tf.getText();//获取文本框内容
File file=new File(path);//将路径封装成文件对象
//判断是否存在
if(file.exists())
{
ta.setText("");//清空文本区中的内容
if(file.isDirectory()) //如果是目录
{
String[] dir=file.list();//列出目录下的文件和目录
for (String name : dir) //遍历
{
ta.append(name+"\r\n");//添加到文本区中
}
}
else
ta.append(file.getName());//如果是文件,则只显示该文件的名字
}
else
System.out.println("输入有误");
}
public static void main(String[] args)
{
//运行窗体
new KeyAndMouseEvent();
}
}</span>
应用:
/*
列出指定目录下的内容,当输入的路径不正确时,给出错误提示信息。
*/
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,菜单继承关系
注:a,Menu:菜单,继承MenuITem;有右三角的图标存在,可添加Menu和MenuItem
b,MenuBar:菜单栏,可添加菜单和菜单条目,一般先创建菜单栏,再创建菜单
c,MenuItem:菜单条目,无右三角的图标存在,最终菜单项
d,菜单的时间处理和组件相同,可以对类型为MenuIte和Menu的对象这个事件源添加活动监听
ActionListener,并进行相关处理。
e,通过setMenuBar()方法,将菜单添加到Frame中。
<span style="font-size:14px;">package Date419;
import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
public class KeyAndMouseEvent {
//创建组件引用(全局)
private Frame f; //窗体
private Button but; //按钮
private TextField tf; //文本框
private TextArea ta; //文本
//构造函数
KeyAndMouseEvent()
{
init();
}
//窗体创建与功能实现
public void init()
{
//组件实例化
f=new Frame("my fame");
but=new Button("跳转");
tf=new TextField(50);
ta=new TextArea(30,58);
//设置窗体
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)
{
System.out.println("按钮在动");
}
});
*/
but.addMouseListener(new MouseAdapter() //鼠标监听
{
//int count=0;
public void mouseClicked(MouseEvent e) //
{
// if(e.getClickCount()==2) //getClickCount,获取鼠标点击次数
// System.out.println("鼠标双击按钮");
// System.out.println("鼠标单击按钮");
//System.exit(0);
showFile();//显示到文本区中
}
/*
public void mouseEntered(MouseEvent e) //鼠标放入按钮去监听
{
System.out.println("鼠标放进入按钮范围"+(++count)+"次");
}
*/
});
/*
//按钮键盘事件
but.addKeyListener(new KeyAdapter() //键盘监听事件,
{
public void keyPressed(KeyEvent e)
{
//捕获同时按下ctrl+entry
if(e.isControlDown()&&e.getKeyCode()==KeyEvent.VK_ENTER)
System.out.println("Ctrl+Enter.....is Down");
//System.out.println(e.getKeyText(e.getKeyCode())+"---"+e.getKeyCode());
}
});
*/
tf.addKeyListener(new KeyAdapter() //文本框监听事件
{
public void keyPressed(KeyEvent e)
{
/*
//判断输入字符是否是数字
if(e.getKeyCode()<KeyEvent.VK_0||e.getKeyCode()>KeyEvent.VK_9)
{
System.out.println("你输入的数字非法,请重数");
e.consume();//不显示输入的字符
}
*/
if(e.getKeyCode()==KeyEvent.VK_ENTER)
showFile();//将目录下的内容显示到文本区中
}
});
}
//将路径下的目录或文件列出
private void showFile()
{
String path=tf.getText();//获取文本框内容
File file=new File(path);//将路径封装成文件对象
//判断是否存在
if(file.exists())
{
ta.setText("");//清空文本区中的内容
if(file.isDirectory()) //如果是目录
{
String[] dir=file.list();//列出目录下的文件和目录
for (String name : dir) //遍历
{
ta.append(name+"\r\n");//添加到文本区中
}
}
else
ta.append(file.getName());//如果是文件,则只显示该文件的名字
}
else
System.out.println("输入路径错误");
}
public static void main(String[] args)
{
//运行窗体
new KeyAndMouseEvent();
}
}</span>
三、jar包双击执行
图形化界面的运行,将java生成class文件进行打包处理,使其可以双击执行(jar包)。
步骤如下:
1、首先java文件需要有包(没有需创建),例如:java文件内容首行 package mymenu ;
2、生成包:通过编译javac -d c:\myclass MyMenu.java,此时则在c盘下的myclass文件夹下生成了mymenu包 里面存放所有的.class文件
3、在此目录下新建一个文件,如1.txt或者其他任意名称任意扩展名的文件都可,然后在其中编辑固定的格式:“Main-Class: mymenu.MenuDemo”,只写引号中的内容。需要需要在冒号后有一个空格,在文件末尾要回车。
4、编译:jar -cvfm my.jar 1.txt mymenu即可。如果想添加其他信息,则直接编译jar即可得出相应的命令
5、此时双击即可执行。
说明:
1) 在固定格式中:
a、如果无空格:在编译的时候,就会报IO异常,提示无效的头字段,即invalidheader 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下的字符串值,右击点修改,在原路径的中添加-jar,如:"C:\ProgramFiles\Java\jre6\bin\javaw.exe" -jar "%1",注意-jar两边要有空格,保存。
package Date419;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
class MyMenuText
{
//定义组件引用
private Frame f;
private TextArea ta;
private MenuBar mb;
private Menu fileMe;
private MenuItem openMi,saveMi,otherSaveMi,closeMi;
private FileDialog openDia,saveDia;
private File file;
//构造函数
MyMenuText()
{
init();
}
//功能实现
private void init()
{
//组件实例化
f=new Frame("MyText");
ta=new TextArea();
mb=new MenuBar();
fileMe=new Menu("文件");
openMi=new MenuItem("打开");
saveMi=new MenuItem("保存");
otherSaveMi=new MenuItem("另存为");
closeMi=new MenuItem("退出");
openDia=new FileDialog(f,"选择打开的文件",FileDialog.LOAD);
saveDia=new FileDialog(f,"保存到哪里",FileDialog.SAVE);
//基本设置
f.setBounds(300,150,600,500);
//添加组件
f.add(ta);
f.setMenuBar(mb);
mb.add(fileMe);
fileMe.add(openMi);
fileMe.add(saveMi);
fileMe.add(otherSaveMi);
fileMe.add(closeMi);
//窗体中事件
myEvent();
//窗体显示
f.setVisible(true);
}
private void myEvent()
{
//窗体关闭功能
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
//打开事件
openMi.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
//显示文件对话窗口
openDia.setVisible(true);//------------------
String dir=openDia.getDirectory();//获取目录
String fileName=openDia.getFile();//获取文件名
if(dir==null||fileName==null)//对打开了文件对话框,但未做出操作的处理
return;
file=new File(dir,fileName);//文件对象
try
{
ta.setText("");//每打开一个文件时,将文本区的内容清空
//带缓冲技术的读取流
BufferedReader br=new BufferedReader(new FileReader(file));
String line=null;//读一行
while ((line=br.readLine())!=null)
{
//添加到文本区域
ta.append(line+"\r\n");
}
br.close();//关流
}
catch (IOException ie)
{
throw new RuntimeException("文件打开失败");
}
}
});
//保存事件
saveMi.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
//如果是第一次保存,则显示文件对话框
if(file==null)//-------------
{
//显示文件对话框
saveDia.setVisible(true);//----------------------
String dir=saveDia.getDirectory();
String filename=saveDia.getFile();
if(dir==null||filename==null)//--------------------
return;
file=new File(dir,filename);
}
save();//保存文件方法
}
});
//另存为事件
otherSaveMi.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
//不管是不是第一次保存,都显示文件对话框
saveDia.setVisible(true);//----------------------
String dir=saveDia.getDirectory();
String filename=saveDia.getFile();
if(dir==null||filename==null)//--------------------
return;
file=new File(dir,filename);
save();//保存文件方法
//保存时,默认文件对话框位置在打开文件的位置
openDia.setFile(file.getName());
}
});
//退出事件
closeMi.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
});
}
//保存文件
private void save()
{
try
{
//带缓冲区的写入流
BufferedWriter bw=new BufferedWriter(new FileWriter(file));
//获取文本区域中的内容
String text=ta.getText();
bw.write(text);//写入文件中
bw.close();//关流
}
catch (IOException ie)
{
throw new RuntimeException("文件保存失败");
}
}
public static void main(String[] args)
{
//程序运行
new MyMenuText();
}
}