使用JButton将文本添加到两个文本字段 [英] Add text to two textfields with JButtons

查看:87
本文介绍了使用JButton将文本添加到两个文本字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我的问题不是很具体,这就是我要尝试做的.我有一个包含两个JTextField,一个JLabel("Answer =")和一个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.

我有一个JButtons数组(0到9),允许用户单击它们以将数字添加到JTextField中,并使光标处于活动状态...这是问题所在.我只能让两个文本字段之一向它们添加数字,或者都向彼此添加相同的数字.

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.

例如,如果我单击一个按钮并且addActionListener设置为(new AddDigitsONE),它将只允许我在第一个JTextField中添加数字.即使我尝试将光标设置为第二个JTextField并使用JButton向其添加数字,它也会跳回到第一个JTextField.

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.

用于将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中输入数字)?


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)?

推荐答案

除了可以使用ActionListener之外,还可以将Action添加到按钮.在这种情况下,您将要扩展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:

class AddDigit extends TextAction
{
    private String digit;

    public AddDigit(String digit)
    {
        super( digit );
        this.digit = digit;
    }

    public void actionPerformed(ActionEvent e)
    {
        JTextComponent component = getFocusedComponent();
        component.replaceSelection( digit );
    }
}

这篇关于使用JButton将文本添加到两个文本字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆