我正在尝试向我的 GUI 添加一个表,但是当我将框架的布局从 GridLayout 更改为 NULL 时,它会从 GUI 中丢失吗?
以下代码用于创建 GUI:
public SalariedEmployeeGUI() {
setLayout(null);
getContentPane().setBackground(Color.WHITE);
JPanel salariedEmpTablePanel = new JPanel();
salariedEmpTablePanel.setLayout(null);
salariedEmpTablePanel.setLocation(60,110);
salariedEmpTablePanel.setSize(550,390);
salariedEmpTablePanel.setBorder(BorderFactory.createTitledBorder(
BorderFactory.createEtchedBorder(EtchedBorder.RAISED,
Color.BLUE, Color.DARK_GRAY), "Salaried Employee Table"));
salariedEmpTablePanel.setBackground(Color.WHITE);
sEmpTable = new JTable();
sEmpTable.setPreferredScrollableViewportSize(new Dimension(550, 390));
JScrollPane scrollPane = new JScrollPane(sEmpTable);
salariedEmpTablePanel.add(sEmpTable);
add(salariedEmpTablePanel);
setupTable();
loadTable();
setupTable 和 loadTable 方法如下.. 但我认为它们工作正常:
private void setupTable() {
payrollQueries = new PayrollQueries();
tableModel = new DefaultTableModel();
tableModel.setColumnCount(5);
tableModel.setColumnIdentifiers(new String[]{"First Name",
"Last Name", "Type", "SSN", "Weekly Salary"});
sEmpTable.setModel(tableModel);
}
private void loadTable() {
entries = payrollQueries.getSalariedEmployee();
int tableRow = 0;
tableModel.setNumRows(entries.size());
System.out.println("number of salaries employees: " + entries.size());
for (SalariedEmployee se : entries) {
tableModel.setValueAt(se.getFirstName(), tableRow, 0);
tableModel.setValueAt(se.getLastName(), tableRow, 1);
tableModel.setValueAt(se.getSocialSecurityNumber(), tableRow, 2);
tableModel.setValueAt(se.geteType(), tableRow, 3);
tableModel.setValueAt(se.getWeeklySalary(), tableRow, 4);
tableRow++;
}
}