gpt4 book ai didi

java - 如何在涵盖的 JPanel 上调用 MotionListener

转载 作者:行者123 更新时间:2023-12-02 05:51:39 25 4
gpt4 key购买 nike

这个问题是为了帮我解决我的另一个question 。我有一个完全被 JLabels 覆盖的 JPanel。我已向面板添加了一个 MotionListener ,但由于标签挡住了它,因此从未调用它。如果我删除标签,我的 MotionnListener 就会被正确调用。

有没有办法让被覆盖的面板看到运动事件?

public class ChangesAttempt extends JPanel {
private static final long serialVersionUID = -8031881678612431401L;

static JFrame frame;
static JPanel grid;
static JLabel[][] squares;
static boolean[][] litSquares;
static int size, boardSize;
MouseEvent listener = new MouseEvent();
MotionListener mListerner = new MotionListener();
Color backGround = Color.BLUE;
Color selected = Color.PINK;

public ChangesAttempt(int size) {
ChangesAttempt.size = size;
squares = new JLabel[size][size];
litSquares = new boolean[size][size];
grid = new JPanel(new GridLayout(size, size));
grid.addMouseMotionListener(mListerner);
/*Coment out setBoard(); below to see the mousedDragged method actually being called
since the JLabels will not be in the way.*/
setBoard();

frame = new JFrame();
frame.setLayout(new BorderLayout());
frame.add(grid, BorderLayout.CENTER);
frame.setTitle("ChangedLife");
if (25 * size < 525)
boardSize = 525;
else
boardSize = 25 * size;
frame.setSize(boardSize, boardSize);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}

class MouseEvent implements MouseListener {

@Override
public void mousePressed(java.awt.event.MouseEvent e) {
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
if (e.getSource() == squares[i][j]) {
if (litSquares[i][j] == false) {
squares[i][j].setBackground(selected);
litSquares[i][j] = true;
} else {
squares[i][j].setBackground(backGround);
litSquares[i][j] = false;
}
}
}
}
}

@Override
public void mouseClicked(java.awt.event.MouseEvent e) {
}

@Override
public void mouseEntered(java.awt.event.MouseEvent e) {
}

@Override
public void mouseExited(java.awt.event.MouseEvent e) {
}

@Override
public void mouseReleased(java.awt.event.MouseEvent e) {
}
}

class MotionListener implements MouseMotionListener {
@Override
public void mouseDragged(java.awt.event.MouseEvent e) {
Point p = e.getPoint();
System.out.println("mouse Dragged to " + p);
}

@Override
public void mouseMoved(java.awt.event.MouseEvent e) {
// TODO Auto-generated method stub

}
}

public void setBoard() {
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
squares[i][j] = new JLabel();
squares[i][j].addMouseListener(listener);
squares[i][j].setOpaque(true);
squares[i][j].setBackground(backGround);
grid.add(squares[i][j]);
}
}
}

class MyLabel extends JLabel {
private static final long serialVersionUID = -1414933339546989142L;

}

public static void main(String args[]) {
new ChangesAttempt(20);
}
}

最佳答案

没有“简单”的方法来实现这一点,最好将所有鼠标监听器移至面板。

这将要求您在指定的点击点查找组件(位于父组件内),例如...

@Override
public void mousePressed(java.awt.event.MouseEvent e) {
Component component = e.getComponent();
Component source = component.getComponentAt(e.getPoint());
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
if (source == squares[i][j]) {
if (litSquares[i][j] == false) {
squares[i][j].setBackground(selected);
litSquares[i][j] = true;
} else {
squares[i][j].setBackground(backGround);
litSquares[i][j] = false;
}
}
}
}
}

如果您从标签中删除 MouseListener 并将其添加到网格,从而允许您的 MouseMotionListener

关于java - 如何在涵盖的 JPanel 上调用 MotionListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23484255/

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