MD. NASHID KAMAL MD. NASHID KAMAL - 14 days ago 4
Java Question

JButton Not working in My java Code

My Mouse click not working. I tried it with diffrent types.
Here is the Button Class and Display Class. There are no error in my project. But mouse not working. Here, i implemented mouse listener interface in button class. then i created a button class object, then added ation listener to that button object.


Button Class:


import java.awt.Font;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JButton;

public class Button extends JButton implements MouseListener{

private boolean click = false;

public Button(String text) {
super(text);
setFont(new Font(null, Font.BOLD, 30));
setText(text);
}

@Override
public void mouseClicked(MouseEvent arg0) {
click = true;
}

@Override
public void mouseEntered(MouseEvent arg0) {

}

@Override
public void mouseExited(MouseEvent arg0) {

}

@Override
public void mousePressed(MouseEvent arg0) {

}

@Override
public void mouseReleased(MouseEvent arg0) {

}

public boolean getClick() {
return click;
}

}



Display Class:


import java.awt.Toolkit;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.JFrame;

import com.js.CandyThief.CustomButton.Button;


public class Display{

private String title;
private int width;
private int height;

private JFrame frame;

Button btnPlay = new Button("Play");
Button btnHelp = new Button("Help");
Button btnCredits = new Button("Credits");
public Button btnExit = new Button("Exit");


public Display(String title, int width, int height){
super();
this.title = title;
this.width = width;
this.height = height;

createDisplay();
}
private void createDisplay(){
frame = new JFrame(title);
frame.setSize(width, height);
frame.setFocusable(true);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

btnPlay.setBounds(300, 285, 450, 50);
btnPlay.setBorderPainted(true);
frame.add(btnPlay);

btnHelp.setBounds(300, 350, 450, 50);
btnHelp.setBorderPainted(true);
frame.add(btnHelp);

btnCredits.setBounds(300, 415, 450, 50);
btnCredits.setBorderPainted(true);
frame.add(btnCredits);

btnExit.setBounds(300, 480, 450, 50);
btnExit.setBorderPainted(true);
frame.add(btnExit);

Background background = new Background();
frame.add(background);

frame.setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/windowIcon.png")));

btnExit.addActionListener(new AbstractAction() {

@Override
public void actionPerformed(ActionEvent arg0) {
if (btnExit.getClick()) {
System.exit(0);
}
}
});
}



}

Answer

Implementing a MouseListener interface does not make the class use the MouseListener.

In your case you have to add the MouseListener to your Button:

http://docs.oracle.com/javase/7/docs/api/java/awt/Component.html#addMouseListener(java.awt.event.MouseListener)

public Button(String text) {
    super(text);
    setFont(new Font(null, Font.BOLD, 30));
    setText(text);
    addMouseListener(this);
}
Comments