Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将FocusListener实现到JTextArea?_Java_Swing_Jtextarea_Focuslistener - Fatal编程技术网

Java 如何将FocusListener实现到JTextArea?

Java 如何将FocusListener实现到JTextArea?,java,swing,jtextarea,focuslistener,Java,Swing,Jtextarea,Focuslistener,我创建了一个扩展JTextArea的新类,并用FocusListener实现了该类,但是focusLost方法中的代码没有得到执行。原因是什么 protected class JDynamicTextArea extends JTextArea implements FocusListener { public JDynamicTextArea() { super(); addFocusListener(this); } publ

我创建了一个扩展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"); 
    } 
}

似乎对我来说很好

单击第二个文本字段,您将看到focus lost event fire

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);
Set strokes=newhashset(Arrays.asList(KeyStroke.getKeyStroke(“按下的选项卡”));
setFocusTraversalKeys(键盘FocusManager.FORWARD\u TRAVERSAL\u按键、笔划);
strokes=新的HashSet(Arrays.asList(KeyStroke.getKeyStroke(“按下shift键的选项卡”));
setFocusTraversalKeys(键盘FocusManager.BACKWARD\u TRAVERSAL\u键、笔划);

您是否已使用addFocusListener将该类注册为焦点侦听器?是的,我已在构造函数中添加了焦点侦听器。我认为您需要提供示例代码,然后受保护的类JDynamicTextArea扩展JTextArea实现了焦点侦听器{public JDynamicTextArea(){super();addFocusListener(this);}public void focusgain(FocusEvent e){}public void focusLost(FocusEvent e){System.out.println(“Focus lost”);}}我是否也需要实现keylistener?呃。。。我告诉新手不要子类化,而你这个老手却做了两次,根本没有任何可行的理由:-(@kleopatra抱歉Kleo,我不是,我只是想通过一个运行中的示例来演示这些代码的工作原理,以找出他们想要实现的目标。我得到了一个事实,即他们想要将转移焦点键从OP@kleopatra现在你告诉我过去我不应该使用setPreferredSize,所以o提供合适的大小提示是覆盖getPreferredSize…现在,如果我按照你的建议,我永远不可能在Swing中创建任何东西;)-说真的,我理解并支持你的观点