Search code examples
javaswingjbutton

JButtons aren't working?


For the game i'm making i made a button class to use for all the buttons in my menu. I finally managed to make my buttons appear on screen but now when i click them nothing happens.

my button class:

package menu;

import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

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

public class Button extends JButton{

    public JButton button;
    public ImageIcon buttonImage;

    public int width, height;

    public String backgroundPath;
    public int x, y;
        public ActionListener listener;



public Button(String backgroundPath,int x, int y, ActionListener listener)

    {
        super();
        this.backgroundPath = backgroundPath;
        this.x = x;
        this.y = y;
        this.addActionListener(listener);   

        buttonImage = new 
            ImageIcon(PlayPanel.class.getResource(backgroundPath));
        button = new JButton();
        this.setIcon(buttonImage);
        this.setBounds(x, y, buttonImage.getIconWidth(), 
            buttonImage.getIconHeight());


    }


}

i know there's probably an error in my menupanel (@ actionPerformed). the code looks like this:

package menu;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

@SuppressWarnings("serial")
public class MenuPanel extends JPanel implements ActionListener
{

    private Button playKnop, highScoreKnop, quitKnop, HTPKnop;
    private Tanks mainVenster;
    public MenuPanel menuPanel;

    int x = 95, width = 200, height = 50;



    public MenuPanel(Tanks mainVenster) 
    {
        this.mainVenster = mainVenster;
        this.setLayout(null); 

        playKnop = new Button("/buttons/PLAY.png",x, 350, menuPanel);       
        highScoreKnop = new Button("/buttons/HS.png",x, 460, menuPanel);
        HTPKnop = new Button("/buttons/HTP.png",x, 515, menuPanel);
        quitKnop = new Button("/buttons/QUIT.png",x, 570, menuPanel);


        this.add(playKnop);
        this.add(quitKnop);
        this.add(HTPKnop);
        this.add(highScoreKnop);

        validate();

    }


    public void actionPerformed(ActionEvent ae)
    {
        if (ae.getSource() == playKnop){
        mainVenster.switchPanel(new PlayPanel(mainVenster));

    } else if (ae.getSource() == quitKnop) {
        mainVenster.switchPanel(new QuitPanel(mainVenster));

    } else if (ae.getSource() == HTPKnop) {
        mainVenster.switchPanel(new HTPPanel(mainVenster));

    } else if (ae.getSource() == highScoreKnop) {
        mainVenster.switchPanel(new HSPanel(mainVenster));
    }

    }

}

i think the error is the fact that i'm writing mainVenster (venster = dutch for panel/window) and i should probably write other parameters. the problem is that i don't know which ones.


Solution

  • You are defining your buttons in the listener, so you must refer to class itself as this:

    playKnop = new Button("/buttons/PLAY.png",x, 350, this);       
    highScoreKnop = new Button("/buttons/HS.png",x, 460, this);
    HTPKnop = new Button("/buttons/HTP.png",x, 515, this);
    quitKnop = new Button("/buttons/QUIT.png",x, 570, this);
    

    You already have a menuPanel attribute, so when creating the class you can assign a value, in this case this.

    menuPanel = this;
    

    But I wont recommend you delete this attribute or rename it to _this or _menuPanel to agree conventions.