2013-03-22 96 views
0

我有一个相当简单的计算器,我试图绑定键到JButtons。我对Java很陌生,而且我不太了解。我知道它涉及到ActionListener,但是我无法将自己的头部包裹到如何使其进入我目前的状态。绑定键到JButtons

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

@SuppressWarnings("serial") 
public class Calculator2 extends JFrame implements ActionListener { 

    // Declare the GUI objects and variables HERE 
    JTextField ansText; 
    JButton b1, b2, b3, b4, b5, b6, b7, b8, b9, b0, plus, minus, multi, div, 
      equal, clear; 
    JPanel p1, p2, p3; 
    Double val1 = 0.0, val2 = 0.0, answer = 0.0; 
    int operator = 0; 

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

    public Calculator2() { 

     // GUI Creation Code goes HERE 

     ansText = new JTextField("", 7); 
     ansText.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT); 


     ansText.addKeyListener(new KeyAdapter() { //Allow only numbers in ansText 
      public void keyTyped(KeyEvent e) { 
       char c = e.getKeyChar(); 
       if (((c < '0') || (c > '9')) && (c != KeyEvent.VK_BACK_SPACE)) { 
        e.consume(); 
       } 
      } 
     }); 

     b1 = new JButton("1"); 
     b1.addActionListener(this); 
     b2 = new JButton("2"); 
     b2.addActionListener(this); 
     b3 = new JButton("3"); 
     b3.addActionListener(this); 
     b4 = new JButton("4"); 
     b4.addActionListener(this); 
     b5 = new JButton("5"); 
     b5.addActionListener(this); 
     b6 = new JButton("6"); 
     b6.addActionListener(this); 
     b7 = new JButton("7"); 
     b7.addActionListener(this); 
     b8 = new JButton("8"); 
     b8.addActionListener(this); 
     b9 = new JButton("9"); 
     b9.addActionListener(this); 
     b0 = new JButton("0"); 
     b0.addActionListener(this); 
     plus = new JButton("+"); 
     plus.addActionListener(this); 
     minus = new JButton("-"); 
     minus.addActionListener(this); 
     multi = new JButton("*"); 
     multi.addActionListener(this); 
     div = new JButton("/"); 
     div.addActionListener(this); 
     equal = new JButton("="); 
     equal.addActionListener(this); 
     clear = new JButton("C"); 
     clear.addActionListener(this); 

     this.setLayout(new BorderLayout()); 

     p1 = new JPanel(); 
     this.add(p1, BorderLayout.NORTH); 
     p1.setLayout(new GridLayout(0, 1, 2, 2)); 
     p1.add(ansText); 

     p2 = new JPanel(); 
     this.add(p2, BorderLayout.CENTER); 
     p2.setLayout(new GridLayout(4, 3, 2, 2)); 
     p2.add(b1); 
     p2.add(b2); 
     p2.add(b3); 
     p2.add(plus); 

     p2.add(b4); 
     p2.add(b5); 
     p2.add(b6); 
     p2.add(minus); 

     p2.add(b7); 
     p2.add(b8); 
     p2.add(b9); 
     p2.add(multi); 

     p2.add(clear); 
     p2.add(b0); 
     p2.add(equal); 
     p2.add(div); 

     this.setSize(200, 250); 
     this.setVisible(true); 
    } 

    public void actionPerformed(ActionEvent e) { 

     //Number input 
     if (e.getSource() == b1) { 
      ansText.setText(ansText.getText() + b1.getText()); 
     } 
     else if (e.getSource() == b2) { 
      ansText.setText(ansText.getText() + b2.getText()); 
     } 
     else if (e.getSource() == b3) { 
      ansText.setText(ansText.getText() + b3.getText()); 
     } 
     else if (e.getSource() == b4) { 
      ansText.setText(ansText.getText() + b4.getText()); 
     } 
     else if (e.getSource() == b5) { 
      ansText.setText(ansText.getText() + b5.getText()); 
     } 
     else if (e.getSource() == b6) { 
      ansText.setText(ansText.getText() + b6.getText()); 
     } 
     else if (e.getSource() == b7) { 
      ansText.setText(ansText.getText() + b7.getText()); 
     } 
     else if (e.getSource() == b8) { 
      ansText.setText(ansText.getText() + b8.getText()); 
     } 
     else if (e.getSource() == b9) { 
      ansText.setText(ansText.getText() + b9.getText()); 
     } 
     else if (e.getSource() == b0) { 
      ansText.setText(ansText.getText() + b0.getText()); 
     } 

     //Operator input 
     else if (e.getSource() == plus) { 
      operator = 1; 
      val1 = Double.parseDouble(ansText.getText()); 
      ansText.setText(""); 
     } 
     else if (e.getSource() == minus) { 
      operator = 2; 
      val1 = Double.parseDouble(ansText.getText()); 
      ansText.setText(""); 
     } 
     else if (e.getSource() == multi) { 
      operator = 3; 
      val1 = Double.parseDouble(ansText.getText()); 
      ansText.setText(""); 
     } 
     else if (e.getSource() == div) { 
      operator = 4; 
      val1 = Double.parseDouble(ansText.getText()); 
      ansText.setText(""); 
     } 
     //Misc 
     else if (e.getSource() == equal) { 
      val2 = Double.parseDouble(ansText.getText()); 
      if (operator == 1) { 
       answer = val1 + val2; 
       ansText.setText("" + answer); 
      } else if (operator == 2) { 
       answer = val1 - val2; 
       ansText.setText("" + answer); 
      } else if (operator == 3) { 
       answer = val1 * val2; 
       ansText.setText("" + answer); 
      } else if (operator == 4) { 
       answer = val1/val2; 
       ansText.setText("" + answer); 
      } 
     } 
     else if (e.getSource() == clear) { 
      ansText.setText(""); 
      val1 = 0.0; 
      val2 = 0.0; 
      answer = 0.0; 
      operator = 0; 
     } 
    } 
} 
+0

您看到了什么错误?问题是什么? – theJollySin 2013-03-22 02:30:31

+0

如果您试图将按键绑定到按钮,即按键盘上的“+”与单击GUI上的“+”相同,请查看http://docs.oracle.com/javase/tutorial/uiswing/misc/ keybinding.html – jedyobidan 2013-03-22 02:33:54

回答

0

你不希望有一个只有几个内部类的类来做所有的工作。与大多数其他GUI系统一样,Java Swing基于MVC Model-View-Controller。

的此处模型将是一些对象,其中包含被存储在显示累加器和值的当前值。如果您正在编写RPN计算器,则模型对象将包含RPN堆栈。

观将是Swing类:在JTextFieldJButtons

的控制器会出现一些新的对象,说CalculatorController,完成实际的工作。而不是你的ActionListener加,减,乘或除数,他们会调用CalculatorController上的方法,而这个对象反过来会更新JTextField。

通过jedyobidan的建议,看看Oracle文档都非常好。您可能希望将键击输入到JPanel本身的输入映射中,因为它们应该适用于用户所关注的面板中的任何位置。动作地图然后将保存AbstractActions,调用CalculatorController上的方法。

我一直认为,在Mac或它的前身NeXT公司,与他们的界面生成器程序上的GUI开发的风格,是很多新手比的Java Swing使用的样式更好。

2

How to Use Key Bindings教程介绍键绑定的细节。下面是一个简单的例子,说明如何将操作绑定到键盘和主键盘上的加号键上:

JPanel panel = new JPanel(); 
JPanel panel = new JPanel(); 
    panel.getInputMap(JPanel.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(
      KeyStroke.getKeyStroke(KeyEvent.VK_ADD, 0), "plus"); 
panel.getInputMap(JPanel.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(
     KeyStroke.getKeyStroke(KeyEvent.VK_EQUALS, 
       InputEvent.SHIFT_MASK), "plus"); 

panel.getActionMap().put("plus", plusAction); 
panel.add(button); 
+2

您忘记提及抽象焦点Xxx.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT/*。WHEN_FOCUSED * /) .put(KeyStroke.getKeyStroke(“0”),“zero”); – mKorbel 2013-03-22 06:38:08

+0

@mKorbel错过了,谢谢! – tenorsax 2013-03-22 14:08:22