未检测到 MouseListener
MouseListener not getting detected
我有一个 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(主容器,控制器)
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 个文本行的 header)
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);
}
最终结果:
问题
是根本没有检测到悬停事件。我试图直接在 BoardPanel
上设置 MouseListener
,但还是老样子。
在这种情况下,如何在 鼠标事件 上正确设置 listener?
首先,从 paintComponent()
中删除 addMouseListener()
。请注意 paintComponent()
被重复调用——我确定您不打算在每个 repaint()
.
上继续添加新的鼠标侦听器
protected void paintComponent(final Graphics g) {
super.paintComponent(g);
}
在构造函数或某种 initGUI()
调用中添加您的侦听器:
public BoardPanel() {
addMouseListener(new BoardMouseListener());
}
在此处添加调试语句。你会看到它确实被调用了。
public void mouseEntered(final MouseEvent e) {
System.out.println(e);
if (mHoverListener != null) {
mHoverListener.onCellHover(e.getX(), e.getY());
}
}
你的 main 检测移动事件的问题是你没有分配 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
。如果您在移动过程中按下按钮,则不会 "recorded" 因为这是一个拖动事件 - 使用适当的方法。
我有一个 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(主容器,控制器)
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 个文本行的 header)
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);
}
最终结果:
问题
是根本没有检测到悬停事件。我试图直接在 BoardPanel
上设置 MouseListener
,但还是老样子。
在这种情况下,如何在 鼠标事件 上正确设置 listener?
首先,从 paintComponent()
中删除 addMouseListener()
。请注意 paintComponent()
被重复调用——我确定您不打算在每个 repaint()
.
protected void paintComponent(final Graphics g) {
super.paintComponent(g);
}
在构造函数或某种 initGUI()
调用中添加您的侦听器:
public BoardPanel() {
addMouseListener(new BoardMouseListener());
}
在此处添加调试语句。你会看到它确实被调用了。
public void mouseEntered(final MouseEvent e) {
System.out.println(e);
if (mHoverListener != null) {
mHoverListener.onCellHover(e.getX(), e.getY());
}
}
你的 main 检测移动事件的问题是你没有分配 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
。如果您在移动过程中按下按钮,则不会 "recorded" 因为这是一个拖动事件 - 使用适当的方法。