java布局与日历java程序
时间: 2023-11-28 21:05:19 浏览: 169
以下是一个简单的Java布局与日历的Java程序示例,展示了如何使用Java Swing库和Java日期和时间相关的类库,创建一个基本的日历应用程序。这个程序可以让用户选择日期和时间,查看月视图和周视图,添加、编辑和删除事件,以及设置程序的外观和行为参数。请注意,这个示例程序仅供参考,实际上还有很多需要完善的地方。
```java
import java.awt.*;
import java.awt.event.*;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import javax.swing.*;
import javax.swing.border.*;
public class CalendarApp extends JFrame implements ActionListener, ItemListener {
private static final long serialVersionUID = 1L;
private JMenuBar menuBar; // 菜单栏
private JMenu fileMenu, editMenu, viewMenu, helpMenu; // 菜单
private JMenuItem newItem, openItem, saveItem, saveAsItem, exitItem; // 文件菜单项
private JMenuItem undoItem, redoItem, cutItem, copyItem, pasteItem, selectAllItem; // 编辑菜单项
private JCheckBoxMenuItem monthView, weekView; // 查看菜单项
private JMenuItem aboutItem; // 帮助菜单项
private JPanel topPanel, calendarPanel, eventPanel; // 主面板、日历面板和事件面板
private JLabel timeLabel, monthLabel, yearLabel; // 日期时间标签、月份标签和年份标签
private JComboBox<String> monthBox, yearBox; // 月份下拉框和年份下拉框
private JButton prevMonthButton, nextMonthButton, addEventButton, editEventButton, deleteEventButton; // 上个月按钮、下个月按钮、添加事件按钮、编辑事件按钮和删除事件按钮
private JTable eventTable; // 事件列表
private DefaultTableModel eventTableModel; // 事件列表的表格模型
private JScrollPane eventScrollPane; // 事件列表的滚动面板
private JFileChooser fileChooser; // 文件选择器
private JColorChooser colorChooser; // 颜色选择器
private Color themeColor; // 主题颜色
private Font font; // 字体
public CalendarApp() {
setTitle("CalendarApp");
setSize(800, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 初始化菜单
initMenuBar();
// 初始化主面板
initTopPanel();
initCalendarPanel();
initEventPanel();
JPanel mainPanel = new JPanel(new BorderLayout());
mainPanel.add(topPanel, BorderLayout.NORTH);
mainPanel.add(calendarPanel, BorderLayout.CENTER);
mainPanel.add(eventPanel, BorderLayout.EAST);
getContentPane().add(mainPanel);
// 初始化文件选择器
fileChooser = new JFileChooser();
// 初始化颜色选择器
colorChooser = new JColorChooser();
themeColor = Color.BLUE;
colorChooser.setColor(themeColor);
// 初始化字体
font = new Font("Arial", Font.PLAIN, 12);
UIManager.put("Table.font", font);
UIManager.put("TableHeader.font", font);
// 显示窗口
setVisible(true);
}
private void initMenuBar() {
menuBar = new JMenuBar();
setJMenuBar(menuBar);
// 初始化文件菜单
fileMenu = new JMenu("文件");
menuBar.add(fileMenu);
newItem = new JMenuItem("新建");
newItem.addActionListener(this);
fileMenu.add(newItem);
openItem = new JMenuItem("打开");
openItem.addActionListener(this);
fileMenu.add(openItem);
saveItem = new JMenuItem("保存");
saveItem.addActionListener(this);
fileMenu.add(saveItem);
saveAsItem = new JMenuItem("另存为");
saveAsItem.addActionListener(this);
fileMenu.add(saveAsItem);
fileMenu.addSeparator();
exitItem = new JMenuItem("退出");
exitItem.addActionListener(this);
fileMenu.add(exitItem);
// 初始化编辑菜单
editMenu = new JMenu("编辑");
menuBar.add(editMenu);
undoItem = new JMenuItem("撤销");
undoItem.addActionListener(this);
editMenu.add(undoItem);
redoItem = new JMenuItem("重做");
redoItem.addActionListener(this);
editMenu.add(redoItem);
editMenu.addSeparator();
cutItem = new JMenuItem("剪切");
cutItem.addActionListener(this);
editMenu.add(cutItem);
copyItem = new JMenuItem("复制");
copyItem.addActionListener(this);
editMenu.add(copyItem);
pasteItem = new JMenuItem("粘贴");
pasteItem.addActionListener(this);
editMenu.add(pasteItem);
editMenu.addSeparator();
selectAllItem = new JMenuItem("全选");
selectAllItem.addActionListener(this);
editMenu.add(selectAllItem);
// 初始化查看菜单
viewMenu = new JMenu("查看");
menuBar.add(viewMenu);
monthView = new JCheckBoxMenuItem("月视图");
monthView.addActionListener(this);
viewMenu.add(monthView);
weekView = new JCheckBoxMenuItem("周视图");
weekView.addActionListener(this);
viewMenu.add(weekView);
ButtonGroup viewGroup = new ButtonGroup();
viewGroup.add(monthView);
viewGroup.add(weekView);
monthView.setSelected(true);
// 初始化帮助菜单
helpMenu = new JMenu("帮助");
menuBar.add(helpMenu);
aboutItem = new JMenuItem("关于");
aboutItem.addActionListener(this);
helpMenu.add(aboutItem);
}
private void initTopPanel() {
topPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
timeLabel = new JLabel();
updateTimeLabel();
topPanel.add(timeLabel);
topPanel.add(new JSeparator(SwingConstants.VERTICAL));
monthLabel = new JLabel();
yearLabel = new JLabel();
monthBox = new JComboBox<String>(new String[] {"1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月"});
monthBox.setSelectedIndex(Calendar.getInstance().get(Calendar.MONTH));
monthBox.addItemListener(this);
yearBox = new JComboBox<String>(new String[] {"2020", "2021", "2022", "2023", "2024", "2025", "2026", "2027", "2028", "2029", "2030"});
yearBox.setSelectedItem(String.valueOf(Calendar.getInstance().get(Calendar.YEAR)));
yearBox.addItemListener(this);
prevMonthButton = new JButton("<");
prevMonthButton.addActionListener(this);
nextMonthButton = new JButton(">");
nextMonthButton.addActionListener(this);
topPanel.add(prevMonthButton);
topPanel.add(monthLabel);
topPanel.add(monthBox);
topPanel.add(yearLabel);
topPanel.add(yearBox);
topPanel.add(nextMonthButton);
}
private void initCalendarPanel() {
calendarPanel = new JPanel(new BorderLayout());
calendarPanel.setBorder(new LineBorder(Color.GRAY));
calendarPanel.add(getMonthView(), BorderLayout.CENTER);
}
private JPanel getMonthView() {
JPanel monthPanel = new JPanel(new GridLayout(7, 7));
monthPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
String[] daysOfWeek = {"日", "一", "二", "三", "四", "五", "六"};
for (int i = 0; i < daysOfWeek.length; i++) {
JLabel label = new JLabel(daysOfWeek[i], JLabel.CENTER);
label.setForeground(Color.RED);
monthPanel.add(label);
}
Calendar cal = Calendar.getInstance();
cal.set(Calendar.DAY_OF_MONTH, 1);
int firstDayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
int maxDaysOfMonth = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
for (int i = 1; i < firstDayOfWeek; i++) {
monthPanel.add(new JLabel(""));
}
for (int i = 1; i <= maxDaysOfMonth; i++) {
JButton button = new JButton(String.valueOf(i));
button.addActionListener(this);
monthPanel.add(button);
}
int remainingCells = 42 - (maxDaysOfMonth + firstDayOfWeek - 1);
for (int i = 0; i < remainingCells; i++) {
monthPanel.add(new JLabel(""));
}
monthLabel.setText(new SimpleDateFormat("MMMM").format(cal.getTime()));
yearLabel.setText(String.valueOf(cal.get(Calendar.YEAR)));
return monthPanel;
}
private JPanel getWeekView() {
JPanel weekPanel = new JPanel(new GridLayout(2, 1, 5, 5));
weekPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
JPanel dayPanel = new JPanel(new GridLayout(1, 7));
dayPanel.setBorder(BorderFactory.createLineBorder(Color.GRAY));
String[] daysOfWeek = {"日", "一", "二", "三", "四", "五", "六"};
for (int i = 0; i < daysOfWeek.length; i++) {
JLabel label = new JLabel(daysOfWeek[i], JLabel.CENTER);
label.setForeground(Color.RED);
dayPanel.add(label);
}
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
prevMonthButton = new JButton("<");
prevMonthButton.addActionListener(this);
nextMonthButton = new JButton(">");
nextMonthButton.addActionListener(this);
buttonPanel.add(prevMonthButton);
buttonPanel.add(monthLabel);
buttonPanel.add(monthBox);
buttonPanel.add(yearLabel);
buttonPanel.add(yearBox);
buttonPanel.add(nextMonthButton);
weekPanel.add(buttonPanel);
weekPanel.add(dayPanel);
return weekPanel;
}
private void initEventPanel() {
eventPanel = new JPanel(new BorderLayout());
eventPanel.setBorder(new TitledBorder("事件列表"));
eventTableModel = new DefaultTableModel(new String[] {"名称", "日期时间"}, 0);
eventTable = new JTable(eventTableModel);
eventTable.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
eventTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
eventScrollPane = new JScrollPane(eventTable);
eventPanel.add(eventScrollPane, BorderLayout.CENTER);
JPanel buttonPanel = new JPanel(new GridLayout(3, 1, 5, 5));
addEventButton = new JButton("添加事件");
addEventButton.addActionListener(this);
editEventButton = new JButton("编辑事件");
editEventButton.addActionListener(this);
deleteEventButton = new JButton("删除事件");
deleteEventButton.addActionListener(this);
buttonPanel.add(addEventButton);
buttonPanel.add(editEventButton);
buttonPanel.add(deleteEventButton);
eventPanel.add(buttonPanel, BorderLayout.SOUTH);
}
private void updateTimeLabel() {
timeLabel.setText(new SimpleDateFormat("yyyy年MM月dd日 EEEE HH:mm:ss").format(Calendar.getInstance().getTime()));
}
private void updateEventTable() {
eventTableModel.setRowCount(0);
// TODO: 从数据库或文件中读取事件数据,添加到表格中
}
@Override
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if (source == newItem) {
// TODO: 新建文件
} else if (source == openItem) {
// TODO: 打开文件
} else if (source == saveItem) {
// TODO: 保存文件
} else if (source == saveAsItem) {
// TODO: 另存
阅读全文