Java Swing 帮助
我正在尝试创建一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Swing GUI 应该在 EDT 上构建。这部分留给OP作为练习。
鉴于 GUI 的性质,我猜您正在追求类似这样的东西。
Swing GUIs should be constructed on the EDT. That part is left as an exercise for the OP.
Given the nature of the GUI, I guess you are after something more like this.
仅创建一个空数组,您必须初始化按钮。向面板添加 null 会导致 NPE。
only creates an array of nulls, you have to initialize the buttons. and adding null to panel causes a NPE.
刚刚尝试了您的代码,注意到您得到了 NPE,因为您只是创建了一个按钮数组,但没有在其中创建按钮,因此您的按钮数组包含空值,并且当尝试将其添加到板上时,它会抛出 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.
Try something like this after creating array: