简体   繁体   English

JLabel上的setText在KeyPressed方法内不起作用

[英]setText on JLabel doesn't work inside KeyPressed method

Whenever I try to use setText inside the KeyPressed method it doesn't work, altough when I use it in a different method (initComponents) inside the same class it works there. 每当我尝试在KeyPressed方法中使用setText时,它都将不起作用,但当我在同一类中的其他方法(initComponents)中使用它时,它就可以正常工作。

Feel free to ask any other code if it's necessary! 如有必要,请随时询问其他任何代码!

This is the KeyPressed method where it doesn't work: 这是KeyPressed方法不起作用的地方:

    @Override
public void keyTyped(KeyEvent e) {
    char typed = e.getKeyChar();

    if (Character.isLetter(typed) && r.getHuidigeKolom() < r.getAantalLetters()) {
        typed = Character.toUpperCase(typed);
        r.getLetters()[r.positie(r.getHuidigeRij(), r.getHuidigeKolom())].setText(typed + "");
        r.getLetters()[r.positie(r.getHuidigeRij(), r.getHuidigeKolom())].setBackground(Color.blue);

        if (r.getHuidigeKolom() == 0) {
            for (int i = 1; i < r.getAantalLetters(); i++) {
                r.getLetters()[r.positie(r.getHuidigeRij(), i)].setText(".");
                r.getLetters()[r.positie(r.getHuidigeRij(), i)].setBackground(Color.blue);
            }

            r.volgendeKolom(true);

            if (r.getHuidigeKolom() < r.getAantalLetters()) {
                r.getLetters()[r.positie(r.getHuidigeRij(), r.getHuidigeKolom())].setBackground(hoverKleur);
            }

            if (typed == 10 && r.getHuidigeKolom() >= r.getAantalLetters()) {   //typed 10 is ENTER
                this.controle();
            }

            if (typed == 8 && r.getHuidigeKolom() > 0) {    //typed 8 is BACKSPACE
                this.eentjeTerug();
            }
        }
    }
}

The setText method does work in this method: setText方法可以在以下方法中工作:

    private void initComponents(String woord) {
    this.setLayout(new GridLayout(r.getAantalPogingen(), r.getAantalLetters(), 2, 2));
    for (int i = 0; i < r.getAantalPogingen() * r.getAantalLetters(); i++) {
        r.getLetters()[i] = new Label();
        r.getLetters()[i].setBackground(Color.white);
        r.getLetters()[i].setForeground(Color.black);
        r.getLetters()[i].setAlignment(Label.CENTER);
        r.getLetters()[i].setFont(new Font("Groot", Font.BOLD, 48));
        this.add(r.getLetters()[i]);
    }

    for (int i = 0; i < 5; i++) {
        r.getLetters()[i].setText(woord.charAt(i) + "");
        r.getLetters()[i].setBackground(Color.blue);
    }

    r.setHuidigeKolom(0);
    r.setHuidigeRij(0);
}

I really appreciate any help you can provide. 我非常感谢您可以提供的任何帮助。

Without a MCTRE it's going to be a little difficult to pinpoint the exact cause of your problem, but I'm guessing that the root of your problem is that you're using Key Listeners instead of Key Bindings. 没有MCTRE ,查明问题的确切原因会有些困难,但是我猜测问题的根源在于您使用的是键侦听器而不是键绑定。

A KeyListener is very picky about what component is focused, which is likely the problem you're running into. KeyListener对要聚焦的组件非常挑剔,这很可能是您遇到的问题。 It won't fire unless the component it was added to has the application's focus (so it's not ideal to use with containers). 除非添加了该组件的组件具有应用程序的焦点,否则它不会触发(因此,与容器一起使用不是理想的选择)。 Here's a quick example of how to use a Key Binding : 这是一个如何使用键绑定的简单示例:

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

public class KeyBindings extends Box{
    public KeyBindings(){
        super(BoxLayout.Y_AXIS);
        final JTextPane textArea = new JTextPane();
        textArea.insertComponent(new JLabel("Text"));
        add(textArea);

        Action action = new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                textArea.setText("New Text");
            }};
         String keyStrokeAndKey = "control SPACE";
         KeyStroke keyStroke = KeyStroke.getKeyStroke(keyStrokeAndKey);
         textArea.getInputMap().put(keyStroke, keyStrokeAndKey);
         textArea.getActionMap().put(keyStrokeAndKey, action);
    }


    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setContentPane(new KeyBindings());
        frame.pack();
        frame.setVisible(true);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM