Java 自定义按钮(不是 JButton)问题

标签 java swing button mouselistener

好吧,所以,我一直在试图弄清楚如何为所有按钮制作一个自定义 MouseListener,而无需在处理程序中列出每个按钮,因为我将有一个很多。这是我的监听器中现在的代码:

包 com.dinobuilding.handler;

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

import com.BLANK.BLANKScreen;
import com.BLANK.BLANKWindow;
import com.BLANK.menufeature.Button;

public class ButtonHandler implements MouseListener {

public BLANKWindow dbw;
public BLANK Screen dbs;

static Button button = new Button();

public int buttonX = button.x;
public int buttonY = button.y;
public int buttonSizeX = button.xSize;
public int buttonSizeY = button.ySize;

public ButtonHandler(BLANKWindow dbw, BLANKScreen dbs) {
    this.dbw = dbw;
    this.dbs = dbs;
}

public static void setButton(Button b) {
    button = b;
}

public int mouseEventX;
public int mouseEventY;

Graphics g;

public void mouseClicked(MouseEvent e) {
    mouseEventX = e.getLocationOnScreen().x;
    mouseEventY = e.getLocationOnScreen().y;

    if(mouseEventX <= buttonX && mouseEventX >= buttonX + buttonSizeX) {
        if(mouseEventY <= buttonY && mouseEventY >= buttonY + buttonSizeY) {
            button.onClicked(dbs, dbw, g);
        }
    }
}

public void mousePressed(MouseEvent e) {

}

public void mouseReleased(MouseEvent e) {

}

public void mouseEntered(MouseEvent e) {

}

public void mouseExited(MouseEvent e) {

}

这是我尝试实现的第一个按钮中的代码:

package com.BLANK.menus;

import java.awt.Color;
import java.awt.Graphics;

import com.BLANK.BLANKScreen;
import com.BLANK.BLANKWindow;
import com.BLANK.handler.ButtonHandler;
import com.BLANK.menufeature.Button;

public class MainMenuPlayButton extends Button {

public static int x;
public static int y;
public static int xSize;
public static int ySize;
public static String s;
public static Graphics g;

public MainMenuPlayButton(int x, int y, int xSize, int ySize, String s, Graphics g) {
    super(x, y, xSize, ySize, s, g);
    this.x = x;
    this.y = y;
    this.xSize = xSize;
    this.ySize = ySize;
    this.s = s;
    this.g = g;
    setColor(new Color(0, 226, 26));
    draw();
}

public MainMenuPlayButton() {

}

public static void draw() {
    drawButton(x, y, xSize, ySize, g, s);
    ButtonHandler.setButton(new MainMenuPlayButton());
}

public void onClicked(BLANKScreen dbs, BLANKWindow dbw, Graphics g) {
    setColor(new Color(216, 0, 0));
}

我认为我的主要问题是 ButtonHandler 中的代码在 Button 类中的代码之前被调用,因此 ButtonHandler 正在利用 Button 类本身,而不是 MainMenuPlayButton 类。如果您还需要 Button 类,请告诉我,但我无法想象为什么。预先感谢您!

<小时/>

编辑

好吧,经过一些调试,我发现我实际上遇到了相反的问题。该按钮永远不会被单击。 getSource() 方法可以工作,但是我真的不知道如何使用它,并且我认为如果不对每个按钮进行硬编码,我就无法使用它,这确实是我不知道的想做。

编辑 1: 您认为我可以使用 MouseEvent 的 getXgetXOnScreen 吗?顺便说一下,我在 JFrame 上使用frame.addMouseListener 注册了 ButtonHandler,所以...

编辑 2: getX 方法似乎也不起作用。如果您能在这方面帮助我,我将非常感激。

最佳答案

如果您想获取被按下并触发 MouseListener 的对象,请使用 MouseEvent 的 getSource() 方法。例如,这可能有效:

public void mouseClicked(MouseEvent e) {
    (YourButton) button = (YourButton) e.getSource();
    button.onClicked(...);

}

其他位:

  • 将您的类从 Button 重命名为其他名称,因为 Button 名称与 java.awt.Button 类冲突,这可能会导致难以调试的错误。
  • 每当我看到在类中声明 Graphics 字段时,我都会感到畏缩,因为它表明可能存在不恰当的绘画。如果您使用其中之一作为字段,请确保您确实知道自己在做什么,因为如果使用不当,很容易导致图像丢失或 NullPointerException,因为 Java 经常更改 Graphics 对象,并且此更改是完全超出你(程序员)的控制范围。不要说你没有被警告过。
<小时/>

编辑
关于您的评论:

Yes, I do know what I'm doing with the Graphics field, however, if it makes you feel better, know that it's only temporary and I will be changing it to something else later.

好吧,我之前就被这个问题困扰过。只要您从 BufferedImage 获取它,并且不要尝试通过在组件上调用 getGraphics() 或将其从 Paint 或 PaintComponent 方法中拉出来获取它,那么您可能没问题。

<小时/>

Also, I'm pretty sure that I'm getting the object it clicked correctly, but I can't get it to access the correct subclass of Button. It's only getting the Button class itself, not the MainMenuPlayButton.

抱歉,但这没有意义,因为当您获取引用时,您不会获得“类”,这是一个纯粹而简单的对象,事实上您会获得与添加 ButtonListener 的对象完全相同的对象,并且使监听器绊倒,并且此引用的类将是您的按钮所属的类。我假设您将 MouseListener 直接添加到“Button”对象,对吗?再次,是时候进行一些调试了。

<小时/>

编辑2
关于您的问题的最新编辑:

Ok, after debugging some, I have found that I in fact have the opposite problem. The button is never being clicked. The getSource() method could work, however I don't really know how to use that and I don't think that I could use that without hardcoding every single button, which is really something I do not want to do.

不,没有必要对每个按钮进行硬编码,相信我。这就是使用添加到按钮的监听器的全部原因。

EDIT 1: Do you think maybe I could do use the MouseEvent's getX or getXOnScreen? By the way, I registered the ButtonHandler using frame.addMouseListener on my JFrame, so... the

这是你的一个问题。如果您想监听按钮,您将希望能够在按钮本身上注册监听器。如果您有一个数组或它们的集合,则注册监听器很容易。不,我不建议在屏幕上使用 x 和 y,因为它会使您的程序极其脆弱。如果您这样做,对 GUI 结构的任何更改都需要对 x 和 y 处理进行后续硬编码更改。呃。

这就引出了一个问题:为什么要创建自己的 Button 类,以及为什么不使用 JButtons 或 JButtons 的子类。您似乎正在重新发明轮子,但(抱歉直言不讳)创造了一个方形的轮子。

<小时/>

编辑3

But you cast the variable to a button, meaning that if I have multiple buttons I have to cast each and every one of them to a different thing.

不,绝对不行,因为多态性的魔力应该在这里发挥作用。但它们是同一类型的对象,不是吗?或者您的 Button 类是否有许多不同的子类?不管怎样,在 mouseClicked(...) 方法内部,您似乎只想调用按钮上的一个方法,即 onClicked(...) ,我将其Imagine 必须是父类(super class)的对象,对吧?因此,通过在当前按钮上调用此方法,它应该调用自己的正确代码。

The problem I have with JButton is that they already exist. I can't edit them and I can't customize them, ...

这显然是不正确的。您可以通过多种方式更改它们的外观和行为,包括通过子类化或通过工厂创建方法。另外,它们已经配备了能够注册监听器并响应鼠标操作的机制。

...Also, would I have to register/make a new handler for each and every one of the buttons?

您似乎再次忘记了多态性应该解决所有这些问题。一个处理程序应该执行此操作,具体取决于您的代码的表现如何。

I am going to have a LOT of buttons, and I don't think that that would be a viable solution. If not the getX how would I get it to do something when the thing is clicked?

我已经向您提供了我的建议,但有时如果可以改进设计,最好重新编写代码部分,这意味着您可能需要考虑改进代码以使用 JButtons。

关于Java 自定义按钮(不是 JButton)问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25836423/

相关文章:

java - 在mysql中使用数组查询java

java - 默认单元格编辑器类查询?

java - 如何在Java swing中查看数据库结果集

java - 错误 : SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length

javascript - 如何使用 .ajax() 创建保存按钮

jsf - JSF CommandButton onclick不调用Javascript函数

java - 如何检查任何系统上用户的访问级别

Java将文件读入char二维数组

Java Swing OSX 窗口菜单复选标记

java - IntelliJ IDEA - 错误 : JavaFX runtime components are missing,,需要运行此应用程序