------------android培训、java培训、java学习型技术博客、期待与您交流! ------------
Graphical User Interface(图形用户接口)。用图形的方式,来显示计算机操作的界面,这样更方便更直观。比如平时在操作文件夹就是利用图形化界面与计算机交互的
Java为GUI提供的对象都存在java.Awt和javax.Swing两个包中。
java.Awt:Abstract Window ToolKit (抽象窗口工具包),需要调用本地系统方法实现功能。属重量级控件。
javax.Swing:在AWT的基础上,建立的一套图形界面系统,其中提供了更多的组件,而且完全 由Java实现。增强了移植性,属轻量级控件。
注:Container:为容器,是一个特殊的组件,该组件中可以通过add方法添加其他组件进来。
什么叫布局:容器中的组件的排放方式,就是布局。
常见的布局管理器:
FlowLayout(流式布局管理器)
从左到右的顺序排列。
Panel默认的布局管理器。
BorderLayout(边界布局管理器)
东,南,西,北,中
Frame默认的布局管理器。
GridLayout(网格布局管理器)
规则的矩阵
CardLayout(卡片布局管理器)
选项卡
GridBagLayout(网格包布局管理器)
非规则的矩阵
Container常用子类:Window Panel(面板, 不能单独存在。)
Window常用子类:Frame Dialog
创建过程:
1.创建frame窗体。
2.对窗体进行基本设置,比如大小,位置,布局。
3.定义组件。
4.将组件通过窗体的add方法添加到窗体中。
5.让窗体显示,通过setVisible(true)
代码实现:
public static void main(String[] args)
{
Frame f = new Frame("my window");
f.setLayout(new FlowLayout());
f.setSize(500,400);//设置窗体大小
f.setLocation(300,200);//设置窗体出现在屏幕的位置
f.setVisible(true);
}
事件监听机制的特点:
1.事件源
2.事件(Event)
3.监听器(listener)
4.事件处理(引发事件后处理方式)
事件源:就是awt包或者swing包中的那些图形界面组件。
事件:每一个事件源都有自己特有的对应事件和共性事件。
监听器:将可以触发某一个事件的动作(不只一个动作)都已经封装到了监听器中。
以上三者,在java中都已经定义好了。直接获取其对象来用就可以了,我们要做的事情是,就是对产生的动作进行处理。
确定事件源(容器或组件)
通过事件源对象的addXXXListener()方法将侦听器注册到该事件源上。
该方法中接收XXXListener的子类对象,或者XXXListener的子类XXXAdapter的子类对象。
一般用匿名内部类来表示。
在覆盖方法的时候,方法的参数一般是XXXEvent类型的变量接收。
事件触发后会把事件打包成对象传递给该变量。(其中包括事件源对象。通过getSource()或者,getComponent()获取。)
代码示例:
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,600,500);
f.setLayout(new FlowLayout());
but = new Button("my button");
//将组件添加到frame中
f.add(but);
//加载一下窗体上事件。
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()
{
private int count = 1;
public void actionPerformed(ActionEvent e)
{
//System.out.println("退出,按钮干的");
//System.exit(0);
//f.add(new Button("Button-"+(count++)));
//f.setVisible(true);
//f.validate();
//System.out.println(e.getSource());
Button b = (Button)e.getSource();
Frame f1 = (Frame)b.getParent();
f1.add(new Button("button-"+count++));
f1.validate();
}
});
}
public static void main(String[] args)
{
new FrameDemo();
}
}
菜单继承体系:
创建过程:
先创建菜单条,再创建菜单,每一个菜单 中建立菜单项。
也可以菜单添加到菜单中,作为子菜单。
通过setMenuBar()方法,将菜单添加到Frame中。
代码示例:
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();
}
}
这一节学习了Java中的GUI图形化界面的一些基础知识,感觉在设置软件界面的时候是最让人纠结的,这一节重点我感觉还是在事件的监听机制这一块,对控件的各种监听才是我们要学习的重点。