GUI
GUI图形用户界面:用图形的方式,来显示计算机操作的界面
swing demo: http://docs.oracle.com/javase/tutorial/uiswing/examples/components/index.html
主要用到的包:
Awt与 Swing
java.Awt:抽象窗口工具包,需要调用本地系统方法实现功能。属于重量级控件。
javax.Swing:在AWT的基础上,建立的一套图形界面系统,其中提供了更多的组件,而且完全由Java实现。增强了移植性,属于轻量级控件。
布局管理器:容器中的组件的排放方式,就是布局。
常见的布局管理器:- FlowLayout(流式布局管理器):从左到右的顺序排列。Panel默认的布局管理器。
- BorderLayout(边界布局管理器)东,南,西,北,中。 Frame默认的布局管理器。
- GridLayout(网格布局管理器)规则的矩阵
- CardLayout(卡片布局管理器)选项卡
- GridBagLayout(网格包布局管理器)非规则的矩阵
如何建立一个简单的窗体?
- public static void main(String[] args) {
- Frame f = new Frame("my window");
- f.setLayout(new FlowLayout());//设置布局方式
- f.setSize(500,400);//设置窗体大小,500为横向长度,400为纵向长度
- f.setLocation(300,200);//设置窗体出现在屏幕的位置,300为离屏幕左边横坐标距离,200为纵向
- f.setVisible(true);//设置窗体显示
- }
这样,一个简单的窗体就创建了,但是我们发现,这就仅仅是一个空白的窗体界面,连关闭按钮都没有实现,只能是强行的停止Java虚拟机了。
那么如何的给窗体添加点个性呢?
事件监听机制的组成:
- 事件源(组件)
- 事件(Event)
- 监听器(Listener)
- 事件处理(引发事件后处理方式)
原理图:
事件监听机制的特点:
1,事件源。
2,事件
3,监听器
4,事件处理。
事件源:就是awt包或者swing包中的那些图形界面组件。
事件:每一个事件源都有自己特有的对应事件和共性事件。
监听器:可以触发某一个事件的动作(不止一个动作)都已经封装到了监听器中
1,事件源。
2,事件
3,监听器
4,事件处理。
事件源:就是awt包或者swing包中的那些图形界面组件。
事件:每一个事件源都有自己特有的对应事件和共性事件。
监听器:可以触发某一个事件的动作(不止一个动作)都已经封装到了监听器中
如,往上序代码加上些监听器
- }); [java] view plaincopyprint?
- <span style="font-size:14px;">class Tests extends Frame{
- /**
- * 窗体标识ID
- */
- private static final long serialVersionUID = 13L;
- TextArea content = null;
- File f = null;//文件关联
- FileDialog openDia = new FileDialog(this,"打开文件",FileDialog.LOAD);//文件窗口
- FileDialog saveDia = new FileDialog(this,"保存文件",FileDialog.SAVE);
- public void initial(){
- this.setSize(680, 512);//设置窗体大小
- this.setLayout(getLayout());//设置布局方式
- this.setLocation(310,130);//设置窗体出现在屏幕的位置
- // 添加指定的窗口侦听器,以从此窗口接收窗口事件。
- this.addWindowListener(new WindowAdapter() {
- //关闭窗口
- public void windowClosing(WindowEvent e){
- System.exit(0);
- }
- content = new TextArea();//创建一个文本区域
- this.add(content);
- //我想,我么可以添加一个菜单条
- MenuBar mb = new MenuBar();
- this.setMenuBar(mb);
- //再创建一个菜单
- Menu file = new Menu("文件");
- mb.add(file);
- //添加一个菜单项
- MenuItem open = new MenuItem("打开");
- file.add(open);
- //再添加一个菜单项
- MenuItem save = new MenuItem("保存");
- file.add(save);
- //再添加一个菜单项
- MenuItem newSave = new MenuItem("另存为");
- file.add(newSave);
- //当然还可以有一个子菜单吧
- Menu help = new Menu("帮助");
- file.add(help);
- //在子菜单中再添加一个菜单项
- MenuItem about = new MenuItem("关于");
- help.add(about);
- //给菜单项添加指定的窗口侦听器
- open.addActionListener(new ActionListener() {
- @Override
- 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;
- content.setText("");
- f = new File(dirPath, fileName);
- try {
- BufferedReader bufr = new BufferedReader(new FileReader(f));
- String line = null;
- while((line = bufr.readLine())!=null){
- content.append(line+"\r\n");//将文件读取到文本区域中 去
- }
- bufr.close();
- } catch (Exception e2) {
- throw new RuntimeException("读取失败");
- }
- }
- });
- save.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- //如果文件关联为null,则弹出窗口,要求选择路径
- writeSave(f==null);
- }
- });
- newSave.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- writeSave(true);
- }
- });
- this.setVisible(true);
- }
- public void writeSave(boolean b){
- if(b){
- saveDia.setVisible(true);
- String dirPath = saveDia.getDirectory();
- String fileName = saveDia.getFile();
- if(dirPath==null || fileName == null)
- return;
- f = new File(dirPath, fileName);
- }
- try {
- BufferedWriter bufw = new BufferedWriter(new FileWriter(f));
- String text =content.getText();
- bufw.write(text);//将数据写入文件
- bufw.close();
- } catch (Exception e2) {
- throw new RuntimeException();
- }
- }
- public static void main(String[] args) {
- new Tests().initial();
- }
- }</span>