import java.awt.*;
import java.awt.event.*;
import java.text.DecimalFormat;  //用于格式化十进制数字
import javax.swing.*;public class Calculator {
     private JFrame frame;  //定义frame为JFrame类型  下同
     private JPanel panel, panelKeys,panelKeys_up,panelKeys_down; //JPanel 是一般轻量级容器
     
     private JTextField textCompute;     //计算区域
     //JTextField 是一个轻量级组件,它允许编辑单行文本
     
     private JButton buttonBK, buttonC ; //定义退格键和清零键
     private JButton button[];  //定义数字键
     private JButton buttonDot,buttonAddandSub,buttonAdd,buttonSub,buttonMul,
                     buttonDiv,button1,button2,button3,buttonEqual; //运算符键
     
     private double result;     //运算结果
     private final short Add=1;  //+  加
     private final short Sub=2;//-减
     private final short Mul=3;  //× 乘
     private final short Div=4;   //除
     private short operator =-1;   //运算符
     
     public Calculator()
  {
       frame =new JFrame("计算器");
       panel =new JPanel();
      frame.getContentPane().add(panel);  //全局面板
       
       
       ActionListener listener =new ComputeActionListener();
       
       textCompute=new JTextField(15);//有关使用文本字段的信息和示例
       textCompute.setText(""); //将此 TextComponent 文本设置为指定文本
       textCompute.setEditable(false);  //设置为不可编辑
       textCompute.setBackground(Color.WHITE);
       
       //功能键上半部
       panelKeys_up=new JPanel();
       panelKeys_up.setLayout(new FlowLayout(FlowLayout.RIGHT)); 
       //FlowLayout流布局用于安排有向流中的组件,这非常类似于段落中的文本行 
       buttonBK=new JButton("BACK");
       buttonBK.setForeground(Color.black);
       buttonC=new JButton("C");
       buttonC.setForeground(Color.black);
       buttonBK.addActionListener(listener); //注册监听器
       buttonC.addActionListener(listener);
       panelKeys_up.add(buttonBK);
       panelKeys_up.add(buttonC);
       
       //功能键下半部
       panelKeys_down=new JPanel();
       panelKeys_down.setLayout(new GridLayout(4,5));
       button=new JButton[10];
       for(int i=0;i<10;i++) //定义数字键0-9
       {
        button[i]=new JButton(Integer.toString(i));//toString : 返回一个表示指定整数的 String 对象
           button[i].setForeground(Color.black);
       }
       buttonDot=new JButton(".");
       buttonDot.setForeground(Color.black);
       buttonAddandSub=new JButton("+/-");
       buttonAddandSub.setForeground(Color.black);
       buttonAdd=new JButton("+");
       buttonAdd.setForeground(Color.black);
       buttonSub=new JButton("-");
       buttonSub.setForeground(Color.black);
       buttonDiv=new JButton("/");
       buttonDiv.setForeground(Color.black);       
       buttonMul=new JButton("X");
       buttonMul.setForeground(Color.black);
       buttonEqual=new JButton("=");
       buttonEqual.setForeground(Color.black);
       
       button1=new JButton("");
       button1.setForeground(Color.black);
       button2=new JButton("");
       button2.setForeground(Color.black);
       button3=new JButton("");
       button3.setForeground(Color.black);
       
     
       
       panelKeys_down.add(button[1]);
       panelKeys_down.add(button[2]);
       panelKeys_down.add(button[3]);
       panelKeys_down.add(buttonDiv);
       panelKeys_down.add(buttonAddandSub);
       panelKeys_down.add(button[4]);
       panelKeys_down.add(button[5]);
       panelKeys_down.add(button[6]);
       panelKeys_down.add(buttonMul);
       panelKeys_down.add(button2);
       panelKeys_down.add(button[7]);
       panelKeys_down.add(button[8]);
       panelKeys_down.add(button[9]);
       panelKeys_down.add(buttonSub);
       panelKeys_down.add(button3);
       panelKeys_down.add(button1);
       panelKeys_down.add(button[0]);
       panelKeys_down.add(buttonDot);
       panelKeys_down.add(buttonAdd);
       panelKeys_down.add(buttonEqual);
       
       /*for(int i=0;i<10;i++) //定义数字键0-9
       {
        button[i].addActionListener(listener);//注册数字0-9
       }*/
       button[0].addActionListener(listener);
       button[1].addActionListener(listener);
       button[2].addActionListener(listener);
       button[3].addActionListener(listener);
       button[4].addActionListener(listener);
       button[5].addActionListener(listener);
       button[6].addActionListener(listener);
       button[7].addActionListener(listener);
       button[8].addActionListener(listener);
       button[9].addActionListener(listener);
       
       buttonDot.addActionListener(listener);
       buttonAddandSub.addActionListener(listener);
       buttonSub.addActionListener(listener);
       buttonAdd.addActionListener(listener);
       buttonDiv.addActionListener(listener);
       buttonMul.addActionListener(listener);
       buttonEqual.addActionListener(listener);       //加到功能面板
       panelKeys=new JPanel();
       panelKeys.setLayout(new BorderLayout());//这是一个布置容器的边界布局,它可以对容器组件进行安排
       panelKeys.add(panelKeys_up,BorderLayout.NORTH);
       panelKeys.add(panelKeys_down,BorderLayout.CENTER);
       
       //加入到全局面板
       frame.setLayout(new BorderLayout());
       frame.add(textCompute,BorderLayout.NORTH);
       frame.add(panelKeys,BorderLayout.SOUTH);
       
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭是不执行任何操作
       frame.setForeground(Color.blue);
       frame.setSize(250,200);
       frame.setResizable(false);
       frame.setVisible(true);       //按键监听器
      
  }
     
     class ComputeActionListener implements ActionListener
     {
      /*  ActionEvent指示发生了组件定义的动作的语义事件。当特定于组件的动作(比如被按下)发生时,
      由组件(比如 Button)生成此高级别事件。事件被传递给每一个 ActionListener 对象,
      这些对象是使用组件的 addActionListener 方法注册的,用以接收这类事件。 */
      public void actionPerformed(ActionEvent event)
      {
      Object keyButton= event.getSource(); //最初发生 Event 的对象。
      String text=textCompute.getText(); //getText 返回此 TextComponent 中包含的文本
      DecimalFormat df=new DecimalFormat("0.########");//用于格式化十进制数字  //设置数据输出格式
      
      //Back键
      if(keyButton==buttonBK&&text.length()>0)
      {
      textCompute.setText(text.substring(0,text.length()-1));
      }
             //C键
      if(keyButton==buttonC)
      {
      result=0;
      textCompute.setText("");
      }
      //数字键
      for(int i=0;i<=9;i++)
      {
      if(keyButton==button[i]);
      {
      textCompute.setText(text+i);
      }
      }
      
    if ((keyButton == buttonAdd || keyButton == buttonSub || keyButton == buttonMul || keyButton == buttonDiv || keyButton == buttonEqual)) {
          switch (operator) {
          case Add:
          result += Double.parseDouble(text);
          break;
          case Sub:
          result -= Double.parseDouble(text);
          break;
          case Mul:
          result *= Double.parseDouble(text);
          break;
          case Div:
          if (Double.parseDouble(text) == 0) {
          textCompute.setText("除数不能为零");
          } 
          else
          result /= Double.parseDouble(text);
          break;
          default:
          result = Double.parseDouble(text);
          }
         
          textCompute.setText("");
         
          if (keyButton == buttonAdd) {
          operator = Add;
          }
          if (keyButton == buttonSub) {
          operator = Sub;
          }
          if (keyButton == buttonMul) {
          operator = Mul;
          }
          if (keyButton == buttonDiv) {
          operator = Div;
          }
          if (keyButton == buttonEqual) {
          operator = 0;
              textCompute.setText(df.format(result));
          }
         } 
 
      if (keyButton == buttonDot && text.length() > 0 && text.indexOf(".") < 0) {
              textCompute.setText(text + ".");
             }   
             
             //+/-号键
             if (keyButton == buttonAddandSub && text.length() > 0) {
              double value = - Double.parseDouble(text);
              textCompute.setText(df.format(value));
             }
      
      }
     }
     public static void main(String[] args)
     {
      new Calculator();
     } 
}写了个计算器程序,编译没有问题,不过按键都是得到数字9,请高手指示
问题可能在红色代码部分。

解决方案 »

  1.   


    // 数字键
    for (int i = 0; i <= 9; i++) {
    if (keyButton == button[i])
    //这个分号是干啥的?????
    ;
    {
    textCompute.setText(text + i);
    }
    }
      

  2.   

    刚刚学java。原来就是一个分号的问题,看了很久,没有看出来
      

  3.   

    再问一个问题,把
     if ((keyButton == buttonAdd || keyButton == buttonSub || keyButton == buttonMul || keyButton == buttonDiv || keyButton == buttonEqual)) 
    { switch (operator)
     { case Add: result += Double.parseDouble(text); break; 
    case Sub: result -= Double.parseDouble(text); break; 
    case Mul: result *= Double.parseDouble(text); break; 
    case Div: 
    if (Double.parseDouble(text) == 0) 
    { textCompute.setText("除数不能为零"); } 
    else result /= Double.parseDouble(text); break;
     default: result = Double.parseDouble(text); }
     textCompute.setText(""); 
    if (keyButton == buttonAdd) 
    { operator = Add; }
     if (keyButton == buttonSub) { operator = Sub; }
     if (keyButton == buttonMul) { operator = Mul; }
     if (keyButton == buttonDiv) { operator = Div; }
     if (keyButton == buttonEqual) 
    { operator = 0; textCompute.setText(df.format(result)); } } 换成if(keyButton==buttonAdd)
       {
       result+=Double.parseDouble(text);  //将String转为double
       textCompute.setText("");
       }
       if(keyButton==buttonSub)
       {
       result-=Double.parseDouble(text);  //将String转为double
       textCompute.setText("");
       }
       if(keyButton==buttonMul)
       {
       result*=Double.parseDouble(text);  //将String转为double
       textCompute.setText("");
       }
    //    if(keyButton==buttonDiv)
       {
       result/=Double.parseDouble(text);  //将String转为double
       textCompute.setText("");
       }
       if(keyButton==buttonEqual)
       {
       result=Double.parseDouble(text);  //将String转为double
       textCompute.setText("");
       }可不可以??