0

I have a JLabel which has a string that has an integer, which starts at 0. I want it so that way, every time a key (like "w") is pressed, the integer goes up by 1. I have searched the web far and wide, but I have returned nothing (maybe because of my wording). Here's the code:

    public void keyTyped(KeyEvent e) {
        //keyTyped = Invoked when a key is typed. Uses KeyChar, char output
        switch(e.getKeyChar()) {
            case 'a': label.setLocation(label.getX()-10, label.getY());
            for(int coins=0; coins<1;coins++) {
                coins = coins + 1;
            }
                break;
            case 'w': label.setLocation(label.getX(), label.getY()-10);
            for(int coins=0; coins<1;coins++) {
                coins = coins + 1;
            }
                break;
            case 's': label.setLocation(label.getX(), label.getY()+10);
            for(int coins=0; coins<1;coins++) {
                coins = coins + 1;
            }
                break;
            case 'd': label.setLocation(label.getX()+10, label.getY());
            for(int coins=0; coins<1;coins++) {
                coins = coins + 1;
            }
                break;
        }
        
    }

Maybe it is what I wrote in the code and it won't change? I didn't see similar questions like mine.

LeninYT
  • 1
  • 3

1 Answers1

0

See How to Use Key Bindings for example

import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.border.EmptyBorder;

public class Main {
    public static void main(String[] args) {
        new Main();
    }

    public Main() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private JLabel coinLabel;
        private int coins;

        public TestPane() {
            setLayout(new GridBagLayout());
            setBorder(new EmptyBorder(32, 32, 32, 32));
            coinLabel = new JLabel("0");

            add(coinLabel);

            InputMap im = getInputMap(WHEN_IN_FOCUSED_WINDOW);
            ActionMap am = getActionMap();

            CoinAction.Observer observer = new CoinAction.Observer() {
                @Override
                public void coinDidChange(CoinAction action, int delta) {
                    coins += delta;
                    coinLabel.setText(Integer.toString(coins));
                }
            };

            im.put(KeyStroke.getKeyStroke(KeyEvent.VK_W, 0), "Pressed.Up");
            im.put(KeyStroke.getKeyStroke(KeyEvent.VK_S, 0), "Pressed.Down");
            im.put(KeyStroke.getKeyStroke(KeyEvent.VK_A, 0), "Pressed.Left");
            im.put(KeyStroke.getKeyStroke(KeyEvent.VK_D, 0), "Pressed.Right");

            am.put("Pressed.Up", new CoinAction(1, observer));
            am.put("Pressed.Down", new CoinAction(-1, observer));
            am.put("Pressed.Left", new CoinAction(10, observer));
            am.put("Pressed.Right", new CoinAction(-10, observer));
        }

    }

    public class CoinAction extends AbstractAction {

        public interface Observer {
            public void coinDidChange(CoinAction action, int delta);
        }

        private int delta;
        private Observer observer;

        public CoinAction(int delta, Observer observer) {
            this.delta = delta;
            this.observer = observer;
        }

        public int getDelta() {
            return delta;
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            observer.coinDidChange(this, getDelta());
        }

    }
}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366