簡體   English   中英

Java Swing 幫助

[英]Java Swing Help

我正在嘗試創建一個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 對我來說有點新,所以任何幫助將不勝感激。

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

在此處輸入圖像描述

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

只創建一個空數組,您必須初始化按鈕。 並將 null 添加到面板會導致 NPE。

剛剛嘗試了您的代碼,並注意到您得到了 NPE,因為您只是創建了一個按鈕數組,但沒有在其中創建按鈕,因此您的按鈕數組包含 null 值,並且在嘗試將其添加到板上時會拋出 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.

 }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM