gpt4 book ai didi

java - 如何获取 GridLayout 中元素的 X 和 Y 索引?

转载 作者:太空狗 更新时间:2023-10-29 22:50:04 26 4
gpt4 key购买 nike

我正在研究 java 教程,发现在 GridLayout 中查找 JButton 的 x/y 索引的方法是遍历与布局相关联的按钮 b 的二维数组,并检查是否

b[i][j] == buttonReference

  @Override
public void actionPerformed(ActionEvent ae) {
JButton bx = (JButton) ae.getSource();
for (int i = 0; i < 5; i++)
for (int j = 0; j < 5; j++)
if (b[i][j] == bx)
{
bx.setBackground(Color.RED);
}
}

有没有更简单的方法来获取按钮的 X/Y 索引?

类似于:

JButton button = (JButton) ev.getSource();
int x = this.getContentPane().getComponentXIndex(button);
int y = this.getContentPane().getComponentYIndex(button);

this 是一个 GameWindow 实例,ev ActionEvent 在用户按下按钮时触发。

在这种情况下它应该得到:x == 2, y == 1

@GameWindow.java:

package javaswingapplication;

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.*;
import javax.swing.*;

public class GameWindow extends JFrame implements ActionListener
{
JButton b[][] = new JButton[5][5];

int v1[] = { 2, 5, 3, 7, 10 };
int v2[] = { 3, 5, 6, 9, 12 };

public GameWindow(String title)
{
super(title);

setLayout(new GridLayout(5, 5));
setDefaultCloseOperation(EXIT_ON_CLOSE );

for (int i = 0; i < 5; i++)
for (int j = 0; j < 5; j++)
{
b[i][j] = new JButton();
b[i][j].addActionListener(this);
add(b[i][j]);
}
}

@Override
public void actionPerformed(ActionEvent ae) {
((JButton)ae.getSource()).setBackground(Color.red);
}
}

@JavaSwingApplication.java:

package javaswingapplication;

public class JavaSwingApplication {
public static void main(String[] args) {
GameWindow g = new GameWindow("Game");
g.setVisible(true);
g.setSize(500, 500);
}
}

最佳答案

这个例子展示了如何创建一个知道它在网格上的位置的网格按钮。 getGridButton() 方法展示了如何根据其网格坐标高效地获取按钮引用, Action 监听器显示点击和找到的按钮是相同的。

GridButtonPanel

package gui;

import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

/**
* @see http://stackoverflow.com/questions/7702697
*/
public class GridButtonPanel {

private static final int N = 5;
private final List<JButton> list = new ArrayList<JButton>();

private JButton getGridButton(int r, int c) {
int index = r * N + c;
return list.get(index);
}

private JButton createGridButton(final int row, final int col) {
final JButton b = new JButton("r" + row + ",c" + col);
b.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
JButton gb = GridButtonPanel.this.getGridButton(row, col);
System.out.println("r" + row + ",c" + col
+ " " + (b == gb)
+ " " + (b.equals(gb)));
}
});
return b;
}

private JPanel createGridPanel() {
JPanel p = new JPanel(new GridLayout(N, N));
for (int i = 0; i < N * N; i++) {
int row = i / N;
int col = i % N;
JButton gb = createGridButton(row, col);
list.add(gb);
p.add(gb);
}
return p;
}

private void display() {
JFrame f = new JFrame("GridButton");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(createGridPanel());
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {

@Override
public void run() {
new GridButtonPanel().display();
}
});
}
}

关于java - 如何获取 GridLayout 中元素的 X 和 Y 索引?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7702697/

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