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

时间:2019-09-13 13:54:02

标签: java swing jframe jbutton grid-layout

我正在开发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 :(得分:0)

您要在每次循环迭代期间设置第二个单元格(索引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]