java实验之GUI标准组件及事件处理

一、实验目的:

 

了解图形用户界面基本组件:框架、面板、按钮、标签、菜单、列表等的使用方法;了解如何使用布局管理器对组件进行管理;理解和掌握 Java 的事件处理机制。

 

二、实验环境:

 

一台配置有java环境,装有eclipse的电脑。

 

三、实验内容:

(写出主要的内容)

 

  • 实验要求

1.  了解Java图像用户界面的开发,掌握基本组件的使用。

2.  掌握编写独立运行的窗口界面的方法,了解菜单和对话框的使用方法。

3.  理解Java 的事件处理机制,掌握为不同组件编写事件处理程序的方法。

 

  • 在面板中添加多个组件,并进行合理的布局
    • 程序功能:在一个框架(Frame)容器中添加一个面板(Panel)容器,并分别在框架和面板容器中添加组件并使用不同的布局管理方式。
    • 请编写KY7_1.java 程序文件,相关源代码的提示如下。

import java.awt.*;

import java.awt.Color; 

public class KY7_1 {

public static void main(String args[]){

……   //创建一个框架f

  ……   //创建一个面板p

   ……   //把面板p添加到框架f中

   …… //设置面板p的背景颜色为蓝色

   p.setLayout(new FlowLayout());   //设置面板p的布局为流式布局

   ……   //在面板p中添加一个标签l

……  //在面板p中添加六个按钮

Choice c=new Choice ();   // 创建一个下拉列表c,

c.addItem("北京");        // 下拉列表c中有三个选项:北京、上海和天津

c.addItem("上海");

c.addItem("天津");

   ……   //把列表c添加到面板p中

……   //设置框架f的大小和可见性 

   }

  • 将上面程序中的空缺部分添加进去,编译并执行该程序。
  • 将KY7_1.java的完整源程序写在实验报告中。

import java.awt.*;

import java.awt.Color;

public class KY7_1 {

public static void main(String args[]){

Frame f=new Frame();//创建一个框架f

Panel p=new Panel();//创建一个面板p

  f.add(p);//把面板p添加到框架f中

  p.setBackground(Color.blue);//设置面板p的背景颜色为蓝色

  p.setLayout(new FlowLayout());   //设置面板p的布局为流式布局

  Label l=new Label();

  p.add(l);//在面板p中添加一个标签l

  Button button1=new Button();//在面板p中添加六个按钮

  Button button2=new Button();

  Button button3=new Button();

  Button button4=new Button();

  Button button5=new Button();

  Button button6=new Button();

  p.add(button1);

  p.add(button2);

  p.add(button3);

  p.add(button4);

  p.add(button5);

  p.add(button6);

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Choice c=new Choice ();   // 创建一个下拉列表c,

c.addItem("北京");        // 下拉列表c中有三个选项:北京、上海和天津

c.addItem("上海");

c.addItem("天津");

p.add(c);//把列表c添加到面板p中

f.setBounds(400,400,400,400);//设置框架f的大小和可见性

f.setVisible(true);

f.validate();

   }

}

 

 

 

import java.awt.*;

import java.awt.Color;

import javax.swing.JFrame;

public class KY7_1 {

public static void main(String args[]){

    JFrame f=new JFrame();//创建一个框架f

Panel p=new Panel();//创建一个面板p

  f.add(p);//把面板p添加到框架f中

  p.setBackground(Color.blue);//设置面板p的背景颜色为蓝色

  p.setLayout(new FlowLayout());   //设置面板p的布局为流式布局

  Label l=new Label();

  p.add(l);//在面板p中添加一个标签l

  Button button1=new Button();//在面板p中添加六个按钮

  Button button2=new Button();

  Button button3=new Button();

  Button button4=new Button();

  Button button5=new Button();

  Button button6=new Button();

  p.add(button1);

  p.add(button2);

  p.add(button3);

  p.add(button4);

  p.add(button5);

  p.add(button6);

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Choice c=new Choice ();   // 创建一个下拉列表c,

c.addItem("北京");        // 下拉列表c中有三个选项:北京、上海和天津

c.addItem("上海");

c.addItem("天津");

p.add(c);//把列表c添加到面板p中

f.setBounds(400,400,400,400);//设置框架f的大小和可见性

f.setVisible(true);

f.validate();

   }

}

java实验之GUI标准组件及事件处理

  • 为窗口添加菜单,并处理由菜单项所引发的事件
    • 程序功能:在窗口中添加菜单条,在菜单条中添加菜单,并在菜单中添加菜单项和子菜单。通过选择菜单项可以引发不同的事件,从而执行不同操作,例如点击“打开”菜单项可以打开一个“打开文件对话框”。

java实验之GUI标准组件及事件处理

    • 编写KY7_2.java 程序文件,源代码如下。

import java.awt.*;

import java.awt.event.*;

public class KY7_2 extends Frame implements ActionListener {

static Frame f;          //声明一个框架

static FileDialog fd1;    //声明一个文件对话框对象

  static FileDialog fd2;

public static void main(String args[]) {

KY7_2  k = new KY7_2();  

     f = new Frame();   // 创建一个框架f  

……   // 创建一个菜单条 mb

     ……   // 在菜单条mb中添加两个菜单m1("文件")和m2("帮助")

……   /*  "文件"菜单m1中有三个菜单项:m11("打开")、

m12("关闭")和m13("退出")*/

……  /*  "帮助"菜单m2中有三个菜单项:m21("目录")、

m22("索引")和m23("关于")*/

……  //  "文件"菜单m1中还有一个子菜单m3("编辑")

……  /*  "编辑"子菜单m3 中有三个菜单项:m31("复制")、

m32("剪切")和m33("粘贴")*/  

m11.addactionlistner(k);   /* 允许菜单项"打开"引发一个事件,该事件的处理者为当前对象 k  */

m13.addactionlistner(k);   /* 允许菜单项"退出"引发一个事件,该事件的处理者为当前对象 k  */

f.setSize(350,200);   // 设置框架f的大小

f.setMenuBar(mb);   // 设置框架f的菜单条为mb  

f.setVisible(true);    

}

public void actionPerformed (ActionEvent e) {  /* 处理由菜单项"打开"和"退出" 所引发的事件*/

if (e.getActionCommand() == "打开") {  /* 若"打开"菜单项引发事件,则打开“打开文件对话框”*/

FileDialog fd = new FileDialog (f, "open", FileDialog.SAVE); 

fd.setVisible (true);

}

if (e.getActionCommand() == "保存")    /* 若"保存"菜单项引发事件,则打开“保存文件对话框”*/

……  //创建并打开一个“保存文件对话框”

}

}

  • 将上面程序中的空缺部分添加进去,编译并执行该程序。
  • 将KY7_2.java的完整源程序写在实验报告中。

答:源代码为:

import java.awt.*;

import java.awt.event.*;

public class KY9_2 extends Frame implements ActionListener {

static Frame f;  //声明一个框架

static FileDialog fd1;  //声明一个文件对话框

static FileDialog fd2;

public static void main(String[] args) {

KY9_2 k=new KY9_2();

f=new Frame();   //创建一个框架f

MenuBar mb=new MenuBar();//创建一个菜单条mb

    Menu m1=new Menu("文件");//在菜单条mb中添加两个菜单m1("文件")和m2("帮助")

    Menu m2=new Menu("帮助");

    mb.add(m1);

    mb.add(m2);

    MenuItem m11=new MenuItem("打开");//"文件"菜单m1中有三个菜单项:m11("打开")、m12("关闭")、m13("退出")

    MenuItem m12=new MenuItem("关闭");

    MenuItem m13=new MenuItem("退出");

    m1.add(m11);

    m1.add(m12);

    m1.add(m13);

    MenuItem m21=new MenuItem("目录");//"帮助"菜单m2中有三个菜单项:m21("目录")、m22("索引")、m23("关于")

    MenuItem m22=new MenuItem("索引");

    MenuItem m23=new MenuItem("关于");

    m2.add(m21);

    m2.add(m22);

    m2.add(m23);

    Menu m3=new Menu("编辑");//  "文件"菜单m1中还有一个子菜单m3("编辑")

    m1.add(m3);

    MenuItem m31=new MenuItem("复制");/*  "编辑"子菜单m3 中有三个菜单项:m31("复制")、 m32("剪切")和m33("粘贴")*/  

    MenuItem m32=new MenuItem("剪切");

    MenuItem m33=new MenuItem("粘贴");

    m3.add(m31);

    m3.add(m32);

    m3.add(m33);

    m11.addActionListener(k); /* 允许菜单项"打开"引发一个事件,该事件的处理者为当前对象 k */

    m13.addActionListener(k); /* 允许菜单项"退出"引发一个事件,该事件的处理者为当前对象 k  */

    f.setSize(350,200);// 设置框架f的大小

    f.setMenuBar(mb);// 设置框架f的菜单条为mb  

    f.setVisible(true);

}

public void actionPerformed(ActionEvent e) { /* 处理由菜单项"打开"和"退出"所引发的事件*/

if(e.getActionCommand()=="打开"){ /* 若"打开"菜单项引发事件,则打开“打开文件对话框”*/

FileDialog fd1=new FileDialog(f,"open",FileDialog.SAVE);

fd1.setVisible(true);

}

if(e.getActionCommand()=="保存"){ /* 若"保存"菜单项引发事件,则打开“保存文件对话框”*/

FileDialog fd2=new FileDialog(f,"save",FileDialog.SAVE);//创建并打开一个“保存文件对话框”

fd2.setVisible(true);

}

}

}

结果截图:

java实验之GUI标准组件及事件处理

  • 思考题

1.构造函数和init()方法谁先被执行?

答:先构造方法再init()方法。因为构造函数调用的init()方法。
 2.编写Applet,包含两个按钮,一个按钮用于放大Applet上的一串字符串,一个按钮用于缩小;连续点击可不断放大或缩小。

源代码:

import java.awt.*;

 import java.applet.*;

public class ky9_3 extends Applet {

private static final long serialVersionUID = 1L;

private Button button = null;

private Button button1 = null;

int size= 20;

public ky9_3() {

super();

}

public void init() {

this.setLayout(null);

this.setSize(300, 200);

this.add(getButton(), null);

this.add(getButton1(), null);

}

private Button getButton() {

if (button == null) {

button = new Button();

button.setBounds(new Rectangle(70, 135,60, 20));

button.setLabel("放大");

button.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent e) {

size++;

repaint();

}

});

}

return button;

}

private Button getButton1() {

if (button1 == null) {

button1 = new Button();

button1.setBounds(new Rectangle(170, 135, 60, 20));

button1.setLabel("缩小");

button1.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent e) {

size--;

repaint();

}

});

}

return button1;

}

public void paint(Graphics g){

g.setFont(new Font("宋体",Font.BOLD+Font.ITALIC,size));

g.drawString("Applet", 100, 100);

}

}

 

结果截图:

java实验之GUI标准组件及事件处理