标题:用 java 写一个计算器 求帮忙把代码补全 需补处有注释
只看楼主
chenwei926fl
Rank: 1
来 自:宜昌
等 级:新手上路
帖 子:16
专家分:0
注 册:2013-4-4
结帖率:50%
 问题点数:0 回复次数:0 
用 java 写一个计算器 求帮忙把代码补全 需补处有注释
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;
import javax.swing.JComboBox;


public class GUI_Calculator extends JFrame{
    //Define the components we need to use
    private JTextField txtNum1,txtNum2,txtResult;
    private JButton btnCal;
    private JComboBox cmb_operater;
   
    @SuppressWarnings("rawtypes")
    public GUI_Calculator()
    {
        this.setLayout(new FlowLayout(FlowLayout.LEFT));
        this.setSize(250, 180);
        //Initialize the Frame Information
        txtNum1 = new JTextField();  
        this.txtNum1.setColumns(8);
        
        cmb_operater = new JComboBox();
        this.cmb_operater.addItem("+");
        this.cmb_operater.addItem("-");
        this.cmb_operater.addItem("*");
        this.cmb_operater.addItem("/");
        //add Event handler to the "cmb_operater" to make it response to the selection.
        
        //Here needs the code
        
        
        //end of the code
        
        
        txtNum2 = new JTextField();
        txtNum2.setColumns(8);
        txtResult = new JTextField();
        txtResult.setColumns(8);
        txtResult.setEnabled(false);
        
        this.btnCal = new JButton("OK");
        this.btnCal.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e){
                double num1,num2,result=0;
                char operator;
                if(e.getSource()==btnCal)
                {
                    num1 = Double.parseDouble(txtNum1.getText());
                    num2 = Double.parseDouble(txtNum2.getText());
                    
                    //get the operator,needs code Here
                    operator = **********************;
                    switch(operator)
                    {
                    case '+':result = num1+num2;break;
                    case '-':result = num1-num2;break;
                    case '*':result = num1*num2;break;
                    case '/':result = num1/num2;break;
                    }
                    
                    txtResult.setText(String.valueOf(result));
                }
            }
        });
        
        this.add(txtNum1);
        this.add(cmb_operater);
        this.add(txtNum2);
        this.add(new JLabel(" = "));
        this.add(txtResult);
        this.add(btnCal);
        
        this.setVisible(true);
    }
   
   
   
    public static void main(String[] args){
        GUI_Calculator gc = new GUI_Calculator();
        
    }

}
搜索更多相关主题的帖子: java 计算器 public private import 
2014-10-10 22:06



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-437086-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.189658 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved