我正在尝试将三维矩阵中的数据拟合到多个 JTable 中。我正在使用由多个面板组成的布局,这些面板以某种方式为我提供目标布局(我对 GridBagLayout 不太熟悉,所以我将自己的布局放在一起)。现在的问题是表格没有显示在框架上,我不知道问题是多个面板还是因为我使用的是 3 维矩阵。在不使用表、层等字段时使用 JTable 有效 - 我假设某事。那里一定会出错。我非常感谢您的帮助和提示!非常感谢!
这是我的代码:
import java.awt.*;
import javax.swing.*;
import javax.swing.JTable;
public class Tabellen extends JFrame{
private static final long serialVersionUID = 7526472295622776147L;
Container c;
JPanel p_tabellen;
JPanel[] p_tab;
JTable[] table;
String[] columnnames={};
String[][][] matrixStr;
double[][][] matrix;
public Tabellen(double [][][] matrix) {
//create a matrix of Strings from a double-matrix that can be read by the JTable constructor
this.matrix=matrix;
matrixStr = new String[matrix.length][matrix[0].length][matrix[0][0].length];
for (int dim=0; dim<matrix.length; dim++){
for (int zeile=0; zeile<matrix[0].length; zeile++){
for (int spalte=0; spalte<matrix[0][0].length; spalte++){
matrixStr[dim][zeile][spalte]= String.valueOf(matrix[dim][zeile][spalte]);
}
}
}
//create panels and Layouts
c = getContentPane();
p_tabellen= new JPanel(new GridLayout(matrix.length,1));
p_tab= new JPanel[matrix.length];
for (int p=0; p<matrix.length; p++){
p_tab[p]= new JPanel(new BorderLayout());
p_tabellen.add(p_tab[p]);
}
c.add(p_tabellen);
//create one table per panel
table = new JTable[matrix.length];
for (int dim=0; dim<matrix.length; dim++){
for (int zeile=0; zeile<matrix[0].length; zeile++){
for (int spalte=0; spalte<matrix[0][0].length; spalte++){
table[dim]= new JTable(matrixStr[dim],columnnames);
p_tab[dim].add(table[dim], BorderLayout.CENTER);
}
}
}
}
}
输入示例:
public class TEST {
public static void main(String[] args) {
double [][][] matrix = {{{2,4,6},{7,8,9}},{{1,2,3},{3,4,8}},{{1,2,4},{5,7,9}},{{2,4,6},{7,8,9}},{{1,2,3},{3,4,8}},{{1,2,4},{5,7,9}},{{2,4,6},{7,8,9}},{{1,2,3},{3,4,8}},{{1,2,4},{5,7,9}},{{2,4,6},{7,8,9}},{{1,2,3},{3,4,8}},{{1,2,4},{5,7,9}},{{2,4,6},{7,8,9}},{{1,2,3},{3,4,8}},{{1,2,4},{5,7,9}}};
Tabellen d= new Tabellen(matrix);
d.setTitle("test");
d.setSize(1300,720);
d.setVisible(true);
d.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}