我如何 select 特定单元格进入 Java 网格 GUI?
How do I select specific cells into a Java grid GUI?
我正在开发 GUI 网格,我需要 select 特定的单元格。我用了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"
所以在你的问题的完整上下文中:也许你应该考虑使用 two-dimensional array 作为你的按钮数组,因为它更接近你试图构建的网格 (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]
我正在开发 GUI 网格,我需要 select 特定的单元格。我用了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"
所以在你的问题的完整上下文中:也许你应该考虑使用 two-dimensional array 作为你的按钮数组,因为它更接近你试图构建的网格 (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]