【问题标题】:How can I get the mouse coordinates relative to a JFrame rather than the entire screen如何获取相对于 JFrame 而不是整个屏幕的鼠标坐标
【发布时间】:2021-10-14 04:28:40
【问题描述】:

问题: 我想获取鼠标相对于 JFrame 而不是屏幕的坐标。我正在使用带有 JPanel 数组的屏幕,当我单击它们时它们会改变颜色。我该怎么做?

我能找到的大多数解决方案都建议获取组件的坐标,但我不能这样做,因为这意味着它也会找到相对于每个 JPanel 的坐标?

代码:

    public TestCoordinates() {
            initComponents();
            JPanel[][] arrGrid = new JPanel[3][4];
            int tileColour = 0;
    
            //nested for loops create the grid
            for (int y = 0; y < 3; y++) {
                tileColour++;
                for (int x = 0; x < 4; x++) {
                    arrGrid[y][x] = new JPanel();
                    add(arrGrid[y][x]);
                    arrGrid[y][x].setBounds((x * 100) + 100, (y * 100) + 100, 100, 100);
                    arrGrid[y][x].setVisible(true);
    
                    //sets grid colour to alternating colours
                    tileColour++;
                    double tileColourDouble = tileColour;
                    double tileColour2 = tileColourDouble / 2;
                    arrGrid[y][x].setBackground(Color.cyan);
                    if ((tileColour2 == Math.floor(tileColour2)) && !Double.isInfinite(tileColour2)) {
                        arrGrid[y][x].setBackground(Color.green);
                    } else {
                        arrGrid[y][x].setBackground(Color.cyan);
                    }
    
                    //adds mouse listener
                    arrGrid[y][x].addMouseListener(new MouseAdapter() {
                        @Override
                        public void mouseClicked(MouseEvent e) {
                            int yCo = getRow(e.getLocationOnScreen().y - 100);
                            int xCo = getColumn(e.getLocationOnScreen().x - 100);
                            System.out.println(e.getLocationOnScreen());
                            System.out.println(yCo + ", " + xCo);
                            arrGrid[yCo][xCo].setBackground(Color.gray);
                        }
    
                        //converts screen location to single x-coordinate of clicked JPanel's y-coordinate
                        private int getRow(int y) {
                            int row = 0;
                            for (int i = 0; i < y / 100; i++) {
                                row++;
                            }
                            return row;
                        }
    
                        //converts screen location to single x-coordinate of clicked JPanel's x-coordinate
                        private int getColumn(int x) {
                            int column = 0;
                            for (int i = 0; i < x / 100; i++) {
                                column++;
                            }
                            return column;
                        }
                    });
                }
            }
        }

【问题讨论】:

  • 为什么不在框架中添加一个 MouseListener 呢?此侦听器仅用于获取 JFrame 上的鼠标信息。
  • 某事like this 可能会为你做这件事

标签: java swing jframe jpanel mouselistener


【解决方案1】:

我想获取鼠标的坐标...我正在使用带有 JPanel 数组的屏幕,当我单击它们时会改变颜色。

您不需要鼠标坐标。您可以只使用MouseEventgetComponent() 方法来获取您单击的组件。然后改变背景。

arrGrid[y][x].addMouseListener(new MouseAdapter() {

您当前的代码正在为每个组件创建一个唯一的侦听器。这是不必要的。

改为创建一个可供所有面板共享的通用侦听器。所以在你的循环代码之外,你会做这样的事情:

MouseListener ml = new MouseAdapter()
{
    @Override mousePressed(MouseEvent e)
    {
        Component panel = e.getComponent();
        panel.setBackground( Color.GRAY );
    }
};

然后在您的循环代码中,您只需执行以下操作:

arrGrid[y][x].addMouseListener( ml );

【讨论】:

  • 我仍然需要 JPanel 的坐标,因为我正在将它们读入数据库。有没有办法解决这个问题?
  • @NiNoes 欢迎您的帮助。您最初的问题是关于单击面板并更改已解决的颜色。 我仍然需要 JPanel 的坐标 - 仍然不明白你在问什么。您可以访问面板的位置。因此,您可以使用这些值,或者如果您要求单击的网格,那么您可以使用位置除以 100,就像您目前所做的那样。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-08-03
  • 1970-01-01
  • 2013-02-04
  • 1970-01-01
  • 2015-08-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多