【发布时间】:2018-01-21 21:00:42
【问题描述】:
因此,对于学校项目,我必须在单独的类中将 JTable 添加到 JPanel 而不修改后者。这是JPanel所在的类。
public class ThisSwingView extends SwingView implements ThisTCRMView {
public ThisSwingView() {
super();
setTitle("This");
JScrollPane centerScrollPane = new JScrollPane();
centerScrollPane.setViewportBorder(new LineBorder(new Color(0, 0, 0)));
centerScrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
centerScrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
setCenterPanel(centerScrollPane);
JPanel centerGrid = new JPanel();
centerScrollPane.setViewportView(centerGrid);
GridBagLayout gbl_centerGrid = new GridBagLayout();
gbl_centerGrid.columnWidths = new int[]{100, 475, 0};
gbl_centerGrid.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
gbl_centerGrid.columnWeights = new double[]{0.0, 1.0, Double.MIN_VALUE};
gbl_centerGrid.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
centerGrid.setLayout(gbl_centerGrid);}}
这是我可以修改的类:
public class NewThisSwingView extends ThisSwingView implements ThisTCRMSales{
public NewThisSwingView(){
super();
JPanel centerGrid = new JPanel();
JTable table;
Object rowData[][] = { { "Row1-Column1", "Row1-Column2", "Row1-Column3" },
{ "Row2-Column1", "Row2-Column2", "Row2-Column3" } };
Object columnNames[] = { "Column One", "Column Two", "Column Three" };
table = new JTable(rowData, columnNames);
GridBagConstraints gbc_Table = new GridBagConstraints();
gbc_contactsTable.insets = new Insets(0, 0, 5, 0);
gbc_contactsTable.fill = GridBagConstraints.HORIZONTAL;
gbc_contactsTable.gridx = 1;
gbc_contactsTable.gridy = 12;
centerGrid.add(table, gbc_Table);
}}
但是,当我这样做时,我的表格不会打印在屏幕上。我已经修改了主类来运行我正在修改的类,但它不起作用。 提前致谢。
【问题讨论】:
-
centerGrid从未添加到NewThisSwingView... 但我看不到您正在使用NewThisSwingView的任何地方,因此,它也永远不会添加到任何东西中。这引发了一系列新问题——比如为什么你不将JTable包装在它自己的JScrollPane中? -
如需尽快获得更好的帮助,请发帖minimal reproducible example 或Short, Self Contained, Correct Example。
标签: java swing jtable layout-manager gridbaglayout