2013-06-24 55 views
0

我有一個操作執行10如果和如果else語句和在我的JTextField inly顯示如何讓他們堆棧使數字與多個數字。我希望我的整數堆棧在我的JTextField

我不需要任何人閱讀所有的代碼。我只想從actionPerformed的整數堆棧。

我正在製作一個基本的gui計算器。

@Override 
public void actionPerformed(ActionEvent e) { 
    if(e.getSource().equals(oneButton)){ 
     input1 = 1; 
     text.setText(""+input);} 
    else if (e.getSource().equals(twoButton)){ 
     input2 = 2; 
     text.setText(""+input);} 
    else if(e.getSource().equals(threeButton)){ 
     input3 = 3; 
     text.setText(""+input);} 
    else if(e.getSource().equals(fourButton)){ 
     input4 = 4; 
     text.setText(""+input);} 
    else if(e.getSource().equals(fiveButton)){ 
     input5 = 5; 
     text.setText(""+input);} 
    else if(e.getSource().equals(sixButton)){ 
     input6 = 6; 
     text.setText(""+input);} 
    else if(e.getSource().equals(sevenButton)){ 
     input7 = 7; 
     text.setText(""+input);} 
    else if(e.getSource().equals(eightButton)){ 
     input8 = 8; 
     text.setText(""+input);} 
    else if(e.getSource().equals(nineButton)){ 
     input9 = 9; 
     text.setText(""+input);} 
    else if(e.getSource().equals(zeroButton)){ 
     input0 = 0; 
     text.setText(""+input);} 

} 

休息我的代碼

//imports 
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; 
public static int input1; 
public static int input2; 
public static int input3; 
public static int input4; 
public static int input5; 
public static int input6; 
public static int input7; 
public static int input8; 
public static int input9; 
public static int input0; 
public static JTextField text = new JTextField("0", 9); 
public Ken(){ 

    JFrame frame = new JFrame("MyCalc"); 





    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(this); 
    frame.add(oneButton); 

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

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

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

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

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

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

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

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

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

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

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


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


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

    dividButton.setPreferredSize(new Dimension(bHeight, bWidth)); 
    dividButton.setBackground(Color.DARK_GRAY); 
    dividButton.setForeground(Color.GREEN); 
    dividButton.setFont(f); 
    dividButton.addActionListener(this); 
    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(); 

} 




@Override 
public void actionPerformed(ActionEvent e) { 
    if(e.getSource().equals(oneButton)){ 
     input1 = 1; 
     text.setText(""+input);} 
    else if (e.getSource().equals(twoButton)){ 
     input2 = 2; 
     text.setText(""+input);} 
    else if(e.getSource().equals(threeButton)){ 
     input3 = 3; 
     text.setText(""+input);} 
    else if(e.getSource().equals(fourButton)){ 
     input4 = 4; 
     text.setText(""+input);} 
    else if(e.getSource().equals(fiveButton)){ 
     input5 = 5; 
     text.setText(""+input);} 
    else if(e.getSource().equals(sixButton)){ 
     input6 = 6; 
     text.setText(""+input);} 
    else if(e.getSource().equals(sevenButton)){ 
     input7 = 7; 
     text.setText(""+input);} 
    else if(e.getSource().equals(eightButton)){ 
     input8 = 8; 
     text.setText(""+input);} 
    else if(e.getSource().equals(nineButton)){ 
     input9 = 9; 
     text.setText(""+input);} 
    else if(e.getSource().equals(zeroButton)){ 
     input0 = 0; 
     text.setText(""+input);} 

} 


} 
+0

我正要告訴你如何可以用兩行代碼替換20行代碼在你的ActionListener,但因爲你仍然有這一切代碼th在使用'setPreferredSize()'我看你沒有聽過去的建議,所以這次我不打算提出建議。另外,你所有的變量都是靜態的,我知道其他人建議你不要這樣做。那麼,爲什麼當你不聽所給的建議時,你甚至懶得提問? – camickr

回答

2

使用

text.setText(text.getText()+input); 

,而不是

text.setText(""+input); 

因爲你重寫所有的存在於字符文本域。

0

我不知道你的意思,但覺得在這個方向:用你自己的實現按鈕的延長JButton將知道其索引:

private class MyButton extends JButton { 

    private String index; 

    public MyButton(String index) { 
     this.index = index; 
    } 

    public String getIndex() { 
     return index; 
    } 
} 

之後,你可以做到這一點的你actionPerfmormed(),也不需要堆放上千IFS

public void actionPerformed(ActionEvent e) { 
    if (e.getSource() instanceof MyButton) { 
     text.setText(((MyButton)e.getSource()).getIndex()); 
    } 
}