我有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个文本行的标题)
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);
}
根本没有检测到悬停事件。我尝试直接在MouseListener
上设置BoardPanel
,但行为相同。
在这种情况下,如何在鼠标事件上正确设置侦听器?
答案 0 :(得分:2)
首先,从addMouseListener()
移除paintComponent()
。请注意,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());
}
}
答案 1 :(得分:2)
检测到移动事件的主要问题是您没有分配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());
}
}
您的其他问题是:
addMouseListener
内拨打paintComponent
。每次绘制组件时,您都在创建并注册新的侦听器。这很糟糕。setVisible
。extends MouseAdapter
代替implements MouseListener
。如果您在移动过程中按下按钮,则无法记录&#34;因为现在这是一个拖延事件 - 使用适当的方法。