0

I am trying to make a menu class for a small game I'm making, and it all works fine, except now, the KeyListener does not work at all, I tried applying it to JFrame and JPanel, but nothing is working...

Here is my MainClass:

package bombermangame;

import javax.swing.JFrame;

public class MainClass {

    public static int WIDTH = 870, HEIGHT = 800;

    public static JFrame frame = new JFrame();
    public static Menu menu = new Menu();

    public static void main(String[] args) {
        frame.setContentPane(menu);
        frame.pack();
        frame.setResizable(false);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setTitle("BomberMan V0.3");
        frame.setSize(WIDTH, HEIGHT);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}
`

And here is my Menu class:

package bombermangame;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.JButton;
import javax.swing.JPanel;

public class Menu extends JPanel implements ActionListener {
    private static final long serialVersionUID = 1L;

    private JButton startButton = new JButton("Play");

    private int x = 0, y = 500;

    private boolean down = false;
    private boolean up = true;

    private Timer timer = new Timer();

    public Menu() {
        setBackground(Color.blue);
        startButton = new JButton("Start");
        startButton.setBounds(0,0, 100, 40);
        startButton.setPreferredSize(new Dimension(100, 40));
        startButton.addActionListener(this);
        startButton.setFocusPainted(true);
        add(startButton);

    }

    public void actionPerformed(ActionEvent ae) {
        Object a = ae.getSource();
        Game game = new Game();
        Listener keys = new Listener();

        if (a == startButton) {
            timer.cancel();
            MainClass.frame.remove(MainClass.menu);
            MainClass.frame.setContentPane(game);
            MainClass.frame.addKeyListener(keys);
            game.addKeyListener(keys);
            game.setBackground(Color.BLACK);
            game.setDoubleBuffered(true);
            game.setBounds(0, 0, WIDTH, HEIGHT);
            Game.running = true;
        }
    }

}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
joejoethemonkey
  • 63
  • 1
  • 2
  • 12
  • 1
    1) For Swing, we typically use [key bindings](https://docs.oracle.com/javase/tutorial/uiswing/misc/keybinding.html) rather than the lower level `KeyListener`. 2) Use a [`CardLayout`](http://download.oracle.com/javase/8/docs/api/java/awt/CardLayout.html) as shown in [this answer](http://stackoverflow.com/a/5786005/418556). 3) `Listener keys = new Listener();.. MainClass.frame.addKeyListener(keys); game.addKeyListener(keys);` Note that a `Listener` may be the ancestor of a `KeyListener`, but for this it would need `Listener keys = new KeyListener() {..};` – Andrew Thompson Apr 18 '15 at 00:00
  • Listener() is a class tht i have tht implements KeyListener – joejoethemonkey Apr 18 '15 at 00:06
  • *"Listener() is a class tht i have tht implements KeyListener"* For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example) or [SSCCE](http://www.sscce.org/) (Short, Self Contained, Correct Example). But seriously, use key bindings. They get around many of the problems of key listeners. which often fail due to the component not being focusable, not having focus, or both. – Andrew Thompson Apr 18 '15 at 00:08
  • 2
    Search the forum. This question is asked daily. For example look at the links found under the `Related` heading on the right side of the page. – camickr Apr 18 '15 at 00:09
  • @camickr *"For example look at the links found under the `Related` heading"* For some reason, I always forget that handy reference.. Thanks for the reminder. – Andrew Thompson Apr 18 '15 at 00:11

0 Answers0