gpt4 book ai didi

Java Swing 表问题

转载 作者:行者123 更新时间:2023-11-30 05:13:00 25 4
gpt4 key购买 nike

大家好,正在制作 Activity 日历。我在显示列标题时遇到一些问题..这是代码

private JTable calendarTable;
private DefaultTableModel calendarTableModel;

final private String [] days = {"Sunday", "Monday", "Tuesday",
"Wednesday", "Thursday", "Friday",
"Saturday"};
//////////////////////////////////////////////////////////////////////
/* Setup the actual calendar table */


calendarTableModel = new DefaultTableModel() {
public boolean isCellEditable(int row, int col){
return false;
}
};

// setup columns
for(int i = 0; i < 7; i++)
calendarTableModel.addColumn(days[i]);

calendarTable = new JTable(calendarTableModel);

calendarTable.getTableHeader().setResizingAllowed(false);
calendarTable.getTableHeader().setReorderingAllowed(false);

calendarTable.setColumnSelectionAllowed(true);
calendarTable.setRowSelectionAllowed(true);
calendarTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

calendarTable.setRowHeight(105);
calendarTableModel.setColumnCount(7);
calendarTableModel.setRowCount(6);

另外,我对表格有点陌生。我怎样才能在表格的最大大小之间分割 rowHeight ?

最佳答案

表格标题

要么将calendarTable放入JScrollPane中,要么添加calendarTable.getTableHeader()。

行高

添加一个监听器,以便在调整表格大小时更改行高。

calendarTable.addComponentListener(new ComponentAdapter() {
public void componentResized(ComponentEvent evt) {
if (evt.getID() == ComponentEvent.COMPONENT_RESIZED) {
calendarTable.setRowHeight(calendarTable.getHeight() / calendarTable.getRowCount());
}
}
});

关于Java Swing 表问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2688148/

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