----------------------
JavaEE+Android、Java培训、期待与您交流! ----------------------
GUI
图形用户界面(Graphical User Interface),用于计算机与用户交互的一种方式
java也将这种界面封装为对象,其中的对象都放在了两个包中:java.Awt包和javax.Swing包
java.Awt包:Abstract Window Toolkit,即抽象窗口工具箱。要调用本地系统方法实现功能,属重量级控件。
javax.Swing包:在AWT的基础上建立的一套图形界面系统,其中提供了更多的组件,且完全有java实现,增强了移植性,属轻量级控件
布局管理器
1、布局:容器中的组件排列方式
2、常见的布局管理器:
1)FlowLayout:流式布局管理器,从左向右排列,是Panel默认的布局管理器
2)BorderLayout:边界式布局管理器,东南西北中的排列方式,是Frame的默认布局管理器
3)GridLayout:网格式布局管理器,规则的矩阵
4)CardLayout:卡片式布局管理器,即选项卡
5)GridBayLayout:网格包布局管理器,非规则矩阵
3、如果存在多种布局方式,如何创建窗体界面呢?
步骤:
1)先将窗体Frame进行大区域的划分,设置其布局管理器,加入面板Panel
2)将组建加入Panel,设置面板的布局管理器
import java.awt.*;
import java.awt.event.*;
class FrameDemo{
//定义该图形中所需的组件引用
private Frame f;
private Button but;
FrameDemo(){
init();
}
//初始化组件
public void init(){
//对frame进行基本设置
f = new Frame("my frame");
f.setBounds(300,100,600,500);
f.setLayout(new FlowLayout());
but = new Button("button");
//将组件添加进窗体
f.add(but);
//加载一下窗体上事件
myEvent();
//显示窗体
f.setVisible(true);
}
//窗体事件
private 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("button 关闭的窗口");
System.exit(0);
}});
}
public static void main(String[] args)
{
new FrameDemo();
}
}
创建图形化界面思路
1、创建Frame容器:Frame f = new Frame("my Frame");可设置标题
2、对窗体进行设置:如大小,位置,布局等。
f.setSize(int wight,int hight);
f.setLocation(int x,int y),设置横纵坐标;
f.setBounds(int wight,int hight,int x,int y),者直接用这个对大小和位置设置
f.setLayout(Layout layout),参数为指定的布局管理器,如FlowLayout
3、定义组件:如Button b = new Button(“Button”),可设置组件的名称
4、将组件通过窗体的add方法添加到窗体中,f.add(b);
5、让窗体显示,通过setVisible(boolean b),通过设置参数是true还是false是否显示窗体
1、事件源(组件):awt或swing包中的那些图形界面组件
2、事件(Event):每一个事件源都有自己特有的对应的时间和共性事件
3、监听器(Listener):将可触发某一事件的动作(不止一个),都封装到监听器中。注意,是动作封装到监听器中。
4、事件处理:引发事件后的处理方式。
前三个在java中都已将定义好了,直接获取其对象来用即可,我们需要做的就是对产生的动作进行处理,步骤:
1、明确监听器(Frame)将监听器注册到上面,通过方法:addWindowListener(WindowListener w)
2、注意:若用子类实现WindowListener接口,就需要覆盖其中的7个方法,可只用到其中的关闭动作,其他动作未用到,就必须重写全部,因为WindowLister的子类WindowAdapter以实现此接口,并覆盖了其中所有方法,则只需继承WindowAdapter,覆盖需要的方法即可。
3、明确事件,并对事件进行处理,其实,添加什么监听器就需要添加什么事件。
例如:
鼠标和键盘事件
import java.awt.*;
import java.awt.event.*;
class MouseAndKeyEvent
{
//创建全局变量
private Frame f = null;
private Button but = null;
private TextField tf;
//初始化窗体
MouseAndKeyEvent(){
init();
}
//创建窗体
public void init(){
f = new Frame("my frame");
f.setBounds(300,200,600,500);
f.setLayout(new FlowLayout());
tf = new TextField(20);
but = new Button("button");
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);
}});
//给but添加一个活动监听器
but.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.out.println("action ok");
}});
//给文本框添加一个监听器,引发键盘事件
tf.addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent e){
int code = e.getKeyCode();
System.out.println(e.getKeyChar() + "..." + e.getKeyChar());
if(!(code>=KeyEvent.VK_0&&code<=KeyEvent.VK_9)){
e.consume();
System.out.println(code + "是非法的");
}});
//鼠标事件
//myMouseEvent();
//键盘事件
myKeyEvent();
}
//鼠标事件
public void myMouseEvent(){
//给but添加一个鼠标监听器
but.addMouseListener(new MouseAdapter(){
private int count = 1;
private int clickCount = 1;
private int clickCount2 = 1;
public void mouseEntered(MouseEvent e){
System.out.println("鼠标进入该组件" + count++);
}
public void mouseClicked(MouseEvent e){
if(e.getClickCount()==2)
System.out.println("双击动作" + clickCount2++);
//System.out.println("点击动作" + clickCount++);
}});
}
//键盘事件
public void myKeyEvent(){
//给but添加一个键盘监听器
but.addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent e)
{
//组合键ctrl键和回车键同时触发
if(e.isControlDown()&&e.getKeyCode()==KeyEvent.VK_ENTER)
System.out.println("ctrl+enter is runing");
//System.out.println(e.getKeyChar() + "..." + e.getKeyCode());
//System.out.println(KeyEvent.getKeyText(e.getKeyCode()) + "..." + e.getKeyChar());
}
});
}
public static void main(String[] args)
{
new MouseAndKeyEvent();
}
}
练习
1、打开指定目录下的目录条目
import java.awt.*;
import java.awt.event.*;
import java.io.*;
class MyWindowDemo
{
//创建全局变量
private Frame f;
private Button but;
private TextField tf;
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,200,600,500);
f.setLayout(new FlowLayout());
//创建组件
but = new Button("转到");
tf = new TextField(60);
ta = new TextArea(25,75);
d = new Dialog(f,"提示信息-self",true);
d.setBounds(300,100,300,150);
d.setLayout(new FlowLayout());
lab = new Label();
okBut = new Button("确定");
//将组件添加到窗体
f.add(tf);
f.add(but);
f.add(ta);
d.add(lab);
d.add(okBut);
//添加事件
myEvent();
//设置窗体可见
f.setVisible(true);
}
//常见引发的时间
private void myEvent()
{
//给but添加一个活动监听器
but.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
showInfo();
}
});
okBut.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
d.setVisible(false);
}
});
//给文本框添加键盘事件
tf.addKeyListener(new KeyAdapter()
{
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode()==KeyEvent.VK_ENTER)
showInfo();
}
});
//关闭窗体事件
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
//给对话框添加一个监听器,关闭对话框事件
d.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
d.setVisible(false);
}
});
}
//操作文件,对文件夹的内容读取
private void showInfo()
{
String dirPath = tf.getText();
File file = new File(dirPath);
if(file.exists()&&file.isDirectory())
{
ta.setText("");
String[] names = file.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();
}
}
2、 打开文件,将内容显示在文本框中,并将写入文本框中的内容保存
/*
菜单练习
*/
package mymenu;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class MenuDemo
{
//设置全局变量
private Frame f;
private MenuBar bar;
private TextArea ta;
private Menu fileMenu;
private MenuItem closeMT,submu,openItem,saveItem;
private FileDialog openDia,saveDia;
private File file;
//初始化菜单项
MenuDemo()
{
init();
}
//初始化窗体和组件
public void init()
{
//初始化窗体frame
f = new Frame("my Menu");
f.setBounds(300,100,600,500);
//初始化菜单项
bar = new MenuBar();
ta = new TextArea();
fileMenu = new Menu("文件");
submu = new MenuItem("子条目");
openItem = new MenuItem("打开");
saveItem = new MenuItem("保存");
closeMT = new MenuItem("退出");
//将组件加入到文件菜单这个容器组件中
fileMenu.add(submu);
fileMenu.add(openItem);
fileMenu.add(saveItem);
bar.add(fileMenu);
f.setMenuBar(bar);
fileMenu.add(closeMT);
openDia = new FileDialog(f,"我要打开",FileDialog.LOAD);
saveDia = new FileDialog(f,"我要保存",FileDialog.SAVE);
f.add(ta);
//添加事件
myEvent();
//设置窗体可见性
f.setVisible(true);
}
//添加事件
private void myEvent()
{
//添加窗体监听,关闭按钮
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
//添加退出菜单项目的监听,退出程序
closeMT.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
});
//添加打开和保存文件的监听,获取文件
//打开事件
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 = new File(dirPath,fileName);
BufferedReader bufr = null;
try
{
//读取文件中的内容
bufr = new BufferedReader(new FileReader(file));
String line = null;
while((line=bufr.readLine())!=null)
{
ta.append(line+"\r\n");
}
}
catch (IOException ex)
{
throw new RuntimeException("读取失败");
}
finally
{
try
{
if(bufr!=null)
bufr.close();
}
catch (IOException ex)
{
throw new RuntimeException("关闭流失败");
}
}
}
});
//保存事件
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);
BufferedWriter bufw = null;
try
{
//写入文本框中的内容
bufw = new BufferedWriter(new FileWriter(file));
String text = ta.getText();
bufw.write(text);
bufw.flush();
}
catch (IOException ex)
{
throw new RuntimeException("写入失败");
}
finally
{
try
{
if(bufw!=null)
bufw.close();
}
catch (IOException ex)
{
throw new RuntimeException("关闭流失败");
}
}
}
}
});
}
public static void main(String[] args)
{
new MenuDemo();
}
}
----------------------
JavaEE+Android、Java培训、期待与您交流! ----------------------
详细请查看: http://edu.csdn.net