In my JPanel I am using tablelayout.jar Oracle library (have a look here) and so, generally, I have to do the following:
在我的JPanel中,我使用的是tablelayout.jar Oracle库(看看这里),所以,一般来说,我必须做以下事情:
private double[][] size = {
{30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30},
{30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30}
};
JPanel p = new JPanel();
p.setLayout(new TableLayout(size));
where "30" is the dimension respectively for cos and rows. In this case we wanted square cells. So I can do, for example,:
其中“30”分别是cos和行的维度。在这种情况下,我们想要方形单元格所以我可以做,例如:
p.add(new JButton(), "1,4" /*"col,row"*/);
We thought that declaring that "size" matrix like that was not good to do and so we changed the initialization like the following:
我们认为声明像这样的“大小”矩阵并不好,所以我们改变了初始化,如下所示:
size = new double[Constants.GUI_ROWS][Constants.GUI_COLS];
for (int i=0; i<Constants.GUI_COLS-1; i++)
for (int j=0; j<Constants.GUI_ROWS-1; j++)
size[i][j] = 30;
where
Constants.GUI_COLS = 19 ({30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30})
and
Constants.GUI_ROWS = 17 ({30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30})
but this does not work. When we try to add something to the JPanel then nothing is shown. It works only if we write the first initialization by hand. Why this?
但这不起作用。当我们尝试向JPanel添加内容时,则不会显示任何内容。它只有在我们手动编写第一个初始化时才有效。为什么这个?
2 个解决方案
#1
3
To achieve the same as you did by hand you can use
要实现与手工操作相同的功能,您可以使用
double size[][];
size = new double[2][];
size[0]=new double[19];
size[1]=new double[17];
for (int i=0; i<19; i++)
size[0][i] = 30;
for (int i=0; i<17; i++)
size[1][i] = 30;
#2
1
You got the loop conditions off by one. Should be :
你有一个循环条件。应该 :
for (int i=0; i<Constants.GUI_COLS; i++)
for (int j=0; j<Constants.GUI_ROWS; j++)
size[i][j] = 30;
Note that if this call
注意,如果这个电话
p.setLayout(new TableLayout(size));
comes before this call :
在此电话会议之前:
size = new double[Constants.GUI_ROWS][Constants.GUI_COLS];
The old array referred by size
will be used by the TableLayout
.
TableLayout将使用由size引用的旧数组。
#1
3
To achieve the same as you did by hand you can use
要实现与手工操作相同的功能,您可以使用
double size[][];
size = new double[2][];
size[0]=new double[19];
size[1]=new double[17];
for (int i=0; i<19; i++)
size[0][i] = 30;
for (int i=0; i<17; i++)
size[1][i] = 30;
#2
1
You got the loop conditions off by one. Should be :
你有一个循环条件。应该 :
for (int i=0; i<Constants.GUI_COLS; i++)
for (int j=0; j<Constants.GUI_ROWS; j++)
size[i][j] = 30;
Note that if this call
注意,如果这个电话
p.setLayout(new TableLayout(size));
comes before this call :
在此电话会议之前:
size = new double[Constants.GUI_ROWS][Constants.GUI_COLS];
The old array referred by size
will be used by the TableLayout
.
TableLayout将使用由size引用的旧数组。