如何在 Java 网格 GUI 中选择特定单元格?



我正在开发一个GUI网格,我需要选择特定的单元格。我用了JFrame.我的目标是用数字完成第一列和最后一行,就像x-y轴一样。

我尝试声明一个具有我需要的确切单元格位置的JButtons数组,设置文本。(代码中的大写锁定注释(

import java.awt.Color;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
public class GridLayoutTest {
private static JButton[] arrayBtn;
public static void main(String[] args ) {
// the frame that contains the components
JFrame frame = new JFrame("GridLayoutTest from JCG");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// set the size of the frame
frame.setSize(1000, 1000);
// set the rows and cols of the grid, as well the distances 
between them
GridLayout grid = new GridLayout(10,14, 0, 0);
// what layout we want to use for our frame
frame.getContentPane().setBackground(Color.WHITE);;
frame.setLayout(grid);
arrayBtn = new JButton[140];
// add JButtons dynamically
for(int i=0; i < arrayBtn.length; i++) {
arrayBtn[i] = new JButton();  
arrayBtn[i].setBackground(Color.WHITE);
arrayBtn[i].setSize(1,1);
//IF I RUN ONLY THIS WORKS WITH THE FIRST CELL(IF I INSERT OUT 
// THE FOR CYCLE DOESN'T WORK)
arrayBtn[0].setText("9");
// IF I RUN ALSO THIS CODE ROW I HAVE THE SAME ERROR 
arrayBtn[1].setText("9");

frame.add(arrayBtn[i]);
}
frame.setVisible(true);
}
}

例外:

Exception in thread "main" java.lang.NullPointerException
relative to the row of the specific cell selected.

您在每次循环迭代期间设置第二个单元格(索引 1(,这意味着即使在第二个单元格未初始化的第一次迭代期间也是如此。

假设我们第一次进入循环。我们将初始化第一个按钮(索引 0(。然后,将第二个按钮(索引 1(的文本设置为"9"。此按钮尚未初始化,因此我们正在对仍在null的对象调用方法。这就是您收到NullPointerException的原因。

for (int i = 0; i < arrayBtn.length; i++) {
arrayBtn[i] = new JButton();
arrayBtn[i].setBackground(Color.WHITE);
arrayBtn[i].setSize(1, 1);
// This works (set every button text to "9"):
arrayBtn[i].setText("9");
// This will NOT work:
arrayBtn[1].setText("9"); 
frame.add(arrayBtn[i]);
}
// This works (set the first button text to "9"):
arrayBtn[0].setText("9");

请记住,数组索引从索引 0 开始:

String[] array = {"first", "second", "third"};
System.out.println(array[1]); // will print "second"

因此,在您的问题的完整上下文中:也许您应该考虑为您的按钮数组使用二维数组,因为它更接近您尝试构建的网格 (10x14(。

JButton[][] buttonGrid = new JButton[10][14];
for (int y = 0; y < 10; y++) {
for (int x = 0; x < 14; x++) {
buttonGrid[y][x] = new JButton("Test");
if (y == 0) {
// code in here is only executed for the first row
buttonGrid[y][x].setText(Integer.toString(x));
}
if (x == 0) {
// code in here is only executed for the first column
buttonGrid[y][x].setText(Integer.toString(y));
}
}
}

这将看起来像这样:

[0, 1,    2,    3,    4,    5,    6,    7,    8,    9,    10,   11,   12,   13  ]
[1, Test, Test, Test, Test, Test, Test, Test, Test, Test, Test, Test, Test, Test]
[2, Test, Test, Test, Test, Test, Test, Test, Test, Test, Test, Test, Test, Test]
[3, Test, Test, Test, Test, Test, Test, Test, Test, Test, Test, Test, Test, Test]
[4, Test, Test, Test, Test, Test, Test, Test, Test, Test, Test, Test, Test, Test]
[5, Test, Test, Test, Test, Test, Test, Test, Test, Test, Test, Test, Test, Test]
[6, Test, Test, Test, Test, Test, Test, Test, Test, Test, Test, Test, Test, Test]
[7, Test, Test, Test, Test, Test, Test, Test, Test, Test, Test, Test, Test, Test]
[8, Test, Test, Test, Test, Test, Test, Test, Test, Test, Test, Test, Test, Test]
[9, Test, Test, Test, Test, Test, Test, Test, Test, Test, Test, Test, Test, Test]

最新更新