【问题标题】:How do I select specific cells into a Java grid GUI?如何在 Java 网格 GUI 中选择特定单元格?
【发布时间】:2019-09-14 11:09:32
【问题描述】:

我正在开发一个 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.

【问题讨论】:

    标签: java swing jframe jbutton grid-layout


    【解决方案1】:

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

    【讨论】:

    • 感谢您的回复!使用您的解决方案,我在所有单元格中都获得了相同的值。如何仅在特定单元格中获取值?
    • 我更新了我的答案。请记住,如果您只想设置一个单元格,您应该在循环之外进行,否则每次循环迭代都会执行该行。这不是必需的!
    • 谢谢!我已经看到更新了。只有 arrayBtn[0].setText("9");现在工作!但如果我还添加 arrayBtn[1].setText("9");我得到与以前相同的错误。你知道为什么吗?
    • 您只能在已初始化的对象上调用方法。如果在尚未使用 new JButton() 初始化的 JButton 上调用 setText(),它将始终生成 NullPointerException。你在哪里调用 arrayBtn[1].setText("9") ?在循环内部?
    • 我已经解决了在 for 循环之后插入这些代码行的问题!谢谢
    猜你喜欢
    • 2021-02-27
    • 2013-10-11
    • 2013-09-02
    • 1970-01-01
    • 2011-01-14
    • 1970-01-01
    • 2023-01-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多