中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

java怎么實現萬年歷圖形界面

小億
112
2023-08-10 06:00:50
欄目: 編程語言

要實現一個萬年歷的圖形界面,可以使用Java的Swing庫來完成。以下是一個簡單的示例代碼:

import javax.swing.*;
import java.awt.*;
import java.time.LocalDate;
public class CalendarGUI {
private JFrame frame;
private JPanel panel;
private JLabel monthLabel;
private JLabel yearLabel;
private JButton prevButton;
private JButton nextButton;
private JTable calendarTable;
private LocalDate currentDate;
public CalendarGUI() {
frame = new JFrame("萬年歷");
panel = new JPanel();
monthLabel = new JLabel();
yearLabel = new JLabel();
prevButton = new JButton("上個月");
nextButton = new JButton("下個月");
calendarTable = new JTable(6, 7);
currentDate = LocalDate.now();
createGUI();
updateCalendar();
}
private void createGUI() {
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 300);
frame.setLayout(new BorderLayout());
panel.setLayout(new BorderLayout());
panel.add(monthLabel, BorderLayout.WEST);
panel.add(yearLabel, BorderLayout.CENTER);
panel.add(prevButton, BorderLayout.LINE_START);
panel.add(nextButton, BorderLayout.LINE_END);
frame.add(panel, BorderLayout.NORTH);
frame.add(new JScrollPane(calendarTable), BorderLayout.CENTER);
prevButton.addActionListener(e -> {
currentDate = currentDate.minusMonths(1);
updateCalendar();
});
nextButton.addActionListener(e -> {
currentDate = currentDate.plusMonths(1);
updateCalendar();
});
frame.setVisible(true);
}
private void updateCalendar() {
monthLabel.setText(currentDate.getMonth().toString());
yearLabel.setText(String.valueOf(currentDate.getYear()));
LocalDate firstDayOfMonth = LocalDate.of(currentDate.getYear(), currentDate.getMonth(), 1);
int firstDayOfWeek = firstDayOfMonth.getDayOfWeek().getValue();
int daysInMonth = currentDate.lengthOfMonth();
for (int i = 0; i < calendarTable.getRowCount(); i++) {
for (int j = 0; j < calendarTable.getColumnCount(); j++) {
int day = i * calendarTable.getColumnCount() + j + 1 - firstDayOfWeek;
if (day <= 0 || day > daysInMonth) {
calendarTable.setValueAt("", i, j);
} else {
calendarTable.setValueAt(day, i, j);
}
}
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(CalendarGUI::new);
}
}

這個示例代碼使用了一個JFrame作為窗口容器,其中包含一個JPanel作為標題欄容器,一個JTable用于顯示日歷表格。通過按鈕的點擊事件,可以切換到上個月或下個月的日歷。使用LocalDate類來獲取當前日期和計算上下個月的日期。

0
磐石市| 岳普湖县| 南丰县| 奉新县| 隆德县| 留坝县| 阿城市| 万全县| 灌阳县| 漠河县| 吉首市| 平乡县| 郎溪县| 长武县| 东海县| 政和县| 湄潭县| 财经| 诸暨市| 遂溪县| 巩留县| 尚义县| 抚州市| 嫩江县| 法库县| 锡林浩特市| 云南省| 六盘水市| 监利县| 苍南县| 禄劝| 东莞市| 晋城| 武功县| 龙江县| 卓尼县| 文成县| 南部县| 侯马市| 兰考县| 筠连县|