这里提供一个Java JTable 实现日历的示例的完整攻略:
1. 实现一个基本的日历
步骤一:创建一个 JFrame,并添加一个 JTable,用来显示日历
public class Calendar extends JFrame {
private final int WIDTH = 600;
private final int HEIGHT = 400;
// 创建一个表格,用来显示日历
private JTable calendarTable = new JTable(6, 7);
private JScrollPane scrollPane = new JScrollPane(calendarTable);
public Calendar() {
setTitle("Calendar");
setSize(WIDTH, HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
// 添加表格到窗口中
add(scrollPane, BorderLayout.CENTER);
setVisible(true);
}
public static void main(String[] args) {
new Calendar();
}
}
步骤二:用数据填充表格,显示日历
public class Calendar extends JFrame {
private final int WIDTH = 600;
private final int HEIGHT = 400;
// 创建一个表格,用来显示日历
private JTable calendarTable = new JTable(6, 7);
private JScrollPane scrollPane = new JScrollPane(calendarTable);
public Calendar() {
setTitle("Calendar");
setSize(WIDTH, HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
// 添加表格到窗口中
add(scrollPane, BorderLayout.CENTER);
// 获取当前月份和年份,并计算出当前月份的天数
Calendar calendar = Calendar.getInstance();
int currentMonth = calendar.get(Calendar.MONTH);
int currentYear = calendar.get(Calendar.YEAR);
int daysInMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
int firstDayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
// 将天数填充到表格中
int row = 0;
int column = firstDayOfWeek - 1;
for (int day = 1; day <= daysInMonth; day++) {
calendarTable.setValueAt(day, row, column);
column++;
if (column > 6) {
column = 0;
row++;
}
}
setVisible(true);
}
public static void main(String[] args) {
new Calendar();
}
}
2. 实现一个带有数据修改的日历
步骤一:创建 TableModel,并添加到 JTable 中
public class CalendarTableModel extends AbstractTableModel {
private static final String[] columnNames = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
private static final int ROW_COUNT = 6;
private static final int COLUMN_COUNT = 7;
private int[][] data;
public CalendarTableModel() {
data = new int[ROW_COUNT][COLUMN_COUNT];
}
public void setData(int[][] data) {
this.data = data;
fireTableDataChanged();
}
@Override
public int getRowCount() {
return ROW_COUNT;
}
@Override
public int getColumnCount() {
return COLUMN_COUNT;
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
return data[rowIndex][columnIndex];
}
@Override
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
if (aValue instanceof Integer) {
data[rowIndex][columnIndex] = (Integer) aValue;
fireTableCellUpdated(rowIndex, columnIndex);
}
}
@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
return true;
}
@Override
public String getColumnName(int column) {
return columnNames[column];
}
}
public class ModifiedCalendar extends JFrame {
private final int WIDTH = 600;
private final int HEIGHT = 400;
// 创建一个表格,用来显示日历
private JTable calendarTable;
private JScrollPane scrollPane;
public ModifiedCalendar() {
setTitle("Calendar");
setSize(WIDTH, HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
// 初始化表格数据
int[][] data = new int[6][7];
Calendar calendar = Calendar.getInstance();
int currentMonth = calendar.get(Calendar.MONTH);
int currentYear = calendar.get(Calendar.YEAR);
int daysInMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
int firstDayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
int row = 0;
int column = firstDayOfWeek - 1;
for (int day = 1; day <= daysInMonth; day++) {
data[row][column] = day;
column++;
if (column > 6) {
column = 0;
row++;
}
}
// 创建 TableModel
CalendarTableModel model = new CalendarTableModel();
model.setData(data);
// 添加 TableModel 到 JTable 中
calendarTable = new JTable(model);
scrollPane = new JScrollPane(calendarTable);
add(scrollPane, BorderLayout.CENTER);
setVisible(true);
}
public static void main(String[] args) {
new ModifiedCalendar();
}
}
步骤二:实现数据修改,并更新表格
public class ModifiedCalendar extends JFrame {
private final int WIDTH = 600;
private final int HEIGHT = 400;
// 创建一个表格,用来显示日历
private JTable calendarTable;
private JScrollPane scrollPane;
// 初始化表格数据
private int[][] data = new int[6][7];
Calendar calendar = Calendar.getInstance();
int currentMonth = calendar.get(Calendar.MONTH);
int currentYear = calendar.get(Calendar.YEAR);
int daysInMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
int firstDayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
int row = 0;
int column = firstDayOfWeek - 1;
for (int day = 1; day <= daysInMonth; day++) {
data[row][column] = day;
column++;
if (column > 6) {
column = 0;
row++;
}
}
public ModifiedCalendar() {
setTitle("Calendar");
setSize(WIDTH, HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
// 创建 TableModel
CalendarTableModel model = new CalendarTableModel();
model.setData(data);
// 添加 TableModel 到 JTable 中
calendarTable = new JTable(model);
scrollPane = new JScrollPane(calendarTable);
add(scrollPane, BorderLayout.CENTER);
// 监听数据修改事件,并更新表格
model.addTableModelListener(new TableModelListener() {
@Override
public void tableChanged(TableModelEvent e) {
int row = e.getFirstRow();
int column = e.getColumn();
TableModel model = (TableModel) e.getSource();
int value = (Integer) model.getValueAt(row, column);
System.out.println("row: " + row + ", column: " + column + ", value: " + value);
}
});
setVisible(true);
}
public static void main(String[] args) {
new ModifiedCalendar();
}
}
以上是Java JTable 实现日历的两个示例攻略,希望对您有所帮助。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:Java JTable 实现日历的示例 - Python技术站