2011-09-28 66 views
2

我已經使用Java Swing製作了一個計算器,它大部分完成了,但我想知道如何使它工作,以便當您按鍵盤上的0-9鍵時,它將執行與手動按下計算器上的JButton相同的功能。我嘗試使用but1.setMnemonic(KeyEvent.VK_1);但是當我按下1鍵時,它沒有做任何事情。我怎樣才能做到這一點,所以當我按下鍵盤按鍵時,它會正確地激活所述按鈕?這裏是我使用的代碼,感謝任何幫助!Java Swing中的按鍵問題

import java.awt.Color; 
import java.awt.FlowLayout; 
import javax.swing.*; 
import java.awt.event.ActionListener; 
import java.awt.event.ActionEvent; 
import javax.swing.Action; // For key presses 
import java.awt.event.KeyEvent; // key presses 
import java.awt.event.KeyListener; 

public class ui_calculator implements ActionListener { 

JFrame frame = new JFrame("Calculator"); // Creates the frame 
JPanel panel = new JPanel(new FlowLayout()); // Add FlowLayout with center alignment 

// Make new buttons, text areas 
JTextArea text = new JTextArea(1, 20); 
JButton but1 = new JButton("1"); 
JButton but2 = new JButton("2"); 
JButton but3 = new JButton("3"); 
JButton but4 = new JButton("4"); 
JButton but5 = new JButton("5"); 
JButton but6 = new JButton("6"); 
JButton but7 = new JButton("7"); 
JButton but8 = new JButton("8"); 
JButton but9 = new JButton("9"); 
JButton but0 = new JButton("0"); 

JButton butadd = new JButton("+"); 
JButton butsub = new JButton("-"); 
JButton butmul = new JButton("*"); 
JButton butdiv = new JButton("/"); 
JButton buteq = new JButton("="); 
JButton butclear = new JButton("C"); 

Double number1, number2, result; // Numbers double as precise as a float 
int addc = 0, subc = 0, mulc = 0, divc = 0; 


public void ui() 
{ 

    frame.setVisible(true); // Make frame visible 
    frame.setSize(250, 200); // Calculator Size 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // What happens when frame closes (exits program when user closes frame. 

    frame.add(panel); 

    panel.add(text); 

    // Adding number buttons 
    panel.add(but1); 
    panel.add(but2); 
    panel.add(but3); 
    panel.add(but4); 
    panel.add(but5); 
    panel.add(but6); 
    panel.add(but7); 
    panel.add(but8); 
    panel.add(but9); 
    panel.add(but0); 

    // Adding action buttons 
    panel.add(butadd); 
    panel.add(butsub); 
    panel.add(butmul); 
    panel.add(butdiv); 
    panel.add(buteq); 
    panel.add(butclear); 

but1.addActionListener(this); 
but2.addActionListener(this); 
but3.addActionListener(this); 
but4.addActionListener(this); 
but5.addActionListener(this); 
but6.addActionListener(this); 
but7.addActionListener(this); 
but8.addActionListener(this); 
but9.addActionListener(this); 
but0.addActionListener(this); 
butadd.addActionListener(this); 
butsub.addActionListener(this); 
butmul.addActionListener(this); 
butdiv.addActionListener(this); 
buteq.addActionListener(this); 
butclear.addActionListener(this); 

//but1.setMnemonic(KeyEvent.VK_1); 

// Colored buttons (red) 
butadd.setForeground(Color.red); 
butsub.setForeground(Color.red); 
butmul.setForeground(Color.red); 
butdiv.setForeground(Color.red); 
buteq.setForeground(Color.red); 
butclear.setForeground(Color.red); 

// Colored buttons (blue) 
but1.setForeground(Color.blue); 
but2.setForeground(Color.blue); 
but3.setForeground(Color.blue); 
but4.setForeground(Color.blue); 
but5.setForeground(Color.blue); 
but6.setForeground(Color.blue); 
but7.setForeground(Color.blue); 
but8.setForeground(Color.blue); 
but9.setForeground(Color.blue); 
but0.setForeground(Color.blue); 

} 


@Override 
public void actionPerformed(ActionEvent e) { // Invokes when certain action occurs 

    Object source = e.getSource(); // Object on which event occured 

    if(source == butclear) // Reset all text 
    { 
     number1 = 0.0; 
     number2 = 0.0; 
     text.setText(""); 
    } 

    // Specific buttons trigger text 
    if(source == but1) 
    { 
     text.append("1"); 
    } 

/* if(KeyEvent.VK_1 = true) 
    { 
     text.append("1"); 
    } */ 

    if(source == but2) 
    { 
     text.append("2"); 
    } 

    if(source == but3) 
    { 
     text.append("3"); 
    } 

    if(source == but4) 
    { 
     text.append("4"); 
    } 

    if(source == but5) 
    { 
     text.append("5"); 
    } 

    if(source == but6) 
    { 
     text.append("6"); 
    } 

    if(source == but7) 
    { 
     text.append("7"); 
    } 

    if(source == but8) 
    { 
     text.append("8"); 
    } 

    if(source == but9) 
    { 
     text.append("9"); 
    } 

    if(source == but0) 
    { 
     text.append("0"); 
    } 

    if(source == butadd) // Addition actions 
    { 
     number1 = number_reader(); 
     text.setText(""); 
     addc = 1; 
     subc = 0; 
     mulc = 0; 
     divc = 0; 
    } 

    if(source == butsub) // Subtraction actions 
    { 
     number1 = number_reader(); 
     text.setText(""); 
     addc = 0; 
     subc = 1; 
     mulc = 0; 
     divc = 0; 
    } 

    if(source == butmul) // Multiplication actions 
    { 
     number1 = number_reader(); 
     text.setText(""); 
     addc = 0; 
     subc = 0; 
     mulc = 1; 
     divc = 0; 
    } 

    if(source == butdiv) // Division actions 
    { 
     number1 = number_reader(); 
     text.setText(""); 
     addc = 0; 
     subc = 0; 
     mulc = 0; 
     divc = 1; 
    } 

    if(source == buteq) // Equals actions 
    { 
     number2 = number_reader(); 
     if(addc > 0) 
     { 
      result = number1 + number2; 
      text.setText(Double.toString(result)); 
     } 

     if(subc > 0) 
     { 
      result = number1 - number2; 
      text.setText(Double.toString(result)); 
     } 

     if(mulc > 0) 
     { 
      result = number1 * number2; 
      text.setText(Double.toString(result)); 
     } 

     if(divc > 0) 
     { 
      result = number1/number2; 
      text.setText(Double.toString(result)); 
     } 

    } 


} 

public double number_reader() 
{ 
    Double num1; 
    String s; 
    s = text.getText(); 
    num1 = Double.valueOf(s); 

    return num1; 
} 

}

回答

5

助記符將使按鈕按壓與改性劑(通常是Alt鍵或Ctrl)一起使用時,這樣按下Alt + 1(或Ctrl + 1)將起作用。

要使按鈕對應於鍵盤,您應該看看InputMapActionMap

查看http://download.oracle.com/javase/tutorial/uiswing/misc/keybinding.html瞭解更多詳情。

+1

+1鍵綁定;另請參閱此[示例](http://sites.google.com/site/drjohnbmatthews/keypad-panel)。 – trashgod

+1

對於Key Binding建議+1是更好的解決方案。 – camickr