gpt4 book ai didi

java - 未检测到 MouseListener

转载 作者:行者123 更新时间:2023-11-30 06:56:27 24 4
gpt4 key购买 nike

我有一个 JPanel(网格),嵌套在主 JFrame 中。我正在尝试在网格上设置一个 Mouselistener 并将事件报告给主框架。

App.java (入口点)

public class App {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new MainBoard("Tic Swing Toe");
}
});
}
}

MainBoard.java (主容器, Controller )

public class MainBoard extends JFrame {

public static final int WINDOW_MIN_WIDTH = 800;
public static final int WINDOW_MIN_HEIGHT = 700;

private HeaderPanel mHeaderPanel; // The header panel
private BoardPanel mBoardPanel; // The game board panel

public MainBoard(String windowTitle) {
// Set window title
super(windowTitle);

setMinimumSize(new Dimension(WINDOW_MIN_WIDTH, WINDOW_MIN_HEIGHT));
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setContentPane(new TilePanel());
setLayout(new BorderLayout());
pack();
setVisible(true);

// Setup components
initGUI();

// Setup listeners on child components
setupChildListeners();
}

/**
* Setup the GUI components
*/
private void initGUI() {
mHeaderPanel = new HeaderPanel();
mBoardPanel = new BoardPanel();

add(mHeaderPanel, BorderLayout.NORTH);
add(mBoardPanel, BorderLayout.WEST);
}

/**
* Sets listeners on child components
*/
private void setupChildListeners() {
mBoardPanel.setHoverListener((x, y) -> System.out.println(x + " - " + y));
}

/**
* The background of the application
*/
class TilePanel extends JPanel {

private BufferedImage mTile;

public TilePanel() {
try {
// Read image from URL, could change it to disk file
mTile = ImageIO.read(new URL("http://turbo.designwoop.com/uploads/2012/03/01_free_subtle_textures_apple_ios_linen_texture.jpg"));
} catch (IOException e) {
e.printStackTrace();
}
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);

Graphics2D g2d = (Graphics2D) g.create();
int tileWidth = mTile.getWidth();
int tileHeight = mTile.getHeight();
for (int y = 0; y < getHeight(); y += tileHeight) {
for (int x = 0; x < getWidth(); x += tileWidth) {
g2d.drawImage(mTile, x, y, this);
}
}
g2d.dispose();
}
}
}

BoardPanel.java (我感兴趣的包含网格的 JPanel)

public class BoardPanel extends JPanel {

public static final int BOARD_BORDER_RADIUS = 20;
public static final int CELL_MARGIN = BOARD_BORDER_RADIUS / 2;
public static final int CELL_SIZE = 120;
public static final int BOARD_WIDTH = CELL_SIZE * 3 + CELL_MARGIN * 4;
public static final int BOARD_HEIGHT = CELL_SIZE * 3 + CELL_MARGIN * 4;

// The hover listener that shall report to the
private CellHoverListener mHoverListener;

public void setHoverListener(CellHoverListener listener) {
mHoverListener = listener;
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);

addMouseListener(new BoardMouseListener());
}

// A custom MouseListener
class BoardMouseListener implements MouseListener {

@Override
public void mouseClicked(MouseEvent e) { }

@Override
public void mousePressed(MouseEvent e) { }

@Override
public void mouseReleased(MouseEvent e) { }

@Override
public void mouseEntered(MouseEvent e) {
if(mHoverListener != null)
mHoverListener.onCellHover(e.getX(), e.getY());
}

@Override
public void mouseExited(MouseEvent e) { }
}

@Override
public Dimension getPreferredSize() {
return new Dimension(BOARD_WIDTH, BOARD_HEIGHT);
}
}

HeaderPanel.java(包含 2 个文本行的标题)

public class HeaderPanel extends JPanel {

public static final String HEADER_TEXT = "Pure Tic Tac Toe Java - AI";
public static final String SMALL_HEADER_TEXT =
"The only game where you are the champion if it's a draw";

// TODO: figure out a way to fill the whole header dynamically
public static final int HEADER_WIDTH = 1366; // temporary solution
public static final int HEADER_HEIGHT = 100;

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);

// Draw the dark blue rectangle
g.drawRect(0, 0, HEADER_WIDTH, HEADER_HEIGHT);
g.setColor(new Color(33, 33, 33, 240)); // Dark blue
g.fillRect(0, 0, HEADER_WIDTH, HEADER_HEIGHT);

// Draw the first line of text
g.setColor(Color.WHITE);
FontMetrics fm = g.getFontMetrics();
Rectangle2D header = fm.getStringBounds(HEADER_TEXT, g);
int x = (this.getWidth() - (int) header.getWidth()) / 2;
int y = (this.getHeight() - (int) header.getHeight()) / 4 + fm.getAscent();
g.drawString(HEADER_TEXT, x, y);

// Draw the second line of text
fm = g.getFontMetrics();
Rectangle2D miniHeader = fm.getStringBounds(SMALL_HEADER_TEXT, g);
x = (this.getWidth() - (int) miniHeader.getWidth()) / 2;
y = (this.getHeight() - (int) miniHeader.getHeight()) * 3 / 4 + fm.getAscent();
g.setColor(new Color(44, 170, 231, 250));
g.drawString(SMALL_HEADER_TEXT, x, y);
}

@Override
public Dimension getPreferredSize() {
return new Dimension(HEADER_WIDTH, HEADER_HEIGHT);
}
}

CellHoverListener.java

public interface CellHoverListener {
void onCellHover(int x, int y);
}

最终结果:

Result

问题

是根本没有检测到悬停事件。我尝试直接在 BoardPanel 上设置一个 MouseListener,但还是老样子。

在这种情况下,如何在鼠标事件上正确设置监听器

最佳答案

检测移动事件的主要问题是您没有分配MouseMotionListener:

例如,为您的面板创建一个构造函数:

public BoardPanel() {

BoardMouseListener bml = new BoardMouseListener();
addMouseListener(bml);
addMouseMotionListener(bml);
}

如果您不需要构造函数,也可以在您设置组件的任何地方。然后为您要检测的任何事件使用 @Override 方法:

class BoardMouseListener extends MouseAdapter {

@Override
public void mouseEntered(MouseEvent e) {

System.out.println(e.getX() + " - " + e.getY());
}

@Override
public void mouseMoved(MouseEvent e) {

System.out.println(e.getX() + " - " + e.getY());
}
}

您的其他问题是:

  • paintComponent 中调用 addMouseListener。每次绘制组件时,您都在创建并注册一个新的监听器。这很糟糕。
  • setVisible 应仅在您完成所有组件的设置后调用。
  • 如果您不需要重写其所有方法,请使用extends MouseAdapter而不是implements MouseListener。如果您在移动过程中按下按钮,则不会“记录”该按钮,因为这现在是一个拖动事件 - 使用适当的方法。

关于java - 未检测到 MouseListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34360135/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com