2013-06-24 38 views
0

我有一個稱爲輸入的整數轉換爲字符串字符串,我有我的oneButton.addActionListener設置和public void actionPerformed(ActionEvent e){我想要我的計算器的整數當我按下我的JButton中的一個時

如果(e.getSource()== oneButton){

if

}}

,但在我的計算器數量保持爲0,任何幫助

CODE:

//everything is imported 

public class Ken implements ActionListener{ 


static JButton oneButton = new JButton("1"); 
static JButton twoButton = new JButton("2"); 
static JButton threeButton = new JButton("3"); 
static JButton fourButton = new JButton("4"); 
static JButton fiveButton = new JButton("5"); 
static JButton sixButton = new JButton("6"); 
static JButton sevenButton = new JButton("7"); 
static JButton eightButton = new JButton("8"); 
static JButton nineButton = new JButton("9"); 
static JButton zeroButton = new JButton("0"); 
static JButton minusButton = new JButton("-"); 
static JButton timesButton = new JButton("*"); 
static JButton enterButton = new JButton("="); 
static JButton dividButton = new JButton("/"); 
static JButton plusButton = new JButton("+"); 
public static int input = 0;  


public Ken(){ 

    JFrame frame = new JFrame("MyCalc"); 


    String string = ""+input; 

    JTextField text = new JTextField(string, 9); 
    frame.setSize(476, 500); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.setVisible(true); 
    frame.setResizable(false); 
    FlowLayout fl = new FlowLayout(0, 30, 20); 
    setLayout(fl); 
    Border empty; 
    empty = BorderFactory.createEmptyBorder(); 

    frame.getContentPane().setLayout(fl); 

    Container con = frame.getContentPane(); 
    con.setBackground(Color.LIGHT_GRAY); 
    Font f = new Font("MS UI Gothic", Font.BOLD, 40); 
    Font f2 = new Font("Engravers MT", Font.PLAIN, 40); 
    int bHeight = 80; 
    int bWidth = 70; 
    text.setBackground(Color.GREEN); 
    text.setFont(f2); 
    text.setPreferredSize(new Dimension(bHeight, bWidth)); 
    text.setEditable(false); 


    frame.add(text, BorderLayout.NORTH); 

    oneButton.setPreferredSize(new Dimension(bHeight, bWidth)); 
    oneButton.setBackground(Color.DARK_GRAY); 
    oneButton.setForeground(Color.GREEN); 
    oneButton.setFont(f); 
    oneButton.addActionListener(new Evt()); 
    frame.add(oneButton); 

    twoButton.setPreferredSize(new Dimension(bHeight, bWidth)); 
    twoButton.setBackground(Color.DARK_GRAY); 
    twoButton.setForeground(Color.GREEN); 
    twoButton.setFont(f); 
    frame.add(twoButton); 

    threeButton.setPreferredSize(new Dimension(bHeight, bWidth)); 
    threeButton.setBackground(Color.DARK_GRAY); 
    threeButton.setForeground(Color.GREEN); 
    threeButton.setFont(f); 
    frame.add(threeButton); 

    plusButton.setPreferredSize(new Dimension(bHeight, bWidth)); 
    plusButton.setBackground(Color.DARK_GRAY); 
    plusButton.setForeground(Color.GREEN); 
    plusButton.setFont(f); 
    frame.add(plusButton); 

    fourButton.setPreferredSize(new Dimension(bHeight, bWidth)); 
    fourButton.setBackground(Color.DARK_GRAY); 
    fourButton.setForeground(Color.GREEN); 
    fourButton.setFont(f); 
    frame.add(fourButton); 

    fiveButton.setPreferredSize(new Dimension(bHeight, bWidth)); 
    fiveButton.setBackground(Color.DARK_GRAY); 
    fiveButton.setForeground(Color.GREEN); 
    fiveButton.setFont(f); 
    frame.add(fiveButton); 

    sixButton.setPreferredSize(new Dimension(bHeight, bWidth)); 
    sixButton.setBackground(Color.DARK_GRAY); 
    sixButton.setForeground(Color.GREEN); 
    sixButton.setFont(f); 
    frame.add(sixButton); 

    minusButton.setPreferredSize(new Dimension(bHeight, bWidth)); 
    minusButton.setBackground(Color.DARK_GRAY); 
    minusButton.setForeground(Color.GREEN); 
    minusButton.setFont(f); 
    frame.add(minusButton); 

    sevenButton.setPreferredSize(new Dimension(bHeight, bWidth)); 
    sevenButton.setBackground(Color.DARK_GRAY); 
    sevenButton.setForeground(Color.GREEN); 
    sevenButton.setFont(f); 
    frame.add(sevenButton); 

    eightButton.setPreferredSize(new Dimension(bHeight, bWidth)); 
    eightButton.setBackground(Color.DARK_GRAY); 
    eightButton.setForeground(Color.GREEN); 
    eightButton.setFont(f); 
    frame.add(eightButton); 

    nineButton.setPreferredSize(new Dimension(bHeight,bWidth)); 
    nineButton.setBackground(Color.DARK_GRAY); 
    nineButton.setForeground(Color.GREEN); 
    nineButton.setFont(f); 
    frame.add(nineButton); 

    timesButton.setPreferredSize(new Dimension(bHeight, bWidth)); 
    timesButton.setBackground(Color.DARK_GRAY); 
    timesButton.setForeground(Color.GREEN); 
    timesButton.setFont(f); 
    frame.add(timesButton); 


    zeroButton.setPreferredSize(new Dimension(bHeight, bWidth)); 
    zeroButton.setBackground(Color.DARK_GRAY); 
    zeroButton.setForeground(Color.GREEN); 
    zeroButton.setFont(f); 
    frame.add(zeroButton); 


    enterButton.setPreferredSize(new Dimension(190, bWidth)); 
    enterButton.setBackground(Color.DARK_GRAY); 
    enterButton.setForeground(Color.GREEN); 
    enterButton.setFont(f); 
    frame.add(enterButton); 

    dividButton.setPreferredSize(new Dimension(bHeight, bWidth)); 
    dividButton.setBackground(Color.DARK_GRAY); 
    dividButton.setForeground(Color.GREEN); 
    dividButton.setFont(f); 
    frame.add(dividButton); 

    frame.setComponentOrientation(
      ComponentOrientation.LEFT_TO_RIGHT); 


} 


private static Dimension Dimension(int bHeight, int bWidth) { 
    // TODO Auto-generated method stub 
    return null; 
} 



private static void setLayout(FlowLayout fl) { 
    // TODO Auto-generated method stub 

} 



public static void main(String[] args){ 
new Ken(); 
} 


private class Evt implements ActionListener{ 

    @Override 
    public void actionPerformed(ActionEvent e) { 
    if(e.getSource() == oneButton){ 

    } 

    } 

} 


} 

任何幫助嗎?

回答

0

您已在主類「Ken」上實現了ActionListener接口,因此在這種情況下您不需要Evt類。

無論如何,我都很驚訝,因爲您的主要班級使用implements ActionListener但未實現所需功能時應該出現錯誤。

因此,在您的主類中實施actionPerformed而不是Evt,並且當您將ctionlistener附加到您使用this的按鈕時。

在你的動作監聽你應該做

if(e.getSource().equals(oneButton) 

但無論如何,這不是問題。

而在您的事件處理程序中,您必須更新顯示數字的gui元素。僅將輸入設置爲1是不夠的。所以,你應該做JTextField text也是一個成員變量,那麼你可以更新它的內容與此:

input = 1; 
text.setText(""+input); 

而且你不應該讓所有的元素靜態的。無論如何,他們都被宣佈爲集體成員。

+0

謝謝你,在對actionPerformed使用System.out.println()進行一些調試之後,我得出結論,問題是輸入變量沒有改變。任何想法? –

+0

工作過,謝謝 –

相關問題