简体   繁体   English

Java Swing 帮助

[英]Java Swing Help

I'm trying to create a grid of JButtons , with index values to the left and right of the button grid.我正在尝试创建一个JButtons网格,索引值位于按钮网格的左侧和右侧。

Here is my code, but I'm getting a NullPointerException .这是我的代码,但我得到了NullPointerException What is the problem?问题是什么?

public class Test {
public static void main(String args[]) {
    JFrame myapp = new JFrame("test");
    myapp.setLayout(new FlowLayout());

    myapp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    myapp.setSize(400,400);
    myapp.setVisible(true);

    myapp.getContentPane().add(Board());
}

private static JPanel Board() {
    JButton[][] buttons = new JButton [10][10];

    JPanel board = new JPanel(new GridLayout(11,11));

    String[] rowlabels = {" ","A","B","C","D","E","F","G","H","I","J"};
    String[] columnlabels = {" ","1","2","3","4","5","6","7","8","9","10"};
    JTextField k;

    for (int i = 0; i < 11; i++) {
        for (int j = 0; j < 11; j++) {
            if ((j != 0) && (i != 0)) {
                //myboard[i-1][j-1].addActionListener(new ButtonHandler());
                board.add(buttons[i-1][j-1]);
            }
            if (i == 0) {
                if (j != 0) {
                    // used to display row of numbers
                    k = new JTextField(columnlabels[j]);
                    k.setEditable(false);
                    k.setHorizontalAlignment((int) JFrame.CENTER_ALIGNMENT);
                } else {
                    // used to display column of numbers
                    k = new JTextField();
                    k.setEditable(false);
                }
                board.add(k);
            } else if (j == 0) {
                k = new JTextField(rowlabels[i]);
                k.setEditable(false);
                k.setHorizontalAlignment((int) JFrame.CENTER_ALIGNMENT);
                board.add(k);
            }
        }
    }

    return board;
}
}

I'm probably doing something obvious, but Swing is kind of new to me, so any help would be greatly appreciated.我可能正在做一些显而易见的事情,但 Swing 对我来说有点新,所以任何帮助将不胜感激。

Swing GUIs should be constructed on the EDT. Swing GUI 应在 EDT 上构建。 That part is left as an exercise for the OP.该部分留作 OP 的练习。

import java.awt.*;
import javax.swing.*;

public class Test {
    public static void main(String args[]) {
        JFrame myapp = new JFrame("test");
        myapp.setLayout(new FlowLayout());

        myapp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        myapp.getContentPane().add(Board());

        // very important!
        myapp.pack();

        // do these last.
        myapp.setSize(400,400);
        myapp.setVisible(true);

    }

    private static JPanel Board() {
        JButton[][] buttons = new JButton [10][10];
        for(int x=0; x<buttons.length; x++) {
            for (int y=0; y<buttons[0].length; y++) {
                buttons[x][y] = new JButton();
            }
        }

        JPanel board = new JPanel(new GridLayout(11,11));

        String[] rowlabels = {" ","A","B","C","D","E","F","G","H","I","J"};
        String[] columnlabels = {" ","1","2","3","4","5","6","7","8","9","10"};
        JTextField k;

        for (int i = 0; i < 11; i++) {
            for (int j = 0; j < 11; j++) {
                if ((j != 0) && (i != 0)) {
                    //myboard[i-1][j-1].addActionListener(new ButtonHandler());
                    board.add(buttons[i-1][j-1]);
                }
                if (i == 0) {
                    if (j != 0) {
                        // used to display row of numbers
                        k = new JTextField(columnlabels[j]);
                        k.setEditable(false);
                        k.setHorizontalAlignment((int) JFrame.CENTER_ALIGNMENT);
                    } else {
                        // used to display column of numbers
                        k = new JTextField();
                        k.setEditable(false);
                    }
                    board.add(k);
                } else if (j == 0) {
                    k = new JTextField(rowlabels[i]);
                    k.setEditable(false);
                    k.setHorizontalAlignment((int) JFrame.CENTER_ALIGNMENT);
                    board.add(k);
                }
            }
        }

        return board;
    }
}

在此处输入图像描述


Given the nature of the GUI, I guess you are after something more like this.鉴于 GUI 的性质,我猜你是在追求类似这样的东西。

import java.awt.*;
import javax.swing.*;

public class Test {
    public static void main(String args[]) {
        JFrame myapp = new JFrame("test");
        myapp.setLayout(new FlowLayout());

        myapp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        myapp.getContentPane().add(Board());

        // very important!
        myapp.pack();

        // do this last.
        myapp.setVisible(true);
    }

    private static JPanel Board() {
        JButton[][] buttons = new JButton [10][10];
        for(int x=0; x<buttons.length; x++) {
            for (int y=0; y<buttons[0].length; y++) {
                JButton temp = new JButton();
                temp.setPreferredSize(new Dimension(30,30));
                buttons[x][y] = temp;
            }
        }

        JPanel board = new JPanel(new GridLayout(11,11));

        String[] rowlabels = {" ","A","B","C","D","E","F","G","H","I","J"};
        String[] columnlabels = {" ","1","2","3","4","5","6","7","8","9","10"};
        JTextField k;

        for (int i = 0; i < 11; i++) {
            for (int j = 0; j < 11; j++) {
                if ((j != 0) && (i != 0)) {
                    //myboard[i-1][j-1].addActionListener(new ButtonHandler());
                    board.add(buttons[i-1][j-1]);
                }
                if (i == 0) {
                    if (j != 0) {
                        // used to display row of numbers
                        k = new JTextField(columnlabels[j]);
                        k.setEditable(false);
                        k.setHorizontalAlignment((int) JFrame.CENTER_ALIGNMENT);
                    } else {
                        // used to display column of numbers
                        k = new JTextField();
                        k.setEditable(false);
                    }
                    board.add(k);
                } else if (j == 0) {
                    k = new JTextField(rowlabels[i]);
                    k.setEditable(false);
                    k.setHorizontalAlignment((int) JFrame.CENTER_ALIGNMENT);
                    board.add(k);
                }
            }
        }

        return board;
    }
}

在此处输入图像描述

JButton[][] buttons = new JButton [10][10];

only creates an array of nulls, you have to initialize the buttons.只创建一个空数组,您必须初始化按钮。 and adding null to panel causes a NPE.并将 null 添加到面板会导致 NPE。

Just tried your code and noticed that you get NPE because you are just creating a buttons array but do not create buttons in it so your buttons array contains null values and when trying to add it in board it throws NPE.刚刚尝试了您的代码,并注意到您得到了 NPE,因为您只是创建了一个按钮数组,但没有在其中创建按钮,因此您的按钮数组包含 null 值,并且在尝试将其添加到板上时会抛出 NPE。

Try something like this after creating array:创建数组后尝试这样的事情:

 private static JPanel Board() {
    JButton[][] buttons = new JButton [10][10];
    for (int i = 0; i < 10; i++) {
        for (int j = 0; j < 10; j++) {
            buttons[i][j] = new JButton();
        }
    }
    JPanel board = new JPanel(new GridLayout(11,11));

    String[] rowlabels = {" ","A","B","C","D","E","F","G","H","I","J"};
    String[] columnlabels = {" ","1","2","3","4","5","6","7","8","9","10"};
    JTextField k;

    // ---- your remaining code.

 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM