黑马程序员,Java基础知识九:GUI图形用户界面

来源:互联网 发布:百胜软件单机版 编辑:程序博客网 时间:2024/04/30 15:19

GUI图形用户界面

从前当计算机的系统还处于使用命令行与计算机交互的时代,用户需要记忆许多常用的命令,每次的操作都需要完整的输入所需的命令,操作起来不方便,也不直观。如今随着计算机在新世纪不断的发展,已经推出了视窗系统,它的图形化界面,GUI全称Graphical User Interface(图形用户界面),是用图形的方式来显示计算机操作的界面的,这样比命令行用户接口CLI更加直观和方便。而在java中封装了许多图形用户界面的工具类,帮助人们更好的设计界面,操作电脑。


在这张图中,显示了GUI中的一些常用的组件,container是容器的意思,它是一个特殊的组件,该组件可以通过add方法来添加其他组件进来。在它的的下面,window“窗体”是顶级的容器类,非顶层组件的显示都依赖于顶层组件,frame“框架”,dialog“对话框”,FileDialog"文件对话框"都属于窗体的一种。而不属于窗体的Panel“面板”也是一种容器,所不同的是,它本身是透明的,它可以添加任何组件,但它本身也需要放置在窗体内。

在右边,Button是按钮的意思,可以在点击时触发各种事件。Label标签是一种可以显示一小段文字、一副图像的组件。它经常用来给其他组件做标记。而Checkbox是“复选框”,TextField是文本区,可以显示一行文字。TextArea则是文本域,如名,它可以显示一片文字的区域。

我们要怎么创建图形化界面呢?

1.创建frame等窗体。2.对窗体进行基本设置(比如大小,位置,布局管理等)。3.定义组件。4.将组件添加入窗体中。5.让窗体显示,通过setVisible(true);来实现。

示例:

public class FrameDemo{public static void main(String[] args){Frame f = new Frame();Button b = new Button("这是一个按钮");f.setSize(500,400);f.setLocation(300,200);f.setLayout(new FlowLayout());f.add(b);f.setVisible(true);}}

通过这样的设置,我们简单的实现了一个带按钮的窗体。

事件监听机制:

1.事件源,就是awt包或者swing包中的那些图形用户组件。

2.事件,每一个事件源都有自己特有的对应事件和共性事件。

3.监听器:将可以触发某一个事件的动作(不只是一个动作)都封装在监听器中。

以上三者,我们都在java中已经定义好了,直接获取来用就可以了,而我们要做的事情是,对产生的动作进行处理。

如何处理事件呢?

1,首先我们要确定事件源(容器或者组件)

2,通过事件源对象的addXXXListener()方法将侦听器注册到该事件源上。该方法接收一个XXXListener的子类对象,或者其子类XXXAdapter的子类对象。

3,一般用匿名内部类来表示。

4,在覆盖方法的时候,方法的参数一般是XXXEvent类型的变量接收。

5,事件触发后会把事件打包成对象传递给该变量。(其中包括事件源对象。通过getSource方法或者getComponent方法来获取。)

class  AwtDemo{public static void main(String[] args) {Frame f = new Frame("my awt");f.setSize(500,400);f.setLocation(300,200);f.setLayout(new FlowLayout());Button b = new Button("我是一个按钮");f.add(b);f.addWindowListener(new WindowAdapter(){public void windowClosing(WindowEvent e){System.out.println("我关");System.exit(0);}public void windowActivated(WindowEvent e) {System.out.println("我活了。");}public void windowOpened(WindowEvent e) {System.out.println("我被打开了,hahahhahah");}});f.setVisible(true);}}
让按钮具备退出程序的功能:

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);}});<pre name="code" class="java">but.addActionListener(new ActionListener(){private int count = 1;public void actionPerformed(ActionEvent e){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();}}

菜单

Java的awt包中也提供了菜单组件,方便用户制作菜单栏。


在菜单组件中有菜单栏MenuBar,菜单Menu,菜单项MenuItem,一般我们都是先创建菜单条,再创建菜单,然后在每一个菜单中建立菜单项。

也可以把菜单添加到菜单中,作为子菜单。

通过setMenuBar()方法可以将菜单添加到Frame中。

package mymenu;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();}}

0 0
原创粉丝点击