1、Java高级程序设计实验报告一广东金融学院实验报告课程名称:Java高级程序设计实验编号及实验名称实验一 计算器系 别计科系姓 名学 号班 级实验地点实验楼504实验日期2012-11-8实验时数6指导老师同组其他成员无成 绩一、实验目的1、巩固java开发基础2、学习Java图形界面开发技术二、实验环境1使用软件:Windows XP、jdk1.62实验设备:微型计算机三、实验内容及要求参考书本87页程序Calculator.java,以此为基础扩展其功能,设计自己的计算器。四、实验结果(附程序运行效果图)import java.awt.BorderLayout;import javax.s
2、wing.JPanel;import javax.swing.JFrame;import javax.swing.JTextField;import java.awt.Rectangle;import java.awt.GridBagLayout;import java.awt.GridLayout;import javax.swing.JButton;import java.awt.Point;import java.awt.event.KeyAdapter;import java.awt.event.KeyEvent;import java.awt.ComponentOrientation
3、;import java.awt.Dimension;public class Calculator extends JFrame private static final long serialVersionUID = 1L; private JPanel jContentPane = null; private JTextField tR = null; private JPanel p = null; private JButton jButton = null; private JButton jButton1 = null; private JButton jButton2 = nu
4、ll; private JButton jButton3 = null; private JButton jButton4 = null; private JButton jButton5 = null; private JButton jButton6 = null; private JButton jButton7 = null; private JButton jButton8 = null; private JButton jButton9 = null; private JButton jButton10 = null; private JButton jButton11 = nul
5、l; private JButton jButton12 = null; private JButton jButton13 = null; private JButton jButton14 = null; private JButton jButton15 = null; private double result=0; private String lastCommand=; / jve:decl-index=0: private boolean start=true; private JButton jButton16 = null; private JButton jButton17
6、 = null; private JButton jButton18 = null; private JButton jButton19 = null; /* * This is the default constructor */ public Calculator() super(); initialize(); private void initialize() this.setSize(290, 190); this.setResizable(false); this.setLocation(new Point(100, 300); this.setContentPane(getJCo
7、ntentPane(); this.setTitle(计算器); /* * This method initializes jContentPane * * return javax.swing.JPanel */ private JPanel getJContentPane() if (jContentPane = null) jContentPane = new JPanel(); jContentPane.setLayout(null); jContentPane.add(getTR(), null); jContentPane.add(getP(), null); return jCo
8、ntentPane; /* * This method initializes tR * * return javax.swing.JTextField */ private JTextField getTR() if (tR = null) tR = new JTextField(); tR.setBounds(new Rectangle(0, 0, 284, 22); tR.setComponentOrientation(ComponentOrientation.UNKNOWN); tR.addKeyListener(new java.awt.event.KeyAdapter() publ
9、ic void keyPressed(java.awt.event.KeyEvent e) char key=e.getKeyChar(); if(key=0|key=1|key=2|key=3|key=4|key=5 |key=6|key=7|key=8|key=9|key=.) if(start) tR.setText(); start=false; tR.setText(tR.getText(); else if(key=+|key=-|key=*|key=/|key=) if(start) if(key=-) tR.setText(String.valueOf(key); start=
10、false; else lastCommand=String.valueOf(key); else calculator(Double.parseDouble(tR.getText(); lastCommand=String.valueOf(key); start=true; ); return tR; /* * This method initializes p * * return javax.swing.JPanel */ private JPanel getP() if (p = null) GridLayout gridLayout = new GridLayout(5,4); p
11、= new JPanel(); p.setLayout(gridLayout); p.setBounds(new Rectangle(0, 21, 284, 141); p.add(getJButton16(), null); p.add(getJButton17(), null); p.add(getJButton18(), null); p.add(getJButton19(), null); p.add(getJButton2(), null); p.add(getJButton9(), null); p.add(getJButton13(), null); p.add(getJButt
12、on15(), null); p.add(getJButton14(), null); p.add(getJButton11(), null); p.add(getJButton10(), null); p.add(getJButton7(), null); p.add(getJButton6(), null); p.add(getJButton1(), null); p.add(getJButton(), null); p.add(getJButton3(), null); p.add(getJButton4(), null); p.add(getJButton5(), null); p.a
13、dd(getJButton8(), null); p.add(getJButton12(), null); return p; /* * This method initializes jButton * * return javax.swing.JButton */ private JButton getJButton() if (jButton = null) jButton = new JButton(); jButton.setText(6); jButton.addActionListener(new java.awt.event.ActionListener() public vo
14、id actionPerformed(java.awt.event.ActionEvent e) String input=e.getActionCommand(); if(start) tR.setText(); start=false; tR.setText(tR.getText()+input); ); return jButton; /* * This method initializes jButton1 * * return javax.swing.JButton */ private JButton getJButton1() if (jButton1 = null) jButt
15、on1 = new JButton(); jButton1.setText(5); jButton1.addActionListener(new java.awt.event.ActionListener() public void actionPerformed(java.awt.event.ActionEvent e) String input=e.getActionCommand(); if(start) tR.setText(); start=false; tR.setText(tR.getText()+ input); ); return jButton1; /* * This me
16、thod initializes jButton2 * * return javax.swing.JButton */ private JButton getJButton2() if (jButton2 = null) jButton2 = new JButton(); jButton2.setText(+); jButton2.addActionListener(new java.awt.event.ActionListener() public void actionPerformed(java.awt.event.ActionEvent e) String input=e.getAct
17、ionCommand(); if(start) lastCommand=input; else calculator(Double.parseDouble(tR.getText(); lastCommand=input; start=true; tR.setText(); ); return jButton2; /* * This method initializes jButton3 * * return javax.swing.JButton */ private JButton getJButton3() if (jButton3 = null) jButton3 = new JButt
18、on(); jButton3.setText(.); jButton3.addActionListener(new java.awt.event.ActionListener() public void actionPerformed(java.awt.event.ActionEvent e) String input=e.getActionCommand(); if(start) tR.setText(); start=false; tR.setText(tR.getText()+ input); ); return jButton3; /* * This method initialize
19、s jButton4 * * return javax.swing.JButton */ private JButton getJButton4() if (jButton4 = null) jButton4 = new JButton(); jButton4.setText(1); jButton4.addActionListener(new java.awt.event.ActionListener() public void actionPerformed(java.awt.event.ActionEvent e) String input=e.getActionCommand(); i
20、f(start) tR.setText(); start=false; tR.setText(tR.getText()+ input); ); return jButton4; /* * This method initializes jButton5 * * return javax.swing.JButton */ private JButton getJButton5() if (jButton5 = null) jButton5 = new JButton(); jButton5.setText(2); jButton5.addActionListener(new java.awt.e
21、vent.ActionListener() public void actionPerformed(java.awt.event.ActionEvent e) String input=e.getActionCommand(); if(start) tR.setText(); start=false; tR.setText(tR.getText()+ input); ); return jButton5; /* * This method initializes jButton6 * * return javax.swing.JButton */ private JButton getJBut
22、ton6() if (jButton6 = null) jButton6 = new JButton(); jButton6.setText(4); jButton6.addActionListener(new java.awt.event.ActionListener() public void actionPerformed(java.awt.event.ActionEvent e) String input=e.getActionCommand(); if(start) tR.setText(); start=false; tR.setText(tR.getText()+ input);
23、 ); return jButton6; /* * This method initializes jButton7 * * return javax.swing.JButton */ private JButton getJButton7() if (jButton7 = null) jButton7 = new JButton(); jButton7.setText(0); jButton7.addActionListener(new java.awt.event.ActionListener() public void actionPerformed(java.awt.event.Act
24、ionEvent e) String input=e.getActionCommand(); if(start) tR.setText(); start=false; tR.setText(tR.getText()+input); ); return jButton7; /* * This method initializes jButton8 * * return javax.swing.JButton */ private JButton getJButton8() if (jButton8 = null) jButton8 = new JButton(); jButton8.setTex
25、t(3); jButton8.addActionListener(new java.awt.event.ActionListener() public void actionPerformed(java.awt.event.ActionEvent e) String input=e.getActionCommand(); if(start) tR.setText(); start=false; tR.setText(tR.getText()+ input); ); return jButton8; /* * This method initializes jButton9 * * return
26、 javax.swing.JButton */ private JButton getJButton9() if (jButton9 = null) jButton9 = new JButton(); jButton9.setText(-); jButton9.addActionListener(new java.awt.event.ActionListener() public void actionPerformed(java.awt.event.ActionEvent e) String input=e.getActionCommand(); if(start) tR.setText(input); start=false; else calculator(Double.parseDouble(tR.getText(); lastCommand=input; start=true; tR.setText(); ); return jButton9; /* * This method initializes jButton10 * * return javax.swing.JBu
copyright@ 2008-2023 冰点文库 网站版权所有
经营许可证编号:鄂ICP备19020893号-2