【问题标题】:How to implement FocusListener to JTextArea?如何将 FocusListener 实现到 JTextArea?
【发布时间】:2023-04-08 06:29:01
【问题描述】:

我创建了扩展 JTextArea 的新类,并使用 FocusListener 实现了该类,但 focusLost 方法中的代码没有被执行。是什么原因?

protected class JDynamicTextArea extends JTextArea implements FocusListener { 
    public JDynamicTextArea() { 
        super(); 
        addFocusListener(this); 
    } 
    public void focusGained(FocusEvent e) { } 
    public void focusLost(FocusEvent e) { 
        System.out.println("Focus lost"); 
    } 
}

【问题讨论】:

  • 您是否使用 addFocusListener 将该类注册为焦点侦听器?
  • 是的,我在构造函数中添加了焦点监听器。
  • 我认为您需要提供示例代码
  • 受保护的类 JDynamicTextArea 扩展 JTextArea 实现 FocusListener { public JDynamicTextArea() { super(); addFocusListener(this); } public void focusGained(FocusEvent e) { } public void focusLost(FocusEvent e) { System.out.println("焦点丢失"); } }
  • 我也需要实现keylistener吗?

标签: java swing jtextarea focuslistener


【解决方案1】:

对我来说似乎工作得很好......

单击第二个文本字段,您应该会看到焦点丢失事件触发。

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestFocus {

    public static void main(String[] args) {
        new TestFocus();
    }

    public TestFocus() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException ex) {
                } catch (InstantiationException ex) {
                } catch (IllegalAccessException ex) {
                } catch (UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Test");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            add(new JScrollPane(new JDynamicTextArea()));
            add(new JTextField(10));
        }

    }

    protected class JDynamicTextArea extends JTextArea implements FocusListener {

        public JDynamicTextArea() {
            super(10, 10);
            addFocusListener(this);
        }

        public void focusGained(FocusEvent e) {
            System.out.println("Focus gained");
        }

        public void focusLost(FocusEvent e) {
            System.out.println("Focus lost");
        }

    }

}

更新了关键焦点转移

要重新启用键盘转移焦点,您需要在构造函数中添加以下内容

Set<KeyStroke> strokes = new HashSet<KeyStroke>(Arrays.asList(KeyStroke.getKeyStroke("pressed TAB")));
setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, strokes);
strokes = new HashSet<KeyStroke>(Arrays.asList(KeyStroke.getKeyStroke("shift pressed TAB")));
setFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, strokes);

【讨论】:

  • ehhh ...我告诉新手不是子类,而你老手这样做两次根本没有可行的理由:-(
  • @kleopatra 对不起,Kleo,我不是,我试图证明那里的代码可以与一个正在运行的示例一起工作,以找出他们想要实现的目标。我知道他们想从 OP 中恢复转移焦点键
  • @kleopatra 现在你告诉我过去我不应该使用 setPreferredSize,所以提供合适的大小提示的唯一方法是覆盖 getPreferredSize ...现在,如果我按照你的建议,我不可能在 Swing 中创造任何东西 ;) - 说真的,我理解并支持你的观点
猜你喜欢
  • 2015-08-14
  • 1970-01-01
  • 2013-02-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-12
相关资源
最近更新 更多