Java Swing 帮助

发布于 2024-11-11 02:28:45 字数 1842 浏览 3 评论 0原文

我正在尝试创建一个 JButtons 网格,索引值位于按钮网格的左侧和右侧。

这是我的代码,但我收到了 NullPointerException。问题是什么?

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;
}
}

我可能正在做一些显而易见的事情,但 Swing 对我来说有点新,所以任何帮助将不胜感激。

I'm trying to create a grid of JButtons, with index values to the left and right of the button grid.

Here is my code, but I'm getting a 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

安静被遗忘 2024-11-18 02:28:45

Swing GUI 应该在 EDT 上构建。这部分留给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;
    }
}

在此处输入图像描述


鉴于 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;
    }
}

在此处输入图像描述

Swing GUIs should be constructed on the EDT. That part is left as an exercise for the 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;
    }
}

enter image description here


Given the nature of the GUI, I guess you are after something more like this.

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;
    }
}

enter image description here

烟酒忠诚 2024-11-18 02:28:45
JButton[][] buttons = new JButton [10][10];

仅创建一个空数组,您必须初始化按钮。向面板添加 null 会导致 NPE。

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.

暗地喜欢 2024-11-18 02:28:45

刚刚尝试了您的代码,注意到您得到了 NPE,因为您只是创建了一个按钮数组,但没有在其中创建按钮,因此您的按钮数组包含空值,并且当尝试将其添加到板上时,它会抛出 NPE。

创建数组后尝试这样的事情:

 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.

 }

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.

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.

 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文