Reputation: 23
I have made a calculator using Java Swing, and it's mostly done but I was wondering how you could make it work so that when you press a key from 0 - 9 on the keyboard, it will perform the same function as manually pressing the JButton on the calculator. I tried using but1.setMnemonic(KeyEvent.VK_1); but I it didn't do anything when I pressed the 1 key. How can I make it so it will correctly activate said button when I press a keyboard key? Here is the code I am using, thanks for any help!
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;
}
}
Upvotes: 2
Views: 896
Reputation: 1029
The mnemonic will make the button press when used with a modifier (usually Alt or Ctrl), so pressing Alt+1 (or Ctrl+1) will work.
To make the buttons correspond to the Keypad, you should have a look at InputMap
and ActionMap
.
See http://download.oracle.com/javase/tutorial/uiswing/misc/keybinding.html for more details.
Upvotes: 5