亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

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
曲周县| 麻城市| 徐州市| 崇阳县| 东方市| 肇庆市| 巴彦县| 江川县| 南靖县| 九台市| 芦山县| 游戏| 丹江口市| 临海市| 白玉县| 宿松县| 察雅县| 都兰县| 太仓市| 佳木斯市| 札达县| 大城县| 博爱县| 平舆县| 荔波县| 贵港市| 西吉县| 华蓥市| 西林县| 安徽省| 南丰县| 平阳县| 酒泉市| 于田县| 乌拉特后旗| 汾阳市| 台中县| 桃源县| 三门县| 安仁县| 临洮县|