我正在使用GridBagLayout来定位GUI组件。我在JFrame中使用了Jtable。但桌子不适合框架。它看起来如下。当我拖动框架时,它会显示表格。
这是我的代码。我把它用作弹出Jframe的按钮列表器。
public class ViewPrescriptionHistory extends JFrame{
private GridBagConstraints gridbBagConstraints;
private JTable drugDetailsTable;
HistroyTableModel drugDetailsTabelModel = new HistroyTableModel();
public ViewPrescriptionHistory() {
this.setLayout(new GridBagLayout());
this.setSize(500, 500);
this.pack();
initialize();
}
private void initialize() {
gridbBagConstraints = new GridBagConstraints();
gridbBagConstraints.insets = new Insets(10, 0, 10, 0);
gridbBagConstraints.ipady = 10;
gridbBagConstraints.ipadx = 10;
drugDetailsTable = new JTable(drugDetailsTabelModel);
drugDetailsTable.setPreferredScrollableViewportSize(new Dimension(600, 100));
drugDetailsTable.setFillsViewportHeight(true);
JScrollPane scrollPane = new JScrollPane(drugDetailsTable);
scrollPane.setBounds(5, 218, 884, 194);
this.add(scrollPane);
}
}
Here is my table model. I used Abstract Table model.
import java.util.ArrayList;
import java.util.List;
import javax.swing.table.AbstractTableModel;
import assignment3.prescriptionsystem.model.HistoryModel;
public class HistroyTableModel extends AbstractTableModel{
private String[] columnNames = { "Doctor Name", "Date", "Drug Name", "Form" , "Dosage" };
List<HistoryModel> historyList = new ArrayList<HistoryModel>();
public String getColumnName(int column) {
return columnNames[column];
}
public int getColumnCount() {
return columnNames.length;
}
public int getRowCount() {
return historyList.size();
}
public void reloadDrugList(List<HistoryModel> drugsData) {
historyList= drugsData;
fireTableDataChanged();
}
public Object getValueAt(int rowIndex, int columnIndex) {
HistoryModel drugData = historyList.get(rowIndex);
switch (columnIndex) {
case 0:
return drugData.getDocName();
case 1:
return drugData.getDate();
case 2:
return drugData.getDrugName();
case 3:
return drugData.getFrom();
case 4:
return drugData.getDosage();
default:
return null;
}
}
}
这是我的模型类
public class HistoryModel {
String docName;
String date;
String drugName;
String dosage;
String from;
public String getDocName() {
return docName;
}
public void setDocName(String docName) {
this.docName = docName;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getDrugName() {
return drugName;
}
public void setDrugName(String drugName) {
this.drugName = drugName;
}
public String getDosage() {
return dosage;
}
public void setDosage(String dosage) {
this.dosage = dosage;
}
public String getFrom() {
return from;
}
public void setFrom(String from) {
this.from = from;
}
}
如何解决此问题?
答案 0 :(得分:1)
首先,您应该在JFrame的内容窗格上添加组件并设置GridBagLayout,而不是JFrame本身。请考虑从Oracle教程中获取的包含层次结构的图像:
您还应该在 之后添加您的组件时调用pack()
,之前:
pack方法调整框架的大小,使其所有内容都达到或超过其首选大小。 pack的替代方法是通过调用setSize或setBounds(也设置帧位置)显式建立帧大小。通常,使用pack比调用setSize更好,因为pack离开框架布局管理器负责框架大小,布局管理器善于调整平台依赖性和影响组件大小的其他因素。
由于pack()
处理JFrame的大小调整,因此您无需显式调用setSize()
所以你的代码应该是这样的:
getContentPane().setLayout(new GridBagLayout()); // Set the layout on the content pane rather than the JFrame
gridbBagConstraints = new GridBagConstraints();
gridbBagConstraints.insets = new Insets(10, 0, 10, 0);
gridbBagConstraints.ipady = 10;
gridbBagConstraints.ipadx = 10;
drugDetailsTable = new JTable(drugDetailsTabelModel);
drugDetailsTable.setPreferredScrollableViewportSize(new Dimension(600, 100));
drugDetailsTable.setFillsViewportHeight(true);
JScrollPane scrollPane = new JScrollPane(drugDetailsTable);
scrollPane.setBounds(5, 218, 884, 194);
getContentPane().add(scrollPane); // Add the scroll pane to the content pane rather than the JFrame
pack(); // Call pack at the end
生成的GUI看起来像: