JButton 在我的 java 代码中不起作用
JButton Not working in My java Code
我的鼠标点击不起作用。我尝试了不同的类型。
这是按钮 Class 和显示 Class。我的项目没有错误。但是鼠标不工作。在这里,我在按钮 class 中实现了鼠标监听器接口。然后我创建了一个按钮 class 对象,然后向该按钮对象添加了一个监听器。
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);
}
}
});
}
}
实现 MouseListener 接口不会使 class 使用 MouseListener。
在您的情况下,您必须将 MouseListener 添加到 Button:
public Button(String text) {
super(text);
setFont(new Font(null, Font.BOLD, 30));
setText(text);
addMouseListener(this);
}
Use JButton Direct if You are Using developing code with Swing no need to create Button Class Its already persent in javax.swing..
use
import javax.swing.*;
JButton btnPlay = new JButton("Play");
JButton btnHelp = new JButton("Help");
JButton btnCredits = new JButton("Credits");
JButton btnExit = new JButton("Exit");
Instead of this code..
Button btnPlay = new Button("Play");
Button btnHelp = new Button("Help");
Button btnCredits = new Button("Credits")
public Button btnExit = new Button("Exit");
我的鼠标点击不起作用。我尝试了不同的类型。 这是按钮 Class 和显示 Class。我的项目没有错误。但是鼠标不工作。在这里,我在按钮 class 中实现了鼠标监听器接口。然后我创建了一个按钮 class 对象,然后向该按钮对象添加了一个监听器。
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);
}
}
});
}
}
实现 MouseListener 接口不会使 class 使用 MouseListener。
在您的情况下,您必须将 MouseListener 添加到 Button:
public Button(String text) {
super(text);
setFont(new Font(null, Font.BOLD, 30));
setText(text);
addMouseListener(this);
}
Use JButton Direct if You are Using developing code with Swing no need to create Button Class Its already persent in javax.swing..
use
import javax.swing.*;
JButton btnPlay = new JButton("Play");
JButton btnHelp = new JButton("Help");
JButton btnCredits = new JButton("Credits");
JButton btnExit = new JButton("Exit");
Instead of this code..
Button btnPlay = new Button("Play");
Button btnHelp = new Button("Help");
Button btnCredits = new Button("Credits")
public Button btnExit = new Button("Exit");