如何在java上制作一个简单的键盘来有效地在JTextFields中输入?
我正在开发一个java键盘,它将用于在两个JTextField(x和y)中输入数据。一旦用户单击“Enter”,它应该评估 x 和 y 的差异。我是否必须为每个按钮添加一堆actionListener?有没有一种有效的方法来做到这一点。
到目前为止,在我的代码中,我拥有所需的所有按钮,但我不想使用键盘,而是想使用虚拟键盘来输入数字(从技术上讲,是一个字符串)。有什么建议吗?
import javax.swing.*;
import java.awt.*;
public class EmbeddedMain extends JFrame
{
public static void main (String[] args)
{
EmbeddedMain em = new EmbeddedMain();
}
public EmbeddedMain()
{
setTitle("testing");
setSize(450,350);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setLayout(new GridLayout(4,5,3,3));
JButton button1= new JButton("7");
JButton button2= new JButton("8");
JButton button3= new JButton("9");
JButton button4= new JButton("4");
JButton button5= new JButton("5");
JButton button6= new JButton("6");
JButton button7= new JButton("1");
JButton button8= new JButton("2");
JButton button9= new JButton("3");
JButton button0= new JButton("0");
JButton buttonR= new JButton("Reset");
JButton buttonE= new JButton("Enter");
JTextField x = new JTextField(" ");
JTextField y = new JTextField(" ");
JTextField CPP_entry = new JTextField(" ");
CPP_entry.setEditable(false);
add(button1);
add(button2);
add(button3);
add(new JLabel(" x:"));
add(x);
add(button4);
add(button5);
add(button6);
add(new JLabel(" y:"));
add(y);
add(button7);
add(button8);
add(button9);
add(new JLabel(" x-y:"));
add(CPP_entry);
add(buttonR);
add(button0);
add(buttonE);
setVisible(true);
}
}
I am working on a keypad on java that will be used to input data in two JTextField (x & y). And Once the user clicks "Enter", It should evaluate the difference of x and y. Do I have to add bunch of actionListeners for each button?? Is there an efficient way to do it.
So far on my code, I have all the buttons I need, but instead of using my keyboard, I want to use the virtual keypad instead to enter the numbers (technically, a String). Any suggestions?
import javax.swing.*;
import java.awt.*;
public class EmbeddedMain extends JFrame
{
public static void main (String[] args)
{
EmbeddedMain em = new EmbeddedMain();
}
public EmbeddedMain()
{
setTitle("testing");
setSize(450,350);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setLayout(new GridLayout(4,5,3,3));
JButton button1= new JButton("7");
JButton button2= new JButton("8");
JButton button3= new JButton("9");
JButton button4= new JButton("4");
JButton button5= new JButton("5");
JButton button6= new JButton("6");
JButton button7= new JButton("1");
JButton button8= new JButton("2");
JButton button9= new JButton("3");
JButton button0= new JButton("0");
JButton buttonR= new JButton("Reset");
JButton buttonE= new JButton("Enter");
JTextField x = new JTextField(" ");
JTextField y = new JTextField(" ");
JTextField CPP_entry = new JTextField(" ");
CPP_entry.setEditable(false);
add(button1);
add(button2);
add(button3);
add(new JLabel(" x:"));
add(x);
add(button4);
add(button5);
add(button6);
add(new JLabel(" y:"));
add(y);
add(button7);
add(button8);
add(button9);
add(new JLabel(" x-y:"));
add(CPP_entry);
add(buttonR);
add(button0);
add(buttonE);
setVisible(true);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我会制作一个像这样的按钮数组:
然后当事件触发时检查其来源。
I'd make an array of buttons like this:
and then when the event fires check for its origin.
有两件事:
首先,您可以只使用一个
ActionListener
,只需注意将其添加到所有按钮,然后您可以执行以下操作:其次,您可以使用
KeyListener
来捕获当键盘聚焦时按下的按键,该侦听器具有三种方法:keyPressed(KeyEvent e)
、keyReleased(KeyEvent e)
和keyTyped(KeyEvent e)< /代码>。请查看此处的 Oracle 教程,了解如何使用它们。
Two things:
first you can use just one
ActionListener
, just take care to add it to all buttons and then you can do something like:secondly you can use a
KeyListener
to catch the keystrokes pressed while your keypad is focused, that listener has three methods:keyPressed(KeyEvent e)
,keyReleased(KeyEvent e)
andkeyTyped(KeyEvent e)
. Take a look at oracle tutorial here to learn how to use them.