繁体   English   中英

清除当前的FocusOwner(jTextfield)

[英]Clear current FocusOwner (jTextfield)


快速开发应用程序,只需查询一下:

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

谢谢!!!

您可能可以使用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 setText()方法。

如果要通过单击按钮清除textfield字段,则必须编写代码以清除ActionListener类的ActionPerformed方法中的textfield 按下按钮时将调用此方法。 但是,为了按下按钮,您必须将焦点从其他组件移到该按钮。 因此,在ActionPerformed方法中,您将对textField.isFocusOwner()感到false

我对克服此问题的建议是:

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

  2. 在要实现所有这些的Class lastFocused变量lastFocused声明为JTextField类型,将其初始化为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(""); } } 

我认为这应该可以解决您的问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM