使用 JButtons 将文本添加到两个文本字段
如果我的问题不是很具体,这就是我想要做的。我有一个计算器,它有两个 JTextField、一个 JLabel(“Answer =”)和一个用于答案的 JTextField。
我有一个 JButton 数组(0 到 9),允许用户单击它们将数字添加到其中光标处于活动状态的 JTextField...这就是这里的问题。我只能让两个文本字段之一向其添加数字,或者两者都向彼此添加相同的数字。
例如,如果我单击一个按钮并将 addActionListener
设置为 (new AddDigitsONE)
,它将只允许我向第一个 JTextField 添加数字。即使我尝试将光标设置到第二个 JTextField 并使用 JButtons 添加数字,它也会跳回第一个 JTextField。
将 JButton 数组添加到 JFrame 中的 JPanel 的代码
// input is my JPanel set to BorderLayout.SOUTH
for (int i = 0; i < button.length; i++)
{
text = Integer.toString(i);
button[i] = new JButton();
button[i].setText(text);
input.add(button[i]);
button[i].addActionListener(new AddDigitsONE());
}
我的操作侦听器的代码:第一个 JTextField
// firstNumber is my first JTextField
// command is the button pressed (0-9)
// command "<" is erasing one character at a time
private class AddDigitsONE implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String text = firstNumber.getText();
String command = ((JButton)(e.getSource())).getText();
if (command == "<")
{
firstNumber.setText(text.substring(0,text.length()-1));
}
else
firstNumber.setText(text.concat(command));
}
}
我的操作侦听器的代码:第二个 JTextField
private class AddDigitsTWO implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String text = secondNumber.getText();
String command = ((JButton)(e.getSource())).getText();
if (command == "<")
{
secondNumber.setText(text.substring(0,text.length()-1));
}
else
secondNumber.setText(text.concat(command));
}
}
有没有办法合并两个动作侦听器并区分哪个文本字段中的光标处于活动状态(同时允许我使用 JButton 在两个 JTextField 中输入数字)?
If my question wasn't very specific, here is what I am trying to do. I have a calculator that has two JTextFields, a JLabel ("Answer = "), and a JTextField for the answer.
I have an array of JButtons (0 through 9) that allow the user to click on them to add the number to the JTextField with the cursor active in it... which is the problem here. I can only have one of the two textfields add numbers to them or both add the same numbers to each other.
For example, if I click on a button and the addActionListener
is set to (new AddDigitsONE)
it will only allow me to add numbers to the first JTextField. It will jump back to the first JTextField even after I try to set the cursor to the second JTextField and add numbers to it using the JButtons.
Code for adding the array of JButtons to the JPanel in the JFrame
// input is my JPanel set to BorderLayout.SOUTH
for (int i = 0; i < button.length; i++)
{
text = Integer.toString(i);
button[i] = new JButton();
button[i].setText(text);
input.add(button[i]);
button[i].addActionListener(new AddDigitsONE());
}
Code for my action listener: First JTextField
// firstNumber is my first JTextField
// command is the button pressed (0-9)
// command "<" is erasing one character at a time
private class AddDigitsONE implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String text = firstNumber.getText();
String command = ((JButton)(e.getSource())).getText();
if (command == "<")
{
firstNumber.setText(text.substring(0,text.length()-1));
}
else
firstNumber.setText(text.concat(command));
}
}
Code for my action listener: Second JTextField
private class AddDigitsTWO implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String text = secondNumber.getText();
String command = ((JButton)(e.getSource())).getText();
if (command == "<")
{
secondNumber.setText(text.substring(0,text.length()-1));
}
else
secondNumber.setText(text.concat(command));
}
}
Is there a way to merge both action listeners and differentiate between which text field has the cursor active in it (while allowing me to enter numbers in both JTextFields using the JButtons)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以向按钮添加操作,而不是使用 ActionListener。在这种情况下,您将需要扩展 TextAction,因为它有一个方法允许您获取最后一个焦点文本组件,以便您可以将数字插入该组件。代码类似于:
Instead of using an ActionListener you can add an Action to the button. In this case you will want to extend TextAction because it has a method that allows you to get the last focused text component so you can insert the digit into that component. The code would be something like: