1、组件实验姓名 学号 班级 年级 指导教师 西安财经学院信息学院 Java程序设计 实验报告实验名称 组件的应用 实验室 522 实验日期 2014年 月 日 组件的应用一、 实验目的1、 熟悉和理解Java中AWT及Swing,能够设计简单美观的用户界面。2、 掌握使用Java中图形界面设计的组件及事件处理。二、 实验内容 1、编写程序,创建一个Frame,添加相关组件,实现简易计算器的基本功能。三、 实验环境1. 硬件:一台微机2. 软件:操作系统和java编译器四、 实验步骤1、 设置计算器的布局按钮button面板panel(文本框+按键区)框架frameJFrame frame=new
2、 JFrame(简易mini计算器); /创建一个JFrame的实例 JPanel p1=new JPanel(); JPanel p2=new JPanel(); /创建两个JPanelJTextField t1=new JTextField(25); /创建一个文本框 JButton b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16; /设置计算器键盘按钮/为框架设置BorderLayout布局管理器 frame.setLayout(new BorderLayout(4,4); /将计算器键盘面板p1放入框架 frame.a
3、dd(p1); /将文本框p2放入框架的顶部 frame.add(p2,BorderLayout.NORTH); /为p1设置4行4列的GridLayout布局管理器 p1.setLayout(new GridLayout(4,4,4,4); /为p2设置BorderLayout布局管理器 p2.setLayout(new BorderLayout(); 2、 对程序设置监听程序当按动按钮时,需要对事件作出响应,将内容显示到文本框中frame.addWindowListener(new WindowAdapter() public void windowClosing(WindowEvent
4、e) System.exit(0); );b0.addActionListener(new ActionListener() b1.addActionListener(new ActionListener() b2.addActionListener(new ActionListener()3、 完善计算器内部算法显示数字算法:public void actionPerformed(ActionEvent e) s+=1; s2+=1; t1.setText(s); 算数运算: public void actionPerformed(ActionEvent e) s1=s; s+=+; id=
5、1; s2=; t1.setText(s); 清除内容算法:public void actionPerformed(ActionEvent e) if(id1) ; else s+=; double a=Double.parseDouble(s1); double b=Double.parseDouble(s2); double c=0; switch(id) case 1:c=a+b; break; case 2:c=a-b; break; case 3:c=a*b; break; case 4:c=a/b; break; s+=c; t1.setText(s); 4、 调试程序,实现结果五
6、、 实验结果 六、 小结通过这次实验,我收获了许多知识同时发现了不少的问题。熟悉和理解了Java中AWT及Swing,能够设计简单美观的用户界面,还掌握了Java中图形界面设计的组件及事件处理的方法,达到了实验的目的。虽然在实验的过程中遇到一些问题,但大部分通过查阅资料解决了。在运行结果出现时,出现了如图所示的情况,结果正确,但原因有待探索七、 源程序清单import java.awt.*;import javax.swing.*;import java.awt.event.*;import javax.swing.text.*; public class counter int id=0;
7、/设置标记 String s=,s1=null,s2=null; JFrame frame=new JFrame(简易mini计算器); /创建一个JFrame的实例 JPanel p1=new JPanel(); JPanel p2=new JPanel(); /创建两个JPanel JTextField t1=new JTextField(25); /创建一个文本框 JButton b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16; /设置计算器键盘按钮 public static void main(String args
8、) counter that=new counter(); that.go(); public void go() /为框架设置BorderLayout布局管理器 frame.setLayout(new BorderLayout(4,4); /将计算器键盘面板p1放入框架 frame.add(p1); /将文本框p2放入框架的顶部 frame.add(p2,BorderLayout.NORTH); /为p1设置4行4列的GridLayout布局管理器 p1.setLayout(new GridLayout(4,4,4,4); /为p2设置BorderLayout布局管理器 p2.setLayo
9、ut(new BorderLayout(); /设置按钮名称及颜色 b0=new JButton(1); b1=new JButton(2); b2=new JButton(3); b3=new JButton(+);b3.setForeground(Color.red); b4=new JButton(4); b5=new JButton(5); b6=new JButton(6); b7=new JButton(-);b7.setForeground(Color.red); b8=new JButton(7); b9=new JButton(8); b10=new JButton(9);
10、b11=new JButton(*);b11.setForeground(Color.red); b12=new JButton(0); b13=new JButton(.); b14=new JButton(=);b14.setForeground(Color.green); b15=new JButton(/);b15.setForeground(Color.red); b16=new JButton(clean);b16.setForeground(Color.blue); p2.add(t1);/将文本框添加到p2面板中 /将b0-b15按钮添加到p1面板中 p1.add(b0); p
11、1.add(b1); p1.add(b2); p1.add(b3); p1.add(b4); p1.add(b5); p1.add(b6); p1.add(b7); p1.add(b8); p1.add(b9); p1.add(b10); p1.add(b11); p1.add(b12); p1.add(b13); p1.add(b14); p1.add(b15); /将b16按钮添加到框架的下方 frame.getContentPane().add(b16,BorderLayout.SOUTH); frame.setSize(300,300); /设置框架大小 frame.pack(); f
12、rame.setVisible(true);/显示计算器 /为按钮添加监听程序 frame.addWindowListener(new WindowAdapter() public void windowClosing(WindowEvent e) System.exit(0); ); b0.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) s+=1; s2+=1; t1.setText(s); ); b1.addActionListener(new ActionListener(
13、) public void actionPerformed(ActionEvent e) s+=2; s2+=2; t1.setText(s); ); b2.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) s+=3; s2+=3; t1.setText(s); ); b4.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) s+=4; s2+=4; t1.setText
14、(s); ); b5.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) s+=5; s2+=5; t1.setText(s); ); b6.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) s+=6; s2+=6; t1.setText(s); ); b8.addActionListener(new ActionListener() public void action
15、Performed(ActionEvent e) s+=7; s2+=7; t1.setText(s); ); b9.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) s+=8; s2+=8; t1.setText(s); ); b10.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) s+=9; s2+=9; t1.setText(s); ); b12.addActi
16、onListener(new ActionListener() public void actionPerformed(ActionEvent e) s+=0; s2+=0; t1.setText(s); ); b13.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) s+=.; s2+=.; t1.setText(s); ); b3.addActionListener(new ActionListener() public void actionPerformed(ActionE
17、vent e) s1=s; s+=+; id=1; s2=; t1.setText(s); ); b7.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) s1=s; s+=-; id=2; s2=; t1.setText(s); ); b11.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) s1=s; s+=*; id=3; s2=; t1.setText(s); )
18、; b15.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) s1=s; s+=/; id=4; s2=; t1.setText(s); ); b16.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) s=; s2=; t1.setText(s); ); b14.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) if(id1) ; else s+=; double a=Double.parseDouble(s1); double b=Double.parseDouble(s2); double c=0; switch(id) case 1:c=a+b; break; case 2:c=a-b; break; case 3:c=a*b; break; case 4:c=a/b; break; s+=c; t1.setText(s); ); 窗体底端
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1