2013-08-01 37 views
-4

我需要一些幫助,使計算器按鈕能夠輸入多位數字。 謝謝。它幾乎完整,適用於學校項目。我一直無法找到問題的答案。如果有人知道我可以添加什麼,以允許使用按鈕的文本字段中的多位數字,它將不勝感激。Java計算器幾乎完成

//These are the imports that are used in the code 

import java.awt.Button; 
import java.awt.Dimension; 
import java.awt.EventQueue; 
import java.awt.Font; 
import java.awt.GridLayout; 
import java.awt.BorderLayout; 
import java.awt.Insets; 
import java.awt.TextField; 
import java.awt.event.ActionListener; 
import java.awt.event.ActionEvent; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.JTextField; 
import javax.swing.JButton; 
import java.awt.Container; 

public class calculatorfinal implements ActionListener{ 

    JFrame guiFrame; 
    JPanel buttonPanel; 
    JTextField tf1; 

    int calcOperation = 0; 
    int currentCalc; 
    int p = 0; 


    public static void main(String[] args) { 

     //Use the event dispatch thread for Swing components 
     EventQueue.invokeLater(new Runnable() 
     { 

      @Override 
      public void run() 
      { 

       new calculatorfinal();   
      } 
     }); 

    } 

    public calculatorfinal() 
    { 
     guiFrame = new JFrame(); 

     //This exits the program when the frame closes 
     guiFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     guiFrame.setTitle("calculator"); 
     guiFrame.setSize(300,350); 

     //This will centre the JFrame in the middle of the screen 
     guiFrame.setLocationRelativeTo(null); 
     //This sets up tf1 (text field 1), it allows it to be editable aligns the text to the  right 
     tf1 = new JTextField(); 
     tf1.setHorizontalAlignment(JTextField.RIGHT); 
     tf1.setEditable(true); 

     guiFrame.add(tf1, BorderLayout.NORTH); 

     buttonPanel = new JPanel(); 

     //Grid that has five rows and three columns 
     buttonPanel.setLayout(new GridLayout(6,3)); 
     guiFrame.add(buttonPanel, BorderLayout.CENTER); 

     //Adding the number buttons 

     for (int i=1;i<10;i++) 


     { 
     addButton(buttonPanel, String.valueOf(tf1.getText()+i)); 
     } 


     { 
      addButton(buttonPanel, String.valueOf(tf1.getText()+p)); 


     } 

     JButton buttonDecimal = new JButton("."); 
     buttonDecimal.addActionListener(this); 
     buttonPanel.add(buttonDecimal); 

     JButton buttonPlus = new JButton("+"); 
     buttonPlus.setFont(new Font("Verdana", Font.BOLD, 20)); 
     buttonPlus.setActionCommand("+"); 

     OperatorAction subAction = new OperatorAction(1); 
     buttonPlus.addActionListener(subAction); 

     JButton buttonMinus = new JButton("-"); 
     buttonMinus.setFont(new Font("Verdana", Font.BOLD, 20)); 
     buttonMinus.setActionCommand("-"); 

     OperatorAction addAction = new OperatorAction(2); 
     buttonMinus.addActionListener(addAction); 

     JButton buttonTimes = new JButton("*"); 
     buttonTimes.setFont(new Font("Verdana", Font.BOLD, 20)); 
     buttonTimes.setActionCommand("*"); 

     OperatorAction multiplyAction = new OperatorAction(3); 
     buttonTimes.addActionListener(multiplyAction); 

     JButton buttonDivide = new JButton("/"); 
     buttonDivide.setFont(new Font("Verdana", Font.BOLD, 20)); 
     buttonDivide.setActionCommand("/"); 

     OperatorAction ActionDivide = new OperatorAction(4); 
     buttonDivide.addActionListener(ActionDivide); 

     JButton buttonClear = new JButton("Clear"); 
     buttonClear.addActionListener(new ActionListener(){ 
      public void actionPerformed(ActionEvent e){ 
       tf1.setText(""); 
       //textfield.setText(null); 
      } 
     }); 

     JButton buttonEquals = new JButton("="); 
     buttonEquals.setFont(new Font("Verdana", Font.BOLD, 20)); 
     buttonEquals.setActionCommand("="); 
     buttonEquals.addActionListener(new ActionListener() 

     { 
      @Override 
      public void actionPerformed(ActionEvent event) 
      { 
       if (!tf1.getText().isEmpty()) 
       { 
        double number = Double.parseDouble(tf1.getText()); 
        if (calcOperation == 1) 
        { 
         double calculate = currentCalc + number; 
         tf1.setText(Double.toString(calculate)); 
        } 
        else if (calcOperation == 2) 
        { 
         double calculate = currentCalc - number; 
         tf1.setText(Double.toString(calculate)); 
        } 
        else if (calcOperation == 3) 

        { 
       double calculate = currentCalc * number; 
       tf1.setText(Double.toString(calculate)); 
       } 
    else if (calcOperation == 4) 

        { 
       double calculate = currentCalc/number; 
       tf1.setText(Double.toString(calculate)); 
       } 
}}}); 

     buttonPanel.add(buttonPlus); 
     buttonPanel.add(buttonMinus); 
     buttonPanel.add(buttonEquals); 
     buttonPanel.add(buttonTimes); 
     buttonPanel.add(buttonDivide); 
     buttonPanel.add(buttonClear); 

     guiFrame.setVisible(true); 
    } 

    //All the buttons are following the same pattern 
    //so create them all in one place. 
    private void addButton(Container parent, String name) 
    { 
     JButton but = new JButton(name); 
     but.setActionCommand(name); 
     but.addActionListener(this); 
     parent.add(but); 
    } 

    //As all the buttons are doing the same thing it's 
    //easier to make the class implement the ActionListener 
    //interface and control the button clicks from one place 
    @Override 
    public void actionPerformed(ActionEvent event) 
    { 
     //get the Action Command text from the button 
     String action = event.getActionCommand(); 

     //set the text using the Action Command text 
     tf1.setText(action);  
    } 

    private class OperatorAction implements ActionListener 
    { 
     private int operator; 

     public OperatorAction(int operation) 
     { 
      operator = operation; 
     } 

     public void actionPerformed(ActionEvent event) 
     { 
      currentCalc = Integer.parseInt(tf1.getText()); 
      calcOperation = operator; 
     } 
    } 
} 
+1

這是非常困難的,因爲格式的讀取。請刪除所有不必要的空白行。 – chrylis

+2

請將您發佈的代碼減少到提供問題的基本要素。然後準確解釋你的問題。這不是一個「請爲我做我的工作」問題的論壇。您需要顯示您嘗試過的方式,如何無法使用以及您需要幫助的確切位置。 –

回答

0

您的值設置爲行動,而不是以前的價值和作用

tf1.setText(action) 

應該

tf1.setText(tf1.getText() + action) 
+0

非常感謝,會代表你,但由於我沒有15聲望,所以完全解決了我的問題。對不起,沒有任何適當的格式 – user2640158