1

我正在尝试使用 Java 的 KeyListener 在我键入时更新 JLabel。本质上,我正在制作自己的文本字段。这是我所拥有的:

/**
     * Constructor for objects of class Dictionary
     */
    public Dictionary()
    {
        frame = new JFrame();
        frame.setTitle("Shori Dictionary");
        frame.setLayout(new GridBagLayout());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

private void createWord()
    {
        frame.remove(pane);
        pane = new PaintPane(field.getImage());
        pane.setLayout(new BorderLayout());
        frame.add(pane);
        frame.pack();

        newWord = new JLabel(text);
        newWord.setFont(newWord.getFont().deriveFont(Font.BOLD, 28));
        newWord.setForeground(Color.BLACK);
        newWord.setHorizontalTextPosition(JLabel.LEFT);
        newWord.setVerticalAlignment(JLabel.TOP);
        newWord.setVerticalTextPosition(JLabel.TOP);
        newWord.setBorder(BorderFactory.createEmptyBorder(445, 150, 0, 0));
        pane.add(newWord);
        frame.pack();

        frame.setLocationRelativeTo(null);
        pane.setFocusable(true);
        updateInteraction();
    }

    private void keyPress()
    {
        pane.addKeyListener(new KeyListener()
        {
            public void keyTyped(KeyEvent e) {
                for(int i = 97; i <= 122; i++){
                    //Cycles through every lowercase letter
                    if(e.getKeyChar() == (char)(i)&& pane.returnImage() == field.getImage()){
                        text += (char)(i);
                        break;
                    }
                }
                //Even in the Debugger, these next if-elses have never worked
                if(e.getKeyCode() == KeyEvent.VK_SPACE&& pane.returnImage() == field.getImage()) text += " ";
                else if(e.getKeyCode() == KeyEvent.VK_BACK_SPACE&& pane.returnImage() == field.getImage()){
                    int x = text.length();
                    text = text.substring(0,x-1); //Not sure if this works, haven't been able to test it yet
                }
                else if(e.getKeyCode() == KeyEvent.VK_ENTER&& pane.returnImage() == field.getImage()){
                    //do something with the text
                    text = "";
                    //exit the word creator
                }
                newWord.setText(text);
                newWord.repaint(); //Apparently this isn't necessary...
            }
            public void keyPressed(KeyEvent e) {
            }
            public void keyReleased(KeyEvent e) {
            }
        }
        );
    }

    private void mouseAction()
    {
        pane.addMouseListener(new MouseListener()
        {
            public void mouseClicked(MouseEvent arg0) {
                //cover page
                if(open.contains(arg0.getPoint())&& pane.returnImage() == cover.getImage()) displayPages();
                else if(search.contains(arg0.getPoint())&& pane.returnImage() == cover.getImage()) searchWord();
                else if(enter.contains(arg0.getPoint())&& pane.returnImage() == cover.getImage()) createWord();
                //inner pages
                else if(nextPage.contains(arg0.getPoint())&& pane.returnImage() == pages.getImage()) pageFlip("next");
                else if(prevPage.contains(arg0.getPoint())&& pane.returnImage() == pages.getImage()) pageFlip("previous");
                else if(cancel.contains(arg0.getPoint())&& pane.returnImage() == field.getImage()) coverPage();
                frame.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
            }
            public void mouseEntered(MouseEvent arg0) {
            }
            public void mouseExited(MouseEvent arg0) {
            }
            public void mousePressed(MouseEvent arg0) {
            }
            public void mouseReleased(MouseEvent arg0) {
            }
        }
        );
    }

    private void mouseMovement()
    {
        pane.addMouseMotionListener(new MouseMotionListener()
        {
            @Override
            public void mouseMoved(MouseEvent e) {
                if(search.contains(e.getPoint())&& pane.returnImage() == cover.getImage()){
                    frame.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
                }
                else if(enter.contains(e.getPoint())&& pane.returnImage() == cover.getImage()){
                    frame.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
                }
                else if(open.contains(e.getPoint())&& pane.returnImage() == cover.getImage()){
                    frame.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
                }
                else if(nextPage.contains(e.getPoint())&& pane.returnImage() == pages.getImage()){
                    frame.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
                }
                else if(prevPage.contains(e.getPoint())&& pane.returnImage() == pages.getImage()){
                    frame.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
                }
                else if(cancel.contains(e.getPoint())&& pane.returnImage() == field.getImage()){
                    frame.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
                }
                else{
                    frame.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
                }
            }
            @Override
            public void mouseDragged(MouseEvent e) {
            }
        }
        );
    }

    private void updateInteraction(){
        mouseMovement();
        mouseAction();
        keyPress();
    }

    public class PaintPane extends JPanel {
        private Image background;
        private Graphics g2d;

        public PaintPane(Image image) {
            background = image;            
        }

        @Override
        public Dimension getPreferredSize() {
            return background == null ? new Dimension(0, 0) : new Dimension(background.getWidth(this), background.getHeight(this));            
        }

        @Override
        protected void paintComponent(Graphics g) {

            super.paintComponent(g);

            if (background != null) {                
                Insets insets = getInsets();

                int width = getWidth() - 1 - (insets.left + insets.right);
                int height = getHeight() - 1 - (insets.top + insets.bottom);

                int x = (width - background.getWidth(this)) / 2;
                int y = (height - background.getHeight(this)) / 2;

                g.drawImage(background, x, y, this);
            }

            //g.fillRect(654, 798, 358, 77); //for testing rectangle positioning
        }

        public Image returnImage() {
            return background;
        }
    }

我正在使用 BluJ 来编写它,它有一个内置的调试器。我只是尝试在 createWord()keyPress();之前添加updateInteraction();,然后运行调试器逐步完成每个方法。一切都很完美。然后我尝试不使用调试器,在我输入时它不会显示任何文本。所以,我再次打开了调试器。它根本没有检测到任何正在键入的键。我不知道为什么它只起作用了一次,但它肯定起作用了。这是我第一次使用 KeyListener、MouseListener 和 MouseMotionListener。有没有更好的方法让这个程序正常运行?

4

3 回答 3

3

而且在我打字时它不会显示任何文字

组件需要具有焦点才能响应 KeyEvent。JPanel 默认情况下是不可聚焦的。

我正在制作自己的文本字段

为什么?JTextField 缺少什么功能?

我只会使用 JTextField,然后将 DocumentListener 从文本字段添加到文档中。阅读 Swing 教程中有关如何编写文档侦听器的部分以获取更多信息。

于 2013-10-30T20:46:07.103 回答
0

您的组件必须在Key Event. 但是,要检查它的实际效果,首先要让你的JPanle焦点通过 panel.setFocusable(true). 然后尝试panel.requestFocusInWindow() 将焦点放在应用程序启动面板上。

但是,在您的代码中:

for(int i = 97; i <= 122; i++){
                    //Cycles through every lowercase letter
                    if(e.getKeyChar() == (char)(i)&& pane.returnImage() == field.getImage()){
                        text += (char)(i);
                        break;
                    }
                }

为什么必须在这里使用循环?只是简单地说:

if(evt.getKeyChar() >=97 && evt.getKeyChar() <=122 && pane.returnImage() == field.getImage())
    text += evt.getKeyChar() 

应该做的工作。

于 2013-10-30T21:06:39.867 回答
0

如果你只是做一个学习练习,那么可以自己实现而不是使用 JTextField。您可以通过调用 JPanel 来接收关键事件setFocusable(true)

即使在那之后,您也应该意识到可能会绊倒您的一些事情: keyTyped回调不会填写 s 的keyCode属性KeyEvent。这是因为可以输入没有单个关联键的字符。例如,在 Windows 上,您可以通过键入 ALT+0160 触发一次 keyPressed。您按了 5 个键但输入了一个字符(一个空格)并得到了一个keyPressed回调。

总结一下: keyPressedand keyReleaseddeliverkeyCode可能deliver keyCharkeyTyped永远不会得到 a keyCode,总是得到 a keyChar

public class PanelKeyListener extends JFrame {

    private JPanel _contentPane;
    StringBuilder _stringBuilder = new StringBuilder();

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    PanelKeyListener frame = new PanelKeyListener();
                    frame.setSize(400, 400);
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public PanelKeyListener() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        _contentPane = new JPanel() {
            protected void paintComponent(java.awt.Graphics g) {
                g.drawString(_stringBuilder.toString(), 10, 20);
            }
        };
        setContentPane(_contentPane);
        _contentPane.setFocusable(true);
        _contentPane.addKeyListener(new KeyListener() {
            public void keyTyped(KeyEvent e) {
                System.out.println("keyTyped char[" + e.getKeyChar()
                        + "] code[" + e.getKeyCode() + "]");
                _stringBuilder.append(e.getKeyChar());
                _contentPane.repaint();
            }

            public void keyReleased(KeyEvent e) {
                System.out.println("keyReleased char[" + e.getKeyChar()
                        + "] code[" + e.getKeyCode() + "]");
            }

            public void keyPressed(KeyEvent e) {
                System.out.println("keyPressed char[" + e.getKeyChar()
                        + "] code[" + e.getKeyCode() + "]");
            }
        });
    }
}

如果可以避免,就不应该重新发明轮子。如果可行并满足您的需求,请使用/扩展 JTextField。

于 2013-10-30T21:04:05.973 回答