Java计算器小数点



我使用java有完全工作的计算器。。我尝试自己做,但每次都会收到错误消息。这是代码:

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){   }

}

单击按钮时,它正在尝试创建一个不实现ActionListener的新按钮对象。因此,它会出现一个错误,说" 我必须使用一个新按钮在我需要一个带有'Action Performed'方法的对象的情况下做什么",这是一个可能的解决方案;

// 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。秋千比AW好得多,应该对第一次写GUI Java的人进行教育。

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

 private void PointActionPerformed(java.awt.event.ActionEvent evt) {                                      
      txtDisplay.setText(txtDisplay.getText()+Point.getText()); 
}

您可以做到这一点

指定按钮 -

Button Decimal;

种姓您在XML文件中指定的按钮为(按钮)十进制 -

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() + ".");
}

在哪里编辑是您要填写文本的字段。

最新更新