Java Swing问题中的关键压力

时间:2011-09-28 23:20:34

标签: java swing

我使用Java Swing制作了一个计算器,它主要完成但是我想知道如何让它工作,这样当你按下键盘上的0 - 9键时,它将执行与手动按下相同的功能计算器上的JButton。我尝试使用but1.setMnemonic(KeyEvent.VK_1);但是当我按下1键时,它没有做任何事情。我怎样才能这样做,以便在按键盘键时正确激活所述按钮?这是我正在使用的代码,感谢您的帮助!

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;
}

}

1 个答案:

答案 0 :(得分:5)

当使用修饰符(通常为Alt或Ctrl)时,助记符将按下按钮,因此按Alt + 1(或Ctrl + 1)将起作用。

要使按钮与键盘对应,您应该查看InputMapActionMap

有关详细信息,请参阅http://download.oracle.com/javase/tutorial/uiswing/misc/keybinding.html