西南科技大学JAVA实验四实验报告材料.doc
word面向对象程序设计AJava实验报告实验四 GUI实验一、实验目的练习Java图形用户界面设计、界面布局、事件设计方法,熟悉GUI 程序设计的根本技术、根本Widget的使用、GUI设计用户接口的方法二、实验容设计一个类似Windows计算器的Application应用程序三、实验设计1、采用Java Application方式建立应用程序Calculator的框架2、仿照Windows计算器,实现界面设计。将contentPane分成两个jPanel(borderlayout),其中一个放置文本框,另一个继续划分为两局部,分别以FlowLayout和GridLayout的形式放置按钮。设置按钮、文本框等的Text值与相应属性的修改。3、设计方法:设两个变量value1、value2分别存储运算符左右两个操作数,设置标识符flag,初值为0,此时输入的数传给value1,当按下+-*时设置flag为1,此时输入的数字传给value2;按下=时,根据value1,value2与op值进展计算并输出至文本框4、设计方法调节程序bug5、点击事先设计的计算器数字按钮,显示框出现相应数字;点击“+-*/时,显示框出现相应字符;输入计算式后再输入“=,显示框出现计算结果,根本实现Windows计算器功能四、实验过程1、采用Java Application方式建立应用程序框架2、仿照Windows计算器,利用多种布局方式实现界面设计 3、组装操作数和运算符,完成简单的整数加、减、乘、除运算4、通过getTxt方法来获得显示框txt容,setTxt方法把字符显示在Txt框;通过Integer.parseIntString实现字符串到整型的转换,String.valueOf(Int实现整型到字符串的转换。5、遇到的问题:显示txt框无法显示两位与以上的数字。解决方法:txt.setTxt(String.valueOf(num*10+btnNum);把原来的数字*10,再加上要显示的字符并且将整型数字转化为字符串。五、实验结果 运行程序可得到一与Windows计算器界面类似形式由自己设定的计算器,可实现加、减、乘、除简单数学运算。六、讨论与分析 1.通过这次实验懂得了GUI的根本设计过程和其实现 2.对java的图形用户界面设计、事件设计方法有了进一步的了解七、附录:关键代码给出适当注释,可读性高public class Calculator extends javax.swing.JFrame public Calculator() initponents();private void initponents() /设置主面板按钮menuBar = new javax.swing.JMenuBar();fileMenu = new javax.swing.JMenu();openMenuItem = new javax.swing.JMenuItem();saveMenuItem = new javax.swing.JMenuItem();saveAsMenuItem = new javax.swing.JMenuItem();exitMenuItem = new javax.swing.JMenuItem();helpMenu = new javax.swing.JMenu();contentsMenuItem = new javax.swing.JMenuItem();aboutMenuItem = new javax.swing.JMenuItem();editMenu = new javax.swing.JMenu();cutMenuItem = new javax.swing.JMenuItem();copyMenuItem = new javax.swing.JMenuItem();pasteMenuItem = new javax.swing.JMenuItem();deleteMenuItem = new javax.swing.JMenuItem();jLabel1.setText("jLabel1");utton1.setText("utton1");jCheckBox1.setText("jCheckBox1");utton2.setText("utton2");javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);jPanel1.setLayout(jPanel1Layout);jPanel1Layout.setHorizontalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 100,Short.MAX_VALUE);jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 100,Short.MAX_VALUE);jCheckBox2.setText("jCheckBox2");jCheckBox2.addActionListener(new java.awt.event.ActionListener() public void actionPerformed(java.awt.event.ActionEvent evt) jCheckBox2ActionPerformed(evt););setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR);txtText.setFont(new java.awt.Font("微软雅黑", 0, 24);txtText.setHorizontalAlignment(javax.swing.JTextField.RIGHT);txtText.setText("0");txtText.setAutoscrolls(false);txtText.addActionListener(new java.awt.event.ActionListener() public void actionPerformed(java.awt.event.ActionEvent evt) );fileMenu.setText("u67e5u770b");openMenuItem.setText("Open");fileMenu.add(openMenuItem);saveMenuItem.setText("Save");fileMenu.add(saveMenuItem);saveAsMenuItem.setText("Save As .");fileMenu.add(saveAsMenuItem);exitMenuItem.setText("Exit");exitMenuItem.addActionListener(new java.awt.event.ActionListener() public void actionPerformed(java.awt.event.ActionEvent evt) exitMenuItemActionPerformed(evt););fileMenu.add(exitMenuItem);menuBar.add(fileMenu);helpMenu.setText("Help");contentsMenuItem.setText("Contents");helpMenu.add(contentsMenuItem);aboutMenuItem.setText("About");helpMenu.add(aboutMenuItem);editMenu.setText("Edit");cutMenuItem.setText("Cut");editMenu.add(cutMenuItem);copyMenuItem.setText("Copy");editMenu.add(copyMenuItem);pasteMenuItem.setText("Paste");editMenu.add(pasteMenuItem);deleteMenuItem.setText("Delete");editMenu.add(deleteMenuItem);helpMenu.add(editMenu);menuBar.add(helpMenu);setJMenuBar(menuBar);javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane();getContentPane().setLayout(layout);layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(layout.createSequentialGroup().addContainerGap().addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING,false).addGroup(layout.createSequentialGroup().addponent(.addGap(21, 21, 21);layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(layout.createSequentialGroup().addGap(26, 26, 26).addponent(txtText,javax.swing.GroupLayout.PREFERRED_SIZE,64,javax.swing.GroupLayout.PREFERRED_SIZE).addGap(18, 18, 18).addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addponent(/其他按键功能设置已省略utton26,javax.swing.GroupLayout.PREFERRED_SIZE,38,javax.swing.GroupLayout.PREFERRED_SIZE).addponent(utton32,javax.swing.GroupLayout.PREFERRED_SIZE,83,javax.swing.GroupLayout.PREFERRED_SIZE).addContainerGap(60,Short.MAX_VALUE);pack();boolean isOperPressed = false;/为true时输入下一个数字时清屏int oper = 0;/1实现加, 2实现减, 3实现乘, 4实现除, 5实现取余int number1 = 0;/参与运算的第一个数字,保存在number1中private void utton8ActionPerformed(java.awt.event.ActionEvent evt) /按键,实现回退功能String s = txtText.getText();/获取屏幕上的字符串int num = Integer.parseInt(s);/转换为整形num = num / 10;s = Integer.toString(num);/转换为字符型txtText.setText(s);/显示到屏幕上private void utton9ActionPerformed(java.awt.event.ActionEvent evt) /C按键,实现清屏功能txtText.setText("0");private void utton10ActionPerformed(java.awt.event.ActionEvent evt) /CE按键,实现初始化功能txtText.setText("0");isOperPressed = false;oper = 0;number1 = 0;private void utton17ActionPerformed(java.awt.event.ActionEvent evt) /取余if(oper !=0)/if else 语句实现连运算 下同dengyu();oper=5;elseString s = txtText.getText();number1 = Integer.parseInt(s);/保存屏幕上的数字oper = 5;isOperPressed = true;/清屏private void utton16ActionPerformed(java.awt.event.ActionEvent evt) /除if(oper !=0)dengyu();oper=4;elseString s = txtText.getText();number1 = Integer.parseInt(s);oper = 4;isOperPressed = true;private void utton21ActionPerformed(java.awt.event.ActionEvent evt) /乘if(oper !=0)dengyu();oper=3;elseString s = txtText.getText();number1 = Integer.parseInt(s);oper = 3;isOperPressed = true;private void utton26ActionPerformed(java.awt.event.ActionEvent evt) /减if(oper !=0)dengyu();oper=2;elseString s = txtText.getText();number1 = Integer.parseInt(s);oper = 2;isOperPressed = true;private void utton31ActionPerformed(java.awt.event.ActionEvent evt) /加if(oper !=0)dengyu();oper=1;elseString s = txtText.getText();number1 = Integer.parseInt(s);oper = 1;isOperPressed = true;private void utton32ActionPerformed(java.awt.event.ActionEvent evt) / =dengyu();public void dengyu()/等于方法,在屏幕上显示出计算结果int number2;String s;isOperPressed = true;switch (oper) case 0:break;case 1:number2 = Integer.parseInt(txtText.getText();number1 = number1 + number2;s = Integer.toString(number1);txtText.setText(s);/显示到屏幕上,其他数字输出显示方式同oper = 0;break;private void utton15ActionPerformed(java.awt.event.ActionEvent evt) /数字9按键if (!isOperPressed)/原来的数*10再加上按上去的数显示到屏幕上String s = txtText.getText();int num = Integer.parseInt(s);num = num * 10 + 9;s = Integer.toString(num);txtText.setText(s); else /点击清屏并显示点击的数字txtText.setText("9");isOperPressed = false;private void utton14ActionPerformed(java.awt.event.ActionEvent evt) if (!isOperPressed) String s = txtText.getText();int num = Integer.parseInt(s);num = num * 10 + 8;s = Integer.toString(num);txtText.setText(s); else txtText.setText("8");isOperPressed = false;private void utton13ActionPerformed(java.awt.event.ActionEvent evt) if (!isOperPressed) String s = txtText.getText();int num = Integer.parseInt(s);num = num * 10 + 7;s = Integer.toString(num);txtText.setText(s); else txtText.setText("7");isOperPressed = false;private void utton20ActionPerformed(java.awt.event.ActionEvent evt) if (!isOperPressed) String s = txtText.getText();int num = Integer.parseInt(s);num = num * 10 + 6;s = Integer.toString(num);txtText.setText(s); else txtText.setText("6");isOperPressed = false;private void utton19ActionPerformed(java.awt.event.ActionEvent evt) if (!isOperPressed) String s = txtText.getText();int num = Integer.parseInt(s);num = num * 10 + 5;s = Integer.toString(num);txtText.setText(s); else txtText.setText("5");isOperPressed = false;private void utton18ActionPerformed(java.awt.event.ActionEvent evt) if (!isOperPressed) String s = txtText.getText();int num = Integer.parseInt(s);num = num * 10 + 4;s = Integer.toString(num);txtText.setText(s); else txtText.setText("4");isOperPressed = false;private void utton25ActionPerformed(java.awt.event.ActionEvent evt) if (!isOperPressed) String s = txtText.getText();int num = Integer.parseInt(s);num = num * 10 + 3;s = Integer.toString(num);txtText.setText(s); else txtText.setText("3");isOperPressed = false;private void utton24ActionPerformed(java.awt.event.ActionEvent evt) if (!isOperPressed) String s = txtText.getText();int num = Integer.parseInt(s);num = num * 10 + 2;s = Integer.toString(num);txtText.setText(s); else txtText.setText("2");isOperPressed = false;private void utton23MouseClicked(java.awt.event.MouseEvent evt) if (!isOperPressed) String s = txtText.getText();int num = Integer.parseInt(s);num = num * 10 + 1;s = Integer.toString(num);txtText.setText(s); else txtText.setText("1");isOperPressed = false;private void utton29ActionPerformed(java.awt.event.ActionEvent evt) if (!isOperPressed) String s = txtText.getText();int num = Integer.parseInt(s);num = num * 10 + 0;s = Integer.toString(num);txtText.setText(s); else txtText.setText("0");isOperPressed = false;private void jCheckBox2ActionPerformed(java.awt.event.ActionEvent evt) private void exitMenuItemActionPerformed(java.awt.event.ActionEvent evt) /GEN-FIRST:event_exitMenuItemActionPerformedSystem.exit(0);public static void main(String args) java.awt.EventQueue.invokeLater(new Runnable() public void run() new Calculator().setVisible(true););19 / 19