2013-01-20 40 views
4

我需要添加一個「清除計算器」的按鈕,以及一個在butPanel上退出程序的按鈕。它還需要非常基本的Java代碼,因爲我是一個初學者,並且有一個可怕的comp.sci。老師。我有一個有退出按鈕的代碼示例,但我不確定如何將它放入我的程序中。我已經嘗試了很多。 此外,如果有更好的方式來「錯誤檢查」,將不勝感激。如何向該程序添加退出按鈕?如何「清除」?

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 

public class Calculator2 extends JFrame implements ActionListener 
{ 
    JLabel heading = new JLabel ("2. Calculator"); 
    JLabel intro1 = new JLabel ("This program stimulates a four-function calculator."); 
    JLabel intro2 = new JLabel ("It takes an operator and a number. It can add, subtract,"); 
    JLabel intro3 = new JLabel (" multiply and divide. Enter in the format eg. '+35'. "); 
    JLabel inLabel = new JLabel ("  Operation: "); 
    JLabel outLabel = new JLabel ("  Total:   "); 
    JTextField inOper = new JTextField (7); 
    JTextField outTota = new JTextField (7); // intro 

//panels 
JPanel titlePanel = new JPanel(); 
JPanel intro1Panel = new JPanel(); 
JPanel intro2Panel = new JPanel(); 
JPanel intro3Panel = new JPanel(); 
JPanel operPanel = new JPanel(); 
JPanel totaPanel = new JPanel(); 
JPanel butPanel = new JPanel(); 

String operTemp; 
String totaTemp; 

public Calculator2() 
{ 
    setTitle ("C - 2."); 
    inOper.addActionListener (this); 
    outTota.setEditable (false); 
    getContentPane().setLayout (new FlowLayout()); 

    titlePanel.add (heading); 
    intro1Panel.add (intro1); 
    intro2Panel.add (intro2); 
    intro3Panel.add (intro3); 
    operPanel.add (inLabel); 
    operPanel.add (inOper); 
    totaPanel.add (outLabel); 
    totaPanel.add (outTota); //adds components to panels 

    getContentPane().add (titlePanel); 
    getContentPane().add (intro1Panel); 
    getContentPane().add (intro2Panel); 
    getContentPane().add (intro3Panel); 
    getContentPane().add (operPanel); 
    getContentPane().add (totaPanel); //Adds panels to Frame 

    setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); 
} 


public static int isInteger (String input) 
{ 
    try 
    { 
     Integer.parseInt (input); 
     return Integer.parseInt (input); 
    } 
    catch (NumberFormatException nfe) 
    { 
     return 0; 
    } 
} //isInteger method 


// The application 
public String calculate (String operation, String newtotal) 
{ 
    int total = isInteger (newtotal); 
    String totalS; 
    char operator; 
    int number = 0; 
    operator = operation.charAt (0); 

    if (operator == '+') 
    { 
     number = isInteger (operation.substring (1)); 
     total = total + number; 
     totalS = Integer.toString (total); 
    } 


    else if (operator == '-') 
    { 
     number = isInteger (operation.substring (1)); 
     total = total - number; 
     totalS = Integer.toString (total); 
    } 


    else if (operator == '*') 
    { 
     number = isInteger (operation.substring (1)); 
     total = total * number; 
     totalS = Integer.toString (total); 
    } 


    else if (operator == '/') 
    { 
     number = isInteger (operation.substring (1)); 
     total = total/number; 
     totalS = Integer.toString (total); 
    } 
    else 
    { 
     totalS = ("ERROR"); 
    } 

    if (number == 0) 
    { 
     totalS = ("ERROR"); 
    } 
    return totalS; 
} // calculate method 


public void actionPerformed (ActionEvent evt) 
{ 
    String userIn = inOper.getText(); 
    String totalIn = outTota.getText(); 
    try 
    { 
     totaTemp = calculate (userIn, totalIn); 
     outTota.setText (totaTemp + ""); 
    } 
    catch (Exception ex) 
    { 
     outTota.setText ("ERROR"); 
    } 
    repaint(); 
} 


public static void main (String[] args) 
{ 
    Calculator2 calc = new Calculator2(); 
    calc.setSize (350, 350); 
    calc.setResizable (false); 
    calc.setVisible (true); 
} 
} 

回答

8

首先聲明的按鈕並進行初始化:

JButton exitButton = new JButton("Exit"); 
JButton clearButton = new JButton("Clear"); 

然後,確保butPanel有一個佈局和麪板添加到內容窗格:

butPanel.setLayout(new FlowLayout()); 
getContentPane().add(butPanel); 

然後加入在新的按鈕動作監聽器:

exitButton.addActionListener(new ActionListener() { 
    public void actionPerformed(ActionEvent e) { 
    System.exit(0); 
    } 
}); 
clearButton.addActionListener(new ActionListener() { 
    public void actionPerformed(ActionEvent e) { 
    // TODO: clear the stuff here 
    } 
}); 
+2

+1 JPanel默認在API中實現FlowLayout :-) – mKorbel

+0

謝謝!我想我忽略了:) –

+0

是的,我有這一切,但我不知道在哪裏把它放在我的代碼:$ – javanoob

相關問題