2014-01-19 44 views
0

我一直在使用java.Can告訴我如何添加小數point.I完全工作計算器已經有按鈕和變量在類型double.I只是不能使按鈕的工作。 我試圖自己做,但我每次都收到錯誤消息。 下面是代碼: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){ } 


} 
+1

和錯誤消息? –

+3

1)爲什麼要編寫一個小程序?如果這是由於規格。由老師,請參考[爲什麼CS老師應該停止教Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/)。 2)爲什麼選擇AWT而不是Swing?看到我對[Swing extras over AWT]的回答(http://stackoverflow.com/a/6255978/418556)有很多很好的理由放棄使用AWT組件。 –

回答

2

當得到點擊按鈕,它試圖創建一個新的按鈕對象不實現一個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 
    } 
} 

我也同意安德魯·湯普森說AWT不是處理你的任務的首選方法的實例。如果你的老師建議你使用AWT,那麼請使用Swing。 Swing比AWT好得多,應該接受第一次編寫基於GUI的Java的人的教育。

+0

我在university.And第一學期老師還沒教完我們蕩起或JFrame的yet.So我必須這樣做,這way.And呀,我到處都看了沒有人使用的小程序或awt.Anyway感謝你的快速響應。 –

+0

如果我的回覆幫助了您,請將其標記爲正在使用AWT並遇到按鈕事件問題的人的答案。現在,我希望老師會教你以後再使用Swing。我的第一個GUI體驗是Swing(它依賴於講解員來講課)。 如果您沒有獲得與Swing的課程,我建議使用教程,自己學習。它可能會在未來的應用程序中幫助你很多(並使其更容易)。學習編程祝你好運。 – KarelG

+0

是的,我已經做到了。現在所有的東西都是100%的工作。所以你的回答真的幫助了我。我只是不知道如何標記問題爲答案:D。 –

0

要回答這個問題,到小數點添加到Java代碼(我的例子是GUI NetBeans IDE的8.0.2)我有過這個代碼絆倒了。我必須承認,我沒有遇到過這個代碼在網上尋找答案。

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

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