如何获取相对于 JFrame 而不是整个屏幕的鼠标坐标
How can I get the mouse coordinates relative to a JFrame rather than the entire screen
问题:
我想获取鼠标相对于 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;
}
});
}
}
}
I want to get the coordinates of my mouse... I am using a screen with an array of JPanels that change colour when I click on them.
您不需要鼠标坐标。您可以只使用 MouseEvent
的 getComponent()
方法来获取您单击的组件。然后换个背景。
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 );
问题: 我想获取鼠标相对于 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;
}
});
}
}
}
I want to get the coordinates of my mouse... I am using a screen with an array of JPanels that change colour when I click on them.
您不需要鼠标坐标。您可以只使用 MouseEvent
的 getComponent()
方法来获取您单击的组件。然后换个背景。
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 );