我正在创建一个包含 2 个 JTable 的自定义组件,一个作为主数据网格,另一个作为始终可见的摘要栏,我已经提出了这个解决方案,但是调整列大小并没有按应有的方式工作,任何想法我做错了什么?
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.SwingUtilities;
public class FixedRow2Tables extends JFrame
{
private static final long serialVersionUID = 4676303089799270571L;
Object[][] data;
Object[] column;
JTable fixedTable, table;
public FixedRow2Tables()
{
super("Fixed Row Example");
Object[][] mainData = new Object[][] { { "a", "", "", "", "", "" },
{ "", "b", "", "", "", "" }, { "", "", "c", "", "", "" },
{ "", "", "", "d", "", "" }, { "", "", "", "", "e", "" },
{ "", "", "", "", "", "f" } };
Object[][] summaryData = { { "fixed1", "", "", "", "", "" },
{ "fixed2", "", "", "", "", "" } };
column = new Object[] { "A", "B", "C", "D", "E", "F" };
table = new JTable(mainData, column);
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
fixedTable = new JTable(summaryData, column);
fixedTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
fixedTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
fixedTable.setTableHeader(null);
fixedTable.setColumnModel(table.getColumnModel());
JScrollPane scroll = new JScrollPane(table);
scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
scroll.setPreferredSize(new Dimension(400, 100));
getContentPane().add(scroll, BorderLayout.CENTER);
getContentPane().add(fixedTable, BorderLayout.SOUTH);
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
FixedRow2Tables frame = new FixedRow2Tables();
frame.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
frame.pack();
frame.setVisible(true);
}
});
}
}