图形用户界面GUI
|--Awt:需要调用本地系统方法实现功能,跨平台性不是很好
|--Swing:在Awt基础上,建立的一台图形界面系统,完全由java实现,增强了移植性,很好的跨平台性。
【布局管理器】容器中组件的排序方式,即布局
FlowLayout(流式布局管理器):从左到右顺序,panel默认布局管理器
BorderLayout(边界布局管理器):东、南、西、北、中,Frame默认布局
GridLayout(网格布局管理器)网格形式对容器的组件进行布置
GardLayout(卡片布局管理器):选项卡
GridBagLayout(网格包布局管理器):非规则矩阵
1 创键图形化界面
步骤创建Frame窗体
对窗体进行基本设置
定义组件并通过窗体的add方法将组件添加到窗体中
让窗体显示通过setVisible(ture)完成
import java.awt.*;
class FrameDemo
{
public static void main(String[] args)
{
Frame f=new Frame("my awt");//建立窗体对象
f.setSize(500,400);//设置窗体size
f.setLocation(300,200);//设置窗体位置
f.setLayout(new FlowLayout());//设置为流式布局
f.add(new Button("button"));//添加按钮
f.setVisible(true);//显示组件
}
}
运行结果:
2 事件监听机制
1、事件源:Awt和Swing中的界面组件2、事件:每个事件源都有自己特有的对应事件和共性事件
3、监听器:将可以出发某个事件的动作都已经封装到监听器中
以上java都已封装好,处理动作才是关键
4、事件处理
import java.awt.*;
import java.awt.event.*;
class FrameDemo
{
public static void main(String[] args)
{
Frame f=new Frame("my awt");
f.setSize(500,400);
f.setLocation(300,200);
f.setLayout(new FlowLayout());
f.add(new Button("button"));
f.setVisible(true);
f.addWindowListener(new MyWin());//添加监听器,WindowListener有7个抽象方法(若继承需要复写7个方法),发现有一个可以只用一个的子类WindowAdapter把说有方法都空复写了
}
}
class MyWin extends WindowAdapter//(覆盖了WindowListener全部方法,且覆盖内容为空)
{
public void windowClosing(WindowEvent e) //定义关闭操作触发机制动作
{
System.out.println("window closing...");
System.exit(0);//退出窗口动作
}
public void windowActivated(WindowEvent e)
{
System.out.println("Activating...");
}
public void windowOpened(WindowEvent e)
{
System.out.println("window open...");
}
}
【综合应用】
模拟对话框,列出指定目录下的文件:
package cn.swu1;
/**
* 需求:模拟对话框,列出指定目录下的文件和目录
**/
import java.io.*;
import java.awt.*;
import java.awt.event.*;
public class Myawt {
public static void main(String[] args) throws Exception{
new AwtTest().set();
}
}
class AwtTest{
//定义对话框所需引用
private Frame frame;
private Button but,okbut;
private TextField tf;
private TextArea ta;
private Dialog d;
private Label lab;
//设置对话框
public void set(){
//组件实例化
frame=new Frame("Myawt");
frame.setBounds(400, 150, 500, 500);
frame.setLayout(new FlowLayout());
tf=new TextField(50);
but=new Button("Go");
ta=new TextArea(25,60);
//添加组件
frame.add(tf);
frame.add(but);
frame.add(ta);
//窗体事件
myEvent();
//设置窗体可见
frame.setVisible(true);
}
//注册窗体事件
public void myEvent(){
//文本框键盘事件,按enter就执行show方法,将目录打印在文本区
tf.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
if(e.getKeyCode()==KeyEvent.VK_ENTER)//判断事件是否为按下enter键
show(); //打印目录下的文件
}
});
//按钮跳转事件,按下按钮就执行show方法,将目录打印在文本区
but.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
show();//在文本区打印目录
}
});
//窗口关闭事件
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
//在文本区打印目录
public void show(){
ta.setText("");//初始化文本区
String str=tf.getText();//获取文本框字符
File f=new File(str);
if(f.exists()&&f.isDirectory()){
String[] s=f.list();
for(String ss:s){
ta.append(ss+"\r\n");
}}
else //弹出错误对话框
d=new Dialog(frame,"ERROR",true);
d.setBounds(500,300,280,100);
d.setLayout(new FlowLayout());
okbut=new Button("确定");
lab=new Label();
//对话框添加组件
d.add(lab);
d.add(okbut);
//设置标签文本内容
lab.setText("The path::"+str+"is wrong.try againnnnnnn");
//显示对话框
d.setVisible(true);
//关闭对话框事件
d.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
d.setVisible(false);//退出对话框
}
});
//“确定”按钮退出对话框事件
okbut.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
d.setVisible(false);//按确认键,退出对话框
}
});
}
}
运行结果:
3 菜单:Menu
1、菜单继承关系
2、说明
1)Menu:菜单,继承MenuItem;有右三角的图标存在,可添加Menu和MenuItem
2)MenuBar:菜单栏,可添加菜单和菜单条目。一般先创建菜单栏,再创建菜单。
3)MenuItem:菜单条目,也称菜单项,无右三角的图标存在,是最终的菜单项。
4)菜单的事件处理和组件一样,可以对类型为MenuItem和Menu的对象这个事件源添加活动监听ActionListener,并进行相关的事件处理。
5)通过setMenuBar()方法,将菜单添加到Frame中。
package menu;
/*
需求:模拟记事本程序,可以打开、保存文本文件
*/
import java.awt.FileDialog;
import java.awt.Frame;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.*;
public class Mymenu {
public static void main(String[] args) {
new Mymenu().;
}
//定义记事本所需组件
private Frame f;//窗口
private MenuBar bar;//菜单栏
private Menu fileMenu;//菜单
private MenuItem openItem,saveItem,exitItem;//条目
private TextArea ta;//文本区
private FileDialog openDialog,saveDialog;//打开、保存文件对话框
private File file;
//构造函数,初始化
Mymenu(){
set();
}
//设置窗口
public void set(){
//组件初始化
f=new Frame("MyMENU");
f.setBounds(400, 150, 500, 400);
bar=new MenuBar();
fileMenu=new Menu("File");
openItem=new MenuItem("open");
saveItem=new MenuItem("save");
exitItem=new MenuItem("exit");
ta=new TextArea();
openDialog=new FileDialog(f,"OPEN",FileDialog.LOAD);//打开文件对话框
saveDialog=new FileDialog(f,"SAVE",FileDialog.SAVE);//保存文件对话框
//添加组件
f.setMenuBar(bar);//窗口添加菜单栏
f.add(ta);//窗口添加文本区域
fileMenu.add(openItem);//添加条目至菜单
fileMenu.add(saveItem);
fileMenu.add(exitItem);
bar.add(fileMenu);//添加菜单至菜单栏
//窗体上的事件
myEvent();
//显示窗口
f.setVisible(true);
}
//定义窗体事件
public void myEvent(){
//窗口关闭事件
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
//“打开”条目-打开文件事件
openItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
openDialog.setVisible(true);//显示文件对话框
ta.setText("");//初始化对话区域
String dirStr=openDialog.getDirectory();//获取目录
String nameStr=openDialog.getFile();//获取文件名
file=new File(dirStr+nameStr);//将文件封装成对象
try {
BufferedReader br=new BufferedReader(new FileReader(file));
String line=null;
while((line=br.readLine())!=null){
ta.append(line+"\r\n");//读取文件数据,写入文本区域
}
} catch (Exception e1) {
throw new RuntimeException("Read Exception");
}
}
});
//保存事件
saveItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(file==null){//判断文件是否存在
saveDialog.setVisible(true);//显示文件对话框
String dirStr=saveDialog.getDirectory();
String nameStr=saveDialog.getFile();
if(dirStr==null||nameStr==null)//判断目录或者文件是否为空
return;
file=new File(dirStr+nameStr);
}
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(file));
bw.write(ta.getText());//将文本区域的内容写入文件
bw.flush();
bw.close();
} catch (Exception e2) {
throw new RuntimeException("Write Exception");
}
}
});
//退出事件
exitItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
}
}
运行结果:
4 jar包双击执行
步骤如下:
1、首先要在java文件中导入一个包,没有则需创建一个包,如package mymenu;
2、生成包:通过编译javac -d f:\jar Mymenu.java,此时则在f盘下的jar文件夹下生成了所有的.class文件
3、在此目录下新建一个文件,如1.txt或者其他任意名称任意扩展名的文件都可,然后在其中编辑固定的格式:“Main-Class: mymenu.MenuDemo”,只写引号中的内容。需要需要在冒号后有一个空格,在文件末尾要回车。
4、编译:jar -cvfm my.jar 1.txt mymenu即可。如果想添加其他信息,则直接编译jar即可得出相应的命令
5、此时双击即可执行。
2)jar文件必须在系统中注册,才能运行。注册方法如下:
a.打开任意对话框,在菜单栏点击工具按钮,选择文件夹选项
b.选择新建--->扩展名,将扩展名设置为jar,确定
c.选择高级,可更改图标,然后点击新建,命名为open,
d.在用于可执行应用程序中,点浏览,将jdk下的bin的整个文件路径添加进来,并在路径后添加-jar即可。