0

I am making a Basketball scoreboard GUI and I have trouble getting the key listener to respond. Im trying to get the JLabel homescore to plus 1 every time the user presses the F key.

Help would be greatly appreciated. Have a good day :).

public class Scoreboard extends JFrame implements KeyListener
{
    public Scoreboard() {
        UIManager.put("Label.foreground", Color.WHITE);
        UIManager.put("Panel.background", Color.BLACK);
        super.setTitle("Scoreboard");
        setLayout(new GridLayout(0, 3, 0, 0));

        home = new JPanel();

        setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);

        home.setLayout(new GridLayout(2, 0, 0, 10));

        Font font = new Font("Advanced LED Board-7", Font.BOLD, 50);
        homeScore = new JLabel(Integer.toString(homePoints));
        homeScore.addKeyListener(this);
        homeScore.setFont(score);
        homeScore.setHorizontalAlignment(JLabel.CENTER);
        border = BorderFactory.createTitledBorder(hName); // adds title border
        border.setTitleFont(font);
        border.setTitleColor(Color.WHITE);
        border.setTitleJustification(TitledBorder.CENTER);
        homeScore.setBorder(border);
        home.add(homeScore);
    }
     public void keyTyped(KeyEvent e) 
     {

     }

    public void keyPressed(KeyEvent e) 
    {
        int keyCode = e.getKeyCode();
        if (keyCode == KeyEvent.VK_F) 
        {
            homePoints++;
            homeScore.setText(Integer.toString(homePoints));
        }

    }

    public void keyReleased(KeyEvent e) {
        // TODO Auto-generated method stub

    }
jajaja234
  • 1
  • 2
  • does this help your situation? https://stackoverflow.com/questions/16530775/keylistener-not-working-for-jpanel – Kcits970 Feb 16 '20 at 05:57
  • 1
    Does this answer your question? [Keylistener not working for JPanel](https://stackoverflow.com/questions/16530775/keylistener-not-working-for-jpanel) – Ewan Brown Feb 16 '20 at 06:18
  • 1
    See [Java KeyListener vs Keybinding](https://stackoverflow.com/questions/23486827/java-keylistener-vs-keybinding) – c0der Feb 16 '20 at 08:40

0 Answers0