当我按下JFrame时,为什么焦点永远不会从我的组件中丢失?

时间:2013-06-01 08:53:08

标签: java swing focus jframe focuslistener

我对CustomTextField类实现的焦点侦听器有疑问。只有在另一个Swing组件获得焦点时才会调用焦点侦听器。但是如果我通过用鼠标拖动来移动JFrame istelf,则永远不会调用focusLost()方法(换句话说,似乎焦点从CustomTextField转移到JFrame)。

编辑:我的问题的解决方案如下:

import java.awt.*;
import java.awt.event.*;
import java.util.Vector;
import javax.swing.*;

public class ScrollFocus extends JFrame {

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {

        @Override
        public void run() {
            new ScrollFocus();
        }
    });
}

public ScrollFocus() {
    this.setLayout(new BorderLayout());
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Vector<String> values = new Vector<>();
    values.add("a");
    values.add("b");
    values.add("c");
    values.add("d");
    values.add("e");
    JComboBox<String> comboBox = new JComboBox<>(values);
    JScrollPane scrollPane = new JScrollPane(comboBox);
    this.add(scrollPane, BorderLayout.NORTH);

    CustomTextField customTextField = new CustomTextField();
    this.add(customTextField, BorderLayout.CENTER);

    JButton button = new JButton("press");
    final JPopupMenu menu = new JPopupMenu("Menu");
    menu.add(new JMenuItem("Test"));
    button.setComponentPopupMenu(menu);
    this.add(button, BorderLayout.SOUTH);

    pack();
    setVisible(true);
}

class CustomTextField extends JTextField implements FocusListener {

    private CustomPopup customPopup = new CustomPopup();

    public CustomTextField() {
        this.addFocusListener(this);


        this.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0), "VK_UP");
        this.getActionMap().put("VK_UP", new AbstractAction() {

            @Override
            public void actionPerformed(ActionEvent e) {
                setPopupSize();
                customPopup.show(CustomTextField.this, CustomTextField.this.getX(), CustomTextField.this.getY() + CustomTextField.this.getHeight());    
                customPopup.setSelectedIndex(0);
            }
        });
        this.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0), "VK_DOWN");
        this.getActionMap().put("VK_DOWN", new AbstractAction() {

            @Override
            public void actionPerformed(ActionEvent e) {
                setPopupSize();
                customPopup.show(CustomTextField.this, CustomTextField.this.getX(), CustomTextField.this.getY() + CustomTextField.this.getHeight());
                customPopup.setSelectedIndex(0);
            }
        });
    }

    public void setPopupSize() {
        customPopup.setPopupSize(new Dimension(this.getWidth(), 110));
    }

    @Override
    public void focusGained(FocusEvent e) {
    }

    @Override
    public void focusLost(FocusEvent e) {
    }

    class CustomPopup extends JPopupMenu {
        String[] values = new String[]{"Value1", "Value2", "Value3", "Value4", "Value5", "Value6", "Value7",
                "Value8","Value9", "Value10", "Value11", "Value12", "Value13", "Value14", "Value15", "Value16",};
        JList<String> list = new JList<>(values);
        JScrollPane scrollPane = new JScrollPane(list);
        public int index = 0;

        public CustomPopup() {
            this.setLayout(new GridLayout(0,1));
            this.add(scrollPane);
            this.addKeyListener(new KeyAdapter() {
                @Override
                public void keyPressed(KeyEvent e) {
                    if(e.getKeyCode() == KeyEvent.VK_UP){
                        if(customPopup.index > 0)
                            customPopup.setSelectedIndex(--customPopup.index);
                    }
                    else if(e.getKeyCode() == KeyEvent.VK_DOWN){
                        if(customPopup.index < customPopup.getListSize()-1)
                            customPopup.setSelectedIndex(++customPopup.index);
                    }
                }
            });
            this.addFocusListener(new FocusAdapter() {
                @Override
                public void focusLost(FocusEvent e) {
                    index=0;
                }
            });
            pack();

        }

        public void setSelectedIndex(int index) {
            list.setSelectedIndex(index);
            list.ensureIndexIsVisible(index);
            requestFocus();
        }

        public int getListSize() {
            return values.length;
        }
    }
}
}

2 个答案:

答案 0 :(得分:1)

//customPopup.setVisible(true);
customPopup.show((JComponent)e.getSource(), 0, 20);

您应该使用show(...)方法来显示弹出窗口。这必须在弹出窗口中添加一些侦听器,这样您就不再需要在文本字段上使用FocusListener了。

然而,现在这是一个不同的问题。文本字段失去焦点,因此Action永远不会被调用。这没关系,但JList永远不会获得焦点,所以除非你先点击列表框,否则它不响应上/下键。我不确定这里有什么问题。

也许您可以尝试制作弹出窗口,滚动窗格并列出所有不可聚焦的内容,以便焦点保留在文本字段中?

答案 1 :(得分:0)

'焦点',这无疑是一个含糊不清的术语,通常适用于组件,而不是整个窗口。我们想到了“有焦点的窗口”,但我认为我们真正的意思是“当前窗口,包含焦点的窗口”。如果我移动窗口(也称为JFrame)本身,我不希望调用focus_lost。

另一种思考方式;如果我有一个文本字段,单击它,然后键入一两个字母,我会在该文本字段中看到这些字母。如果我稍微移动窗口并键入另一个或两个字母,我仍然希望这些字母出现在该字段中。它仍然具有焦点,从未失去它。