从基于文本字段的搜索栏中转义

8wigbo56  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(373)

我有一个带有搜索栏的菜单栏,它是用 JTextField 这样地:

public class Ui_Frame {
    private static JFrame f;
    private static DrawPanel drawPanel;

    public static void main(String[] args) {
        SwingUtilities.invokeLater(Ui_Frame::createAndShowGUI);
    }

    private static void createAndShowGUI() {

        f = new JFrame("Frame");
        drawPanel = new DrawPanel(); //A class that extends JPanel where I draw

        JMenuBar menubar = new JMenuBar();
        JMenu j_menu_data = new JMenu("Data");
        JTextField j_menu_searchfield = new JTextField();
        j_menu_searchfield.setSize(new Dimension(100,20));
        menubar.add(j_menu_data);
        menubar.add(j_menu_searchfield);
        f.setJMenuBar(menubar);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(drawPanel);
        f.pack();
        f.setVisible(true);
   }
}

我有 KeyListener 代表 DrawPanel 同学们,这些很好用。问题是当我添加搜索栏时 JTextField 对于上面的菜单栏,我写的所有内容都被写入文本字段,并且我的键侦听器不会触发。我无法“走出”文本字段,因此如果我在绘图区域内单击,我按的所有键仍会放入文本字段。
我试过了 getFocus() 对于 DrawPanel 但无济于事。
如何解决这个问题?
编辑:drawpanel类,以便您拥有运行它所需的所有类:

public class DrawPanel extends JPanel {
    public DrawPanel() {
        addKeyListener(new CustomKeyListener());
        this.setBackground(new Color(220,220,220));
        setBorder(BorderFactory.createLineBorder(Color.black));
        setFocusable(true);
        setVisible(true);
    }

    public Dimension getPreferredSize() {
        return new Dimension(500, 500);
    }

    protected void paintComponent(Graphics g) {
        Graphics2D g2D = (Graphics2D) g;
        super.paintComponent(g2D);
    }

    class CustomKeyListener implements KeyListener {
        @Override
        public void keyTyped(KeyEvent e) {
            if (e.getKeyChar() == KeyEvent.VK_SPACE) {
                System.out.println("Pressed SPACE");
            }

        }
        @Override
        public void keyPressed(KeyEvent e) { }
        @Override
        public void keyReleased(KeyEvent e) { }
    }
}
jm81lzqq

jm81lzqq1#

启动swing应用程序时 JTextField 最初是键盘焦点。你知道这一点是因为你看到光标在 JTextField .
点击 DrawPanel 用鼠标不能将键盘焦点转移到 DrawPanel . 您知道这一点是因为在 DrawPanel ,光标仍在 JTextField .
您可以将键盘焦点从 JTextFieldDrawPanel 点击键盘上的tab键,因为这是默认的焦点遍历键。你知道 JTextField 不再具有键盘焦点,因为其中没有闪烁的光标。
如果你真的想要 DrawPanel 要通过在键盘中单击鼠标获得键盘焦点,可以添加 MouseListenerDrawPanel ,如下面的代码所示,它本质上是您的代码(对于类 DrawPanel )带着 MouseListener 补充。
请注意,我没有换班 Ui_Frame .

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.BorderFactory;
import javax.swing.JPanel;

public class DrawPanel extends JPanel implements MouseListener {
    public DrawPanel() {
        addKeyListener(new CustomKeyListener());
        addMouseListener(this);
        this.setBackground(new Color(220,220,220));
        setBorder(BorderFactory.createLineBorder(Color.black));
        setFocusable(true);
        setVisible(true);
    }

    public Dimension getPreferredSize() {
        return new Dimension(500, 500);
    }

    protected void paintComponent(Graphics g) {
        Graphics2D g2D = (Graphics2D) g;
        super.paintComponent(g2D);
    }

    class CustomKeyListener implements KeyListener {
        @Override
        public void keyTyped(KeyEvent e) {
            if (e.getKeyChar() == KeyEvent.VK_SPACE) {
                System.out.println("Pressed SPACE");
            }
        }

        @Override
        public void keyPressed(KeyEvent e) { }

        @Override
        public void keyReleased(KeyEvent e) { }
    }

    @Override
    public void mouseClicked(MouseEvent mouseEvent) {
        System.out.println(this.requestFocusInWindow());
    }

    @Override
    public void mousePressed(MouseEvent mouseEvent) {
    }

    @Override
    public void mouseReleased(MouseEvent mouseEvent) {
    }

    @Override
    public void mouseEntered(MouseEvent mouseEvent) {
    }

    @Override
    public void mouseExited(MouseEvent mouseEvent) {
    }
}

注意这个方法 requestFocusInWindow() 退货 true 如果成功的话。

相关问题