即使 windows 未聚焦,也使 JTextField 接收输入

Make a JTextField recieve input even with windows not focused

我正在做一个小的个人项目。

我有两个 JFrame windows、ViewerConsole

我需要确保当我按下键盘上的键时,相应的文本会出现在控制台 JTextField 中,不仅是当焦点被 JTextField 保持时,而且当它在我的应用程序中包含任何其他组件。

换句话说,我希望 JTextField 在我单击 Viewer 框架后立即接受输入。

这样可行吗?

以防万一,我 运行 win 8 x64 并且我并不真正关心可移植性(我是唯一一个会查看或使用此代码的人)。

编辑:这是我的代码示例:

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;



public class main {
    public static Viewer v; 
    public static Console c;
    public static void main(String[] args) {
        v=new Viewer();
        c=new Console();
    }


    static class Viewer extends JFrame {
        public Viewer(){
            setSize(600,600);
            getContentPane().add(new myPanel());
            addMouseListener(new mouse());
            setVisible(true);

        }
    }
    static class myPanel extends JPanel{
        public void paintComponent(Graphics g){
            g.setColor(new Color((int)(Math.random()*255),(int)(Math.random()*255),(int)(Math.random()*255)));
            g.fillRect(0, 0, 600, 600);
        }
    }
    static class Console extends JFrame {
        public Console(){
            setSize(600,200);
            JTextField text=new JTextField();
            getContentPane().add(text);
            setVisible(true);

        }
    }
    static class mouse implements MouseListener{
        @Override
        public void mouseClicked(MouseEvent arg0) {
            v.repaint();
        }

        @Override
        public void mouseEntered(MouseEvent arg0) {
        }

        @Override
        public void mouseExited(MouseEvent arg0) {
        }

        @Override
        public void mousePressed(MouseEvent arg0) {
        }

        @Override
        public void mouseReleased(MouseEvent arg0) {
        }
    }

}

在这个例子中,在我点击大window改变颜色后,如果我想在另一个window中写东西,我必须先点击它。

我可以推荐 KeyboardFocusManager 吗?我发现这是实现我认为您正在努力实现的目标的最简单方法

KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(
  new KeyEventDispatcher() {
     public void dispatchKeyEvent(KeyEvent ke) {
       //TODO: fill this in
     }
  });

一个不优雅的解决方案是创建一个 KeyListener,它将输入的字符提供给您的控制台,尽管具体如何发生取决于您创建组件的方式。为了这个例子,我将假装通过你的 Console class 中的静态方法来完成它(最好你可以访问 Console 的实例):

public class ApplicationKeyListener implements KeyListener {
    public ApplicationKeyListener() {
    }

    // Other necessary methods...

    public void keyPressed(KeyEvent e) {
        char pressed = e.getKeyChar();
        Console.feedChar(pressed);
    }
}

然后在 Console 中,确保您的 JTextField 对象是全局和静态的,然后设置 feedChar() 方法。

public class Console extends JFrame {
    private static JTextField consoleTextField;

    public Console() {
        consoleTextField = new JTextField();
        // ...
    }

    public static feedChar(char c) {
        consoleTextField.setText(consoleTextField.getText() + c.toString());
    }
}

最后,您必须将此侦听器添加到所有 JFrames 及其子项。

public class Viewer extends JFrame {
    public Viewer() {
        ApplicationKeyListener kl = new ApplicationKeyListener();
        addKeyListener(kl);
        for (Component child : this.getComponents()) {
            child.addKeyListener(kl);
        }
    }
}