gpt4 book ai didi

Java Swing JTable 不可见

转载 作者:行者123 更新时间:2023-12-01 10:49:34 25 4
gpt4 key购买 nike

我无法让 JTable 显示在框架中。当我运行该程序时,该窗口打开,但它是空白的。 Swing 新手,我不确定我做错了什么。请帮忙!

import javax.swing.*;
import java.awt.*;

public class MatrixGenerator{
public static void main(String args[]){
JFrame grid = new JFrame("Exercise14_07");
grid.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container view = grid.getContentPane();
int dimension = 10;
int value;
String entry;
String[] columns = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"};
Object[][] matrix = new Object[dimension][dimension];
for(int i = 0; i < dimension; i++){
for(int j = 0; j < dimension; j++){
value = (int)Math.floor(Math.random()*2);
entry = Integer.toString(value);
matrix[i][j] = entry;
}
}

JTable randomMatrix = new JTable(matrix, columns);
JScrollPane scrollpane = new JScrollPane(randomMatrix);
randomMatrix.setFillsViewportHeight(true);
randomMatrix.setVisible(true);
grid.setSize(300, 300);
grid.setVisible(true);
}
}

最佳答案

JTable randomMatrix = new JTable(matrix, columns);
JScrollPane scrollpane = new JScrollPane(randomMatrix);

// This is very important
grid.add(scrollpane);

randomMatrix.setFillsViewportHeight(true);
grid.setSize(300, 300);
grid.setVisible(true);

关于Java Swing JTable 不可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33989720/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com