有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

计算器如何在Java中使用按钮将两个输入放在两个单独的文本字段中?

我正在使用jswing构建一个基本的计算器,我有两个文本字段,一个用于输入#1,一个用于输入#2。问题是,我怎样才能继续输入,这样当我输入完第一个输入后,它就可以进入下一个文本字段进行第二次输入?而且,在操作算术公式时,似乎存在错误,输出显示错误的答案或两个输入的总和

import java.awt.event.*;


class Kalkuleytor implements ActionListener
{
    JFrame f;
    JTextField t1, t2, t3;
    JLabel lbl1, lbl2, lbl3, lbl4;
    JButton one,two,three,four,five,six,seven,eight,nine,zero,add,mltply,sbtrct,dvd,equal;

    static double a=0,b=0,c=0,result=0;
    static int operator=0;

    Kalkuleytor()
    {
        f=new JFrame("Calculator");
        t1=new JTextField();
        t2=new JTextField();
        t3=new JTextField();
        lbl1=new JLabel("Number 1");
        lbl2=new JLabel("Number 2");
        lbl3=new JLabel("");
        lbl4=new JLabel("Total");
        one=new JButton("1");
        two=new JButton("2");
        three=new JButton("3");
        four=new JButton("4");
        five=new JButton("5");
        six=new JButton("6");
        seven=new JButton("7");
        eight=new JButton("8");
        nine=new JButton("9");
        zero=new JButton("0");
        dvd=new JButton("/");
        mltply=new JButton("*");
        sbtrct=new JButton("-");
        add=new JButton("+");
        equal=new JButton("=");

        //setBounds and add of buttons

        f.setLayout(null);
        f.setVisible(true);
        f.setSize(517,502);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setResizable(false);

        one.addActionListener(this);
    //ActionListener for all button
    }

    public void actionPerformed(ActionEvent e)
    {
        if(e.getSource()==one)
            t1.setText("1");

        if(e.getSource()==two)
            t1.setText("2");

        if(e.getSource()==three)
            t1.setText("3");

        if(e.getSource()==four)
            t1.setText("4");

        if(e.getSource()==five)
            t1.setText("5");

        if(e.getSource()==six)
            t1.setText("6");

        if(e.getSource()==seven)
            t1.setText("7");

        if(e.getSource()==eight)
            t1.setText("8");

        if(e.getSource()==nine)
            t1.setText("9");

        if(e.getSource()==zero)
            t1.setText("0");

        if(e.getSource()==add)
        {
            a=Double.parseDouble(t1.getText());
            operator=1;
            lbl3.setText("+");
        } 

        if(e.getSource()==sbtrct)
        {
            a=Double.parseDouble(t1.getText());
            operator=2;
            lbl3.setText("-");
        }

        if(e.getSource()==mltply)
        {
            a=Double.parseDouble(t1.getText());
            operator=3;
            lbl3.setText("*");
        }

        if(e.getSource()==dvd)
        {
            a=Double.parseDouble(t1.getText());
            operator=4;
            lbl3.setText("/");
        }

        if(e.getSource()==equal)
        {
            c=Double.parseDouble(t1.getText());

            switch(operator)
            {
                case 1: result=a+b;
                    break;

                case 2: result=a-b;
                    break;

                case 3: result=a*b;
                    break;

                case 4: result=a/b;
                    break;

                default: result=0;
            }

            t3.setText(""+result);
        }       
    }

    public static void main(String[]args)
    {
        new Kalkuleytor();
    }
} 

enter image description here


共 (3) 个答案

  1. # 1 楼答案

    此代码中存在多个问题

    • 当输入的值大于9(操作员按钮前有两个按钮)时,只剩下最后一位数字
    • 有两个文本字段t1和t2。在按钮0到9上,填充t1,但从不填充t2
    • 在按钮+-*/上,将t1中的文本转换为变量a中的值,但从不将t2中的文本转换为变量b

    文本字段用于显示,因此您可以更好地使用字符串类型的单独变量来注册来自按钮0-9的输入,并将它们连接起来。根据布尔值填充t1或t2

    添加变量

    String input = "";
    boolean firstInput = true;
    
    if(e.getSource()==one)
       t1.setText("1");
    

    将成为

    if(e.getSource()==one){
        input += "1";
        if (firstInput ) {
            t1.setText(input);
        } 
        else {
            t2.setText(input);
        }
    }
    

    关于运营商的变化

    if(e.getSource()==add) {
       a=Double.parseDouble(t1.getText());
       operator=1;
       lbl3.setText("+");
    } 
    
    if(e.getSource()==add) {
       a=Double.parseDouble(input);
       operator=1;
       lbl3.setText("+");
       firstInput = false;
       input = "";
    } 
    

    在等号上

            if(e.getSource()==equal)
            {
                c=Double.parseDouble(t1.getText());
    
                switch(operator)
                {
                    case 1: result=a+b;
                        break;
    
                    case 2: result=a-b;
                        break;
    
                    case 3: result=a*b;
                        break;
    
                    case 4: result=a/b;
                        break;
    
                    default: result=0;
                }
    
                t3.setText(""+result);
            }       
        }
    

    if(e.getSource()==equal) {
       b=Double.parseDouble(input);
    
       switch(operator)
       {
        ....
       }
       t3.setText(""+result);
       firstInput = false;
       input = "";
    }
    
  2. # 2 楼答案

    1. 要将重点放在输入上,可以使用^{}

    2. 错误的结果可能是因为在equal中使用了错误的变量,如果将文本读入c并在计算中使用b

      if (e.getSource() == equal) {
          b = Double.parseDouble(t1.getText());
          switch (operator) {
          //...
      

    此外,你可以简化你的代码,你可以很容易地重构它:

    • 使用else if如果可以,比如从按钮获取数字,它只能是一种情况,不要尝试所有情况,对于操作员也是一样

      if(e.getSource()==one)
          t1.setText("1");
      else if(e.getSource()==two)
          t1.setText("2");
      
    • 您可以直接从中读取JButton文本并使用它,不要尝试所有的可能性

      Object source = e.getSource();
      if (source instanceof JButton) {
          JButton btn = (JButton) source;
          String textStr = btn.getText();
          if (textStr.matches("\\d")) {
              t1.setText(textStr);
          } else {
              lbl3.setText(textStr);
              a = Double.parseDouble(t1.getText());
              operator = textStr;
          }
      }
      
    • 您也可以直接使用String来存储运算符,不必麻烦您传递给int

      if (e.getSource() == equal) {
          b = Double.parseDouble(t1.getText());
          switch (operator) {
              case "+":
                  result = a + b;
                  break;
              case "-":
                  result = a - b;
                  break;
              case "*":
                  result = a * b;
                  break;
              case "/":
                  result = a / b;
                  break;
              default:
                  result = 0;
          }
          t3.setText("" + result);
      }
      

    这个full code is here


    使用更具体的功能,如BiFunction,您可以:

        Map<String, BiFunction<Double, Double, Double>>  operators = new HashMap<>() {{
            put("+", (d1, d2) -> d1 + d2);
            put("-", (d1, d2) -> d1 - d2);
            put("*", (d1, d2) -> d1 * d2);
            put("/", (d1, d2) -> d1 / d2);
        }};
    

            } else {     // For = button
                b = Double.parseDouble(t1.getText());
                result = operators.getOrDefault(operator, (d1, d2) -> 0.0).apply(a, b);
                t3.setText("" + result);
            }