【问题标题】:java calculator decimal pointjava计算器小数点
【发布时间】:2014-01-19 09:33:29
【问题描述】:

我有使用 java 的完全可用的计算器。可以告诉我如何添加小数点。我已经有了按钮并且变量是 double 类型。我只是无法使按钮工作。 我尝试自己做,但每次都收到错误消息。 代码如下:

package oop;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class Kalkulator2 extends Applet {
String arg1= "", arg2="";
double ergebnis;
Button zahl[] =new Button[10];
Button funktion[] = new Button[4];
Button ausfuehren;
Button decimalpoint;
char dec='.';
Panel zahlPanel,funktionPanel,ergebnisPanel;
TextField ergebnisFeld = new TextField(5);
int operationArgument;
char operation;
public void init () {
    operationArgument= 1; operation =' ';
    setLayout(new BorderLayout());
    zahlPanel = new Panel();
    zahlPanel.setLayout(new GridLayout (4,3));
    for (int i=9; i>=0; i--) {
        zahl[i] = new Button(String.valueOf(i));
        zahl[i].addActionListener(new ButtonZahlen());
        zahlPanel.add(zahl[i]);
    }
    decimalpoint = new Button(String.valueOf(dec));   //decimal point

    //decimalpoint.addActionListener(new Button ());
    ausfuehren = new Button("=");
    ausfuehren.addActionListener(new ButtonAusfuehren()); //zu dem Listener
    zahlPanel.add(decimalpoint);
    zahlPanel.add(ausfuehren);

    add("Center",zahlPanel);
    funktionPanel = new Panel();
    funktionPanel.setLayout(new GridLayout(4,1));
    funktion[0] = new Button("+");
    funktion[0].addActionListener(new ButtonOperation());
    funktionPanel.add(funktion[0]);
    funktion[1] = new Button("-");
    funktion[1].addActionListener(new ButtonOperation());
    funktionPanel.add(funktion[1]);
    funktion[2] = new Button("*");
    funktion[2].addActionListener (new ButtonOperation());
    funktionPanel.add(funktion[2]);
    funktion[3] = new Button("/");
    funktion[3].addActionListener (new ButtonOperation());
    funktionPanel.add(funktion[3]);



    add("East",funktionPanel);
    ergebnisPanel = new Panel();

    ergebnisPanel.add(ergebnisFeld);
    add("North",ergebnisPanel);

}
class ButtonZahlen implements ActionListener{
    public void actionPerformed(ActionEvent e) {
        switch (operationArgument)   {
        case 1 :  {
            arg1+=e.getActionCommand();
            ergebnisFeld.setText(arg1);
            break;
        }
        case 2 :   {
            arg2 +=e.getActionCommand();
            ergebnisFeld.setText(arg2);
            break;
        }
        default: { }

        }
        }
    }
class ButtonAusfuehren implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        if(operation =='+')
                ergebnis = new Double(arg1) + new Double(arg2);
        else if (operation == '-') 
            ergebnis = new Double(arg1) - new Double(arg2);



        else if(operation =='*') 
                ergebnis = new Double(arg1) * new Double(arg2);

        else if(operation =='/')
                ergebnis = new Double(arg1) / new Double(arg2);

        ergebnisFeld.setText(String.valueOf(ergebnis));


        }


    }


class ButtonOperation implements ActionListener{
    public void actionPerformed(ActionEvent e) {
        if(e.getActionCommand().equals("+")) {
            operation = '+'; operationArgument = 2;
            }
        else if(e.getActionCommand().equals("-")) {
                operation = '-'; operationArgument = 2;
                }
        else if(e.getActionCommand().equals("*")) {
                    operation = '*' ; operationArgument =2;
                    }
        else if(e.getActionCommand().equals("/")) {
                        operation = '/' ; operationArgument =2;
        }
                    }

            }
        }


public void paint(Graphics g){   }


}

【问题讨论】:

标签: java awt decimal calculator point


【解决方案1】:

当按钮被点击时,它试图创建一个没有实现 actionListener 的新按钮对象。因此它会抛出一个错误,说“当我需要一个带有'actionPerformed'方法的对象时,我必须用新按钮做什么”这是一个可能的解决方案;

// create button object
decimalpoint = new Button(".");

// not good : decimalpoint.addActionListener(new Button ());
// event on click
decimalpoint.addActionListener(new YourClassName());

YourClassName是处理按钮事件的实例

class YourClassName implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        // add decimal point
    }
}

我也同意 Andrew Thompson 的观点,即 AWT 不是处理任务的首选方式。如果你的老师建议你使用 AWT,那么请使用 Swing。 Swing 比 AWT 好得多,应该对第一次编写基于 GUI 的 java 的人进行教育。

【讨论】:

  • 我在大学的第一个学期。而且老师还没有教我们swing或jframe。所以我必须这样做。是的,我看到的任何地方都没有人使用applet或awt.无论如何感谢您的快速响应。
  • 如果我的回复对您有所帮助,请将其标记为同样使用 AWT 并遇到按钮事件问题的人的答案。现在,希望以后老师会教你使用Swing。我的第一次 GUI 体验是 Swing(取决于不同的讲解员)。如果您没有学习 Swing 课程,我建议您通过教程自己学习。它可能会在未来的应用程序中为您提供很多帮助(并使其更容易)。祝你学习编程好运。
  • 是的。我已经完成了。现在一切正常 100%。所以你的回复对我很有帮助。我只是不知道如何将问题标记为已回答:D。
  • “而且老师还没有教我们swing或jframe。”我会强调,我确实希望参考那篇文章的老师。他们根本不应该教小程序,而教他们 AWT 是在浪费学生的时间。如果他们正在使用“准备好的文本”,那么他们应该更新这些文本。
【解决方案2】:

要回答这个问题,在 java 代码中添加一个小数点(我的例子是 GUI NetBeans IDE 8.0.2)我偶然发现了这段代码。我必须承认我在网上寻找答案时没有遇到此代码。

 private void PointActionPerformed(java.awt.event.ActionEvent evt) {                                      

      txtDisplay.setText(txtDisplay.getText()+Point.getText()); 
}

【讨论】:

    【解决方案3】:

    你可以简单地做到这一点

    指定按钮 -

    Button Decimal;
    

    将您在 xml 文件中指定的按钮强制转换为 (Button)Decimal -

    Decimal = findViewById(R.id.the id you gave to the button);
    

    现在设置点击监听器

    Decimal.setOnClickListener(new View.OnClickListener() {
                 @Override
                 public void onClick(View v) {
    
                 edit.setText(edit.getText().toString() + ".");
    }
    

    其中 edit 是您希望填充文本的字段。

    【讨论】:

    • 这个问题是关于 Java Applets 而不是 Android 应用开发
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-14
    • 2013-04-07
    • 1970-01-01
    相关资源
    最近更新 更多