2014-02-05 49 views
0
public class Scal { 

    JFrame mainframe; 
    JPanel controlsPanel; 
    JPanel inputPanel; 
    JPanel opPanel; 
    JTextField inputbox; 
    String input1 = ""; 

    public Scal(){ 
     prepareCAL(); 
    } 

    public static void main(String[] args) { 
     Scal cal = new Scal(); 
     cal.showcontrols(); 
    } 

    private void prepareCAL() { 
     mainframe = new JFrame(); 
     mainframe.setSize(400, 400); 
     mainframe.setBackground(Color.GRAY);   

     inputPanel = new JPanel(); 
     controlsPanel = new JPanel(); 
     controlsPanel.setSize(new Dimension(200, 250)); 

     opPanel = new JPanel(); 
     opPanel.setSize(200, 250); 
//  opPanel.setMaximumSize(new Dimension(200, 250)); 

     mainframe.add(inputPanel, BorderLayout.NORTH); 
     mainframe.add(controlsPanel, BorderLayout.CENTER); 
     mainframe.add(opPanel, BorderLayout.EAST); 
     mainframe.setVisible(true); 
    } 

    private void showcontrols() { 
     inputbox = new JTextField(30); 
     inputbox.setText(""); 

     inputPanel.add(inputbox); 

     JButton btn1 = new JButton("1"); 
     btn1.setPreferredSize(new Dimension(50, 50)); 
     btn1.setActionCommand("1"); 

     JButton btn2 = new JButton("2"); 
     btn2.setPreferredSize(new Dimension(50, 50)); 
     btn1.setActionCommand("2"); 

     JButton btn3 = new JButton("3"); 
     btn3.setPreferredSize(new Dimension(50, 50)); 
     btn1.setActionCommand("3"); 

     JButton btn4 = new JButton("4"); 
     btn4.setPreferredSize(new Dimension(50, 50)); 
     btn1.setActionCommand("4"); 

     JButton btn5 = new JButton("5"); 
     btn5.setPreferredSize(new Dimension(50, 50)); 
     btn1.setActionCommand("5"); 

     JButton btn6 = new JButton("6"); 
     btn6.setPreferredSize(new Dimension(50, 50)); 
     btn1.setActionCommand("6"); 

     JButton btn7 = new JButton("7"); 
     btn7.setPreferredSize(new Dimension(50, 50)); 
     btn1.setActionCommand("7"); 

     JButton btn8 = new JButton("8"); 
     btn8.setPreferredSize(new Dimension(50, 50)); 
     btn1.setActionCommand("8"); 

     JButton btn9 = new JButton("9"); 
     btn9.setPreferredSize(new Dimension(50, 50)); 
     btn1.setActionCommand("9"); 

     JButton btnclear = new JButton("C"); 
     btnclear.setPreferredSize(new Dimension(50, 50)); 
     btn1.setActionCommand("c"); 

     JButton btn0 = new JButton("0"); 
     btn0.setPreferredSize(new Dimension(50, 50)); 
     btn1.setActionCommand("0"); 

     JButton btnequal = new JButton("="); 
     btnequal.setPreferredSize(new Dimension(50, 50)); 
     btn1.setActionCommand("="); 

     controlsPanel.add(btn1); 
     controlsPanel.add(btn2); 
     controlsPanel.add(btn3); 
     controlsPanel.add(btn4); 
     controlsPanel.add(btn5); 
     controlsPanel.add(btn6);   
     controlsPanel.add(btn7); 
     controlsPanel.add(btn8); 
     controlsPanel.add(btn9); 
     controlsPanel.add(btnclear); 
     controlsPanel.add(btn0); 
     controlsPanel.add(btnequal); 

     JButton btnPlus = new JButton("+"); 
     btnPlus.setPreferredSize(new Dimension(50, 50)); 
     btn1.setActionCommand("+"); 

     JButton btnSub = new JButton("-"); 
     btnSub.setPreferredSize(new Dimension(50, 50)); 
     btn1.setActionCommand("-"); 

     JButton btnMul = new JButton("*"); 
     btnMul.setPreferredSize(new Dimension(50, 50)); 
     btn1.setActionCommand("*"); 

     JButton btnDiv = new JButton("/"); 
     btnDiv.setPreferredSize(new Dimension(50, 50)); 
     btn1.setActionCommand("/"); 

     btn1.addActionListener(new ClickListener()); 
     btn2.addActionListener(new ClickListener()); 
     btn3.addActionListener(new ClickListener()); 
     btn4.addActionListener(new ClickListener()); 
     btn5.addActionListener(new ClickListener()); 
     btn6.addActionListener(new ClickListener()); 
     btn7.addActionListener(new ClickListener()); 
     btn8.addActionListener(new ClickListener()); 
     btn9.addActionListener(new ClickListener()); 
     btn0.addActionListener(new ClickListener()); 
     btnPlus.addActionListener(new ClickListener()); 
     btnSub.addActionListener(new ClickListener()); 
     btnMul.addActionListener(new ClickListener()); 
     btnDiv.addActionListener(new ClickListener()); 
     btnclear.addActionListener(new ClickListener()); 
     btnequal.addActionListener(new ClickListener()); 


     opPanel.add(btnPlus); 
     opPanel.add(btnSub); 
     opPanel.add(btnMul); 
     opPanel.add(btnDiv); 
     mainframe.setVisible(true);  
    } 

    private class ClickListener implements ActionListener { 

     @Override 
     public void actionPerformed(ActionEvent e) { 

      if (e.getSource() == "1"){ 
       inputbox.setText("1"); 
      } 

     } 
    } 

} 

我已經使用事件來改變文本框中的值。我正在製作計算器。那麼他們的另一種更好的方式來實現它? 以及如何正確使用這個邏輯爲什麼它沒有工作。到目前爲止,我只實現了1個。在文本框中只能顯示1個。 需要幫助當我按1時文本字段應該顯示1,但它沒有。爲什麼?

+0

我也使用{String c = e.getActionCommand(); e.equals(「1」)}但它沒有工作 – Assassino

回答

2

也許是因爲您需要使用invokeLater()來啓動任何Swing應用程序。

e.getSource()將返回發生操作的按鈕的引用。如果你想這麼堅持,那麼它應該是== btn1

此外,制定一個SSCCE狀態的規則,您將您的代碼剝離到所需的最小值。爲什麼當所有你已經實現的監聽器都是一個按鈕時,你有額外的代碼?

8

取而代之的是

if (e.getSource() == "1") 

你想

JButton source = (JButton)e.getSource(); 
if ("1".equals(source.getActionCommand())) 

或者只是

if ("1".equals(e.getActionCommand())) 

另一種選擇是要檢查的JButton對象本身

JButton source = (JButton)e.getSource(); 
if (source == btn1) {} 

或者乾脆

if (e.getSource() == btn1) {} 

所以,你必須選擇


注意

什麼getSource()回報是,打響了ActionEvent,這是JButton對象的對象。 JButton永遠不會== "1"。你需要得到它返回一個字符串的actionCommand,並將其與"1"


UPDATE

你在做什麼是做到這一點一再

btn1.setActionCommand("1"); 
btn1.setActionCommand("2"); 
btn1.setActionCommand("3"); 

你總是設置動作命令爲btn1。你需要相應地改變他們

+0

值得注意的是'getSource()'返回觸發事件的對象。請參閱http://docs.oracle.com/javase/7/docs/api/java/util/EventObject.html#getSource() –

+0

我在我的答案中提到過。 –

+0

使用「1」.equals(source.getActionCommand())可能會更好,因爲actionCommand將默認返回null +1 – MadProgrammer

相關問題