【问题标题】:GetComponentAt TroubleGetComponentAt 麻烦
【发布时间】:2015-05-08 15:10:49
【问题描述】:

我在分层情况下遇到了 getcomponentat 问题。我进行了很多研究,发现以下线程实际上是我需要的,但它对我不起作用。我在线程中下载了代码并且它可以工作,但是当我在我的项目中实现它时它没有。我可能正在做一些我无法指出的非常愚蠢的错误。

我有一个具有基本面板的 JFrame。我添加了一个gridPanel(它在其上扩展了JPanel并实现了mouselistner。在网格面板上我添加了单元格(它扩展了JPanel并还实现了mouselistener)。当我单击任何单元格时,我想知道该单元格在网格,但一切都返回为 0,0。

GridLayout + Mouse Listener

就这样吧。

MAINCLASS 
mainFrame = new JFrame("Connect-4");
basePanel = new JPanel();
gridPanel = new Grid(); //Grid extends JPanel

//GRIDCLASS
public class Grid extends JPanel implements MouseListener {
public Grid(){
    //      setPreferredSize(new Dimension(600,700));;
    setLayout(new GridLayout(6, 7));
    for (int i = 0; i < 6; i++) {
        for (int j = 0; j < 7; j++) {
            Cell tempCell = new Cell(i,j); //Cell Exntends JPANEL
            tempCell.addMouseListener(this);
            gridUI[i][j] = tempCell;
            gridTrack[i][j] = 0;
            add(tempCell);

            int index = i*6 + j;
            cellArray.add(tempCell);

        }

    }
    addMouseListener(this);
}
public void mouseClicked(MouseEvent e) {
    // TODO Auto-generated method stub
    System.out.println("Grid Click");
    Cell clickedCell;
    Boolean filled = false;

    Point mousePoint;

    mousePoint = e.getPoint();
    System.out.println(mousePoint.x + "||" + mousePoint.y);
    clickedCell = (Cell)getComponentAt(mousePoint);


    //      Point mousePoint = MouseInfo.
    int cellIndex;
    cellIndex = Integer.parseInt(clickedCell.getName());
    int cellX = cellIndex / 7;
    int cellY = cellIndex % 7;
}


public class Cell extends JPanel implements MouseListener{

private String status; 
private Color curColor;
private Boolean occupied;
public static Boolean gameOver = false;
public static int player;
public static boolean randPlayer = false;
private Color player1 = Color.BLUE;
private Color player2 = Color.RED;
private static int[][] gridTrack = new int[6][7];
public int row,column;
public static int cellSize = 80;
public Cell(int row_in, int column_in){
    setPreferredSize(new Dimension(cellSize,cellSize));
    setBorder(BorderFactory.createLineBorder(Color.BLACK, 3));
    setBackground(Color.GRAY);
    player = 0;
    this.setName(Integer.toString(row_in*6+column_in));
    curColor = Color.WHITE;
    addMouseListener(this);
    occupied = false;
    player = 1;
    row = row_in;
    column = column_in;
    gridTrack[row][column] = 0;
}

【问题讨论】:

    标签: java swing grid jpanel mouselistener


    【解决方案1】:

    当监听器被触发时,事件的点相对于触发事件的组件。将MouseListeners 添加到Cell 会导致坐标相对于Cell - 因此在具有这些坐标的父Component 上使用getComponentAt 将始终在0,0 处返回Cell事件点的坐标永远不会大于单元格的宽度/高度。

    考虑使用 single 侦听器来处理行为,使用适当的技术来获取触发事件的组件:

    1. 为父级添加一个侦听器JPanel- 事件的坐标是相对于父级的。因此使用getComponentAt 将返回发生MouseEvent 的组件
    2. 为每个Cell 添加一个侦听器,并获取使用Cell cell = (Cell)e.getSource() 触发事件的Cell

    【讨论】:

    • 感谢您的回复。刚刚忙于一些事情,所以还没有合并这个。会告诉你进展如何!
    • 好的,它可以工作。但我再次需要一些指导。我从 Cell 类中删除了 MouseListner,并在 Grid 类中通过调用 e.getsource 获取了单元格。每次单击鼠标后,我都会检查网格是否有获胜者,当有人获胜时,我想将消息放在 UI 上。消息框在 PlayConnect 类中。我在这里使用 MouseListener(它之前工作过)。令我惊讶的是,仅调用了 MouseEntered 和 MouseExited 方法,但未单击其他方法。可能是什么原因?与原始问题相比,代码的唯一变化是从 Cell 中删除了 Interface。
    • 鉴于您的代码已更改,这似乎是一个与之前不同的问题,因为通过这样的 cmets 进行调试会带来挑战。
    • 发表了另一个问题..stackoverflow.com/questions/30199007/…
    猜你喜欢
    • 2010-11-11
    • 2011-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多