清除当前 FocusOwner (jTextfield)

Clear current FocusOwner (jTextfield)


在 swing 中开发应用程序,只需一点查询:-

我想使用按钮清除当前的焦点所有者文本字段。可以使用 isFocusOwner() 确定文本字段是否是当前焦点所有者,但 如何清除当前处于焦点的文本字段

谢谢!!!

如果要通过单击按钮清除 textfield,则必须编写代码来清除 ActionListener class 中的 textfieldActionPerformed 方法。按下按钮时调用此方法。但是为了按下按钮,您必须将焦点从其他组件转移到此按钮。所以在方法 ActionPerformed 中你会得到 falsetextField.isFocusOwner().

我克服这个问题的建议是:

  1. 将焦点侦听器添加到这 6 个文本字段。

  2. 声明一个变量说 lastFocused 作为类型 JTextField 在你正在实现所有这些的 Class 中将它初始化为 null

  3. 将以下代码写入focusListerners重写方法

    void focusGained(FocusEvent e){
        lastFocused = (JTextField) e.getComponent();
    }
    
    void focusLost(FocusEvent e){
        lastFocused = null;
    }
    
  4. 现在在 ActionListener 重写的方法中写入以下内容:

    void actionPerformed(ActionEvent e){
        if(lastFocused != null){
            lastFocused.setText("");
        }
    } 
    

我觉得这应该可以解决您的问题。

您或许可以使用 TextAction。 TextAction 可以访问具有焦点的最后一个文本组件。因此,在文本操作中,您只需清除组件中的文本。所有逻辑都完全包含在一个地方。

这是一个演示使用 TextAction 的概念的示例。在这种情况下,按钮表示的数字附加到具有焦点的文本字段:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.text.*;

public class NumpadPanel extends JPanel
{
    public NumpadPanel()
    {
        setLayout( new BorderLayout() );

        JTextField textField1 = new JTextField(4);
        JTextField textField2 = new JTextField(2);
        JTextField textField3 = new JTextField(2);

        JPanel panel = new JPanel();
        panel.add( textField1 );
        panel.add( textField2 );
        panel.add( textField3 );
        add(panel, BorderLayout.PAGE_START);

        Action numberAction = new TextAction("")
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                JTextComponent textComponent = getFocusedComponent();

                if (textComponent != null)
                    textComponent.replaceSelection(e.getActionCommand());
            }
        };

        JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout( new GridLayout(0, 5) );
        add(buttonPanel, BorderLayout.CENTER);

        for (int i = 0; i < 10; i++)
        {
            String text = String.valueOf(i);
            JButton button = new JButton( text );
            button.addActionListener( numberAction );
            button.setMargin( new Insets(20, 20, 20, 20) );
            button.setFocusable( false );
            buttonPanel.add( button );
        }
    }

    private static void createAndShowUI()
    {
        JFrame frame = new JFrame("Numpad Panel");
        frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        frame.add( new NumpadPanel() );
        frame.pack();
        frame.setLocationRelativeTo( null );
        frame.setVisible(true);
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowUI();
            }
        });
    }
}

在您的情况下,您可以使用 setText() 方法而不是使用 replaceSelection() 方法。