当前位置:文档之家› 北京信息科技大学Java语言程序设计-GUI资料

北京信息科技大学Java语言程序设计-GUI资料

【实验名称】实验5 Java GUI【实验目的】1、学习和掌握Java常用的GUI组件。

2、学习和掌握Java常用的布局管理器。

3、实习和掌握Java的事件驱动程序设计方法。

4、能够实现简单的Java GUI。

【实验步骤(解题过程)】1、第一步,Java GUI静态界面的设计和实现,包括:组件和组件的摆放(布局管理器)。

2、第二步,Java GUI动态事件处理的设计与实现,需要Java事件驱动模型。

3、第三步,如果需要实体类支持,则按实验2的步骤实现实体类并使用。

【实验内容】1、(移动小球)编写一个程序,在面板上移动小球。

定义一个面板类来显示小球,并提供想做、向右、向上和向下移动小球的方法。

说明:⑴程序来源编程练习16.3(P456)。

⑵可以不考虑小球移动到边界外的情况。

【实验预习】1.问题描述:创建一个界面,通过鼠标点击按钮实现小球的上下左右移动。

2.输入;鼠标点击按钮;处理:通过事件监听,实现圆的重画;输出;显示出移动效果。

3.技术支持;需要用到事件监听,及圆的绘制即图形知识,布局管理器等。

【附编程代码】import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.*;import java.awt.*;import java.awt.event.*;public class MovingBall extends JFrame {private static JButton bleft=new JButton("向左");private static JButton bright=new JButton("向右");private static JButton bup=new JButton("向上");private static JButton bdown=new JButton("向下");private CirclePanel canvas = new CirclePanel();public MovingBall() {JPanel panel = new JPanel(); // 将按钮放入面板中panel.add(bleft);panel.add(bright);panel.add(bup);panel.add(bdown);this.add(canvas, BorderLayout.CENTER); // 将含有圆的面板放在中央 this.add(panel, BorderLayout.SOUTH); //将含有按钮的面板放在右边//创建按钮的监听器并注册到按钮bleft.addActionListener(new Listener());bright.addActionListener(new Listener());bup.addActionListener(new Listener());bdown.addActionListener(new Listener());}//主方法public static void main(String[] args) {JFrame frame = new MovingBall();frame.setTitle("移动的小球");frame.setLocationRelativeTo(null); // Center the frame frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(480, 300);frame.setVisible(true);}//监听类class Listener implements ActionListener {public void actionPerformed(ActionEvent e) {System.out.println(new java.util.Date(e.getWhen())); if (e.getSource() == bleft)canvas.left();else if (e.getSource() == bright)canvas.right();else if (e.getSource() ==bup)canvas.up();else if (e.getSource() == bdown)canvas.down();}}//含有一个圆的面板的类class CirclePanel extends JPanel {private int x =0;private int y = 0;//左移这个圆public void left() {x--;repaint();}//右移这个圆public void right() {x++;repaint();}//上移这个圆public void up() {y--;repaint();}//下移这个圆public void down() {y++;repaint();}//重画这个圆protected void paintComponent(Graphics g) {super.paintComponent(g);g.drawOval(getWidth()/2+x,getHeight()/2+y,10, 10);}}}【实验过程及运行结果】在实验中遇到的问题是不会画圆,经过查书知道可以使用drawOval方法画圆。

2、(创建一个简单的计算器)编写一个程序完成加、减、乘、除操作。

图5 简单的计算器说明:⑴程序来源编程练习16.4(P456)。

⑵文本框组件javax. swing.JTextField,请参见教材17.6小节(P473),或者Java API文档。

⑶字符串与数值之间的转换,请参见2.18.1小节(P44)和9.2.10小节(P247),或者使用字符串格式化方法9.2.11(P248)。

【实验预习】1.问题描述:创建一个界面,利用鼠标点击加减乘除按钮,实现两个数之间的运算。

2.输入:在文本域中输入两个数字,并用鼠标点击按钮。

处理:创建一个监听类,对按钮进行监听,计算两个数字之间的运算。

输出:在结果文本域中显示运算结果3.技术要求:需要使用布局管理器,事件监听,及字符串与数字之间的转换等。

【附编程代码】import java.awt.FlowLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JTextField;public class easyCalculation extends JFrame{private JButton Add=new JButton("加");private JButton Subtract=new JButton("减");private JButton Multiply=new JButton("乘");private JButton Divide=new JButton("除");private JTextField one=new JTextField(5);private JTextField second=new JTextField(5);private JTextField all=new JTextField(10);private JLabel a=new JLabel("第一个数");private JLabel b=new JLabel("第二个数");private JLabel c=new JLabel("结果");public easyCalculation(){FlowLayout panel=new FlowLayout(FlowLayout.CENTER, 20, 50);setLayout(panel);add(a);add(one);add(b);add(second);add(c);add(all);add(Add);add(Subtract);add(Multiply);add(Divide);Add.addActionListener(new Listener());Subtract.addActionListener(new Listener());Multiply.addActionListener(new Listener());Divide.addActionListener(new Listener());}//主方法public static void main(String[] args) {easyCalculation frame = new easyCalculation();frame.setTitle("简单的计算器");frame.setLocationRelativeTo(null); // Center the frameframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.setSize(500, 200);frame.setVisible(true);}class Listener implements ActionListener {public void actionPerformed(ActionEvent e) {System.out.println(new java.util.Date(e.getWhen()));if (e.getSource() == Add){double a1=Double.parseDouble(one.getText());double b1=Double.parseDouble(second.getText());all.setText(String.format("%.2f",a1+b1));}else if (e.getSource() == Subtract){double a1=Double.parseDouble(one.getText());double b1=Double.parseDouble(second.getText());all.setText(String.format("%.2f",a1-b1));}else if (e.getSource() == Multiply){double a1=Double.parseDouble(one.getText());double b1=Double.parseDouble(second.getText());all.setText(String.format("%.2f",a1*b1));}else if (e.getSource() == Divide){double a1=Double.parseDouble(one.getText());double b1=Double.parseDouble(second.getText());all.setText(String.format("%.2f",a1/b1));}}}}【实验过程及运行结果】在实验过程中遇到问题是,经常忘记添加接口或者继承。

相关主题