我在HS的最后一年正在开发一款平台游戏。我最近决定实施HUD,可以在其中暂停,保存游戏或退出游戏。 除了检测鼠标事件之外,我已经成功地照顾了类,对象和图形。
Button
类是private sub-class
类的Hud
,它扩展了JComponent
并实现了MouseListener
。所有(必需的)MouseListener方法都已实现,但是当鼠标进入Button object
的边界时,似乎并没有触发它们。
在搜索该问题时,一些答案是说addMouseListener(this)
在班上失踪了,但我已经知道了。
如何使Button
类检测鼠标事件?
(即,悬停或单击)
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import javax.swing.JComponent;
public class Hud {
private ArrayList<Button> Buttons;
public Hud() {
System.out.println("Hud created");
init();
}
private void init() {
Buttons = new ArrayList<Button>();
Buttons.add(new Button(10, 10, 100, 40, "Save", 10, new Color(255,126,126), new Color(255,78,78),Color.WHITE));
Buttons.add(new Button(115, 10, 100, 40, "Pause", 10, new Color(255,126,126), new Color(255,78,78),Color.WHITE));
Buttons.add(new Button(220, 10, 100, 40, "Quit", 10, new Color(255,126,126), new Color(255,78,78),Color.WHITE));
Buttons.add(new Button(Config.WINDOW_WH[0]-110, 10, 100, 40, "Settings", 10, new Color(255,126,126), new Color(255,78,78),Color.WHITE));
}
public void update() {
for(Button b: Buttons)
b.update();
}
public void render(Graphics2D g) {
for(Button b: Buttons) {
b.render(g);
}
}
/**
Custom Buttons
**/
private class Button extends JComponent implements MouseListener {
/**
*
*/
private static final long serialVersionUID = 1L;
private int width, height, x, y, border_radius;
private String btnText;
private Font font;
private Color fontColor;
/*
* Blue = (126,172,255)
* Red = (255,126,126)
* Green = (179,255,126)
* Purple = (243,126,255)
*/
private Color btnColor;
private Color hoverColor;
private boolean mouseEntered, mouseClicked, mouseReleased;
public Button(int x, int y, int width, int height, String btnText, int borderRadius, Color defaultColor, Color hoverColor, Color fontColor) {
this.width = width;
this.height = height;
this.x = x;
this.y = y;
this.btnText = btnText;
this.border_radius = borderRadius;
this.btnColor = defaultColor;
this.font = new Font("Monospaced", Font.BOLD, 15);
this.fontColor = fontColor;
addMouseListener(this);
mouseEntered = mouseClicked = mouseReleased = false;
}
public void update() {
/*if((Game.lastMouse_clickX > this.x && Game.lastMouse_clickX < this.x + this.width)
&& (Game.lastMouse_clickY > this.y && Game.lastMouse_clickY < this.y + this.height)) {
System.out.println("Clicked on button");
}*/
}
public void render(Graphics2D g) {
printBtnText(g);
}
public void printBtnText(Graphics2D g) {
g.setColor(this.btnColor);
g.fillRoundRect(this.x, this.y, this.width, this.height, this.border_radius, this.border_radius);
g.setColor(this.fontColor);
g.setFont(this.font);
g.drawString(this.btnText, this.x+(this.width/this.btnText.length()), this.y+25);
}
@Override
public void mouseClicked(MouseEvent e) { this.mouseClicked = true; System.out.println("Clicked");}
@Override
public void mousePressed(MouseEvent e) { this.mouseReleased = false; System.out.println("Pressed");}
@Override
public void mouseReleased(MouseEvent e) { this.mouseReleased = true; System.out.println("Released");}
@Override
public void mouseEntered(MouseEvent e) { this.mouseEntered = true; System.out.println("Mouse entered"); }
@Override
public void mouseExited(MouseEvent e) { this.mouseEntered = false; System.out.println("exited");}
}
}
答案 0 :(得分:0)
Java自定义按钮,扩展的Jcomponent
它可以扩展JComponent,但它不是组件。
要使用组件,您需要创建该组件的一个实例并将其添加到JPanel。
相反,您只是将一些图形渲染到面板上。
要创建真实的组件,请执行以下操作:
请阅读Custom Painting的Swing教程中的部分,以获取入门的实用示例。