91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

怎么用Java寫一個帶有窗體的日歷

小億
99
2024-01-13 02:16:50
欄目: 編程語言

要用Java寫一個帶有窗體的日歷,可以使用Java的Swing庫來創建窗體和組件。

下面是一個簡單的示例代碼,展示了如何使用Java編寫一個帶有窗體的日歷:

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.time.LocalDate;
import java.time.YearMonth;

public class CalendarApp extends JFrame {
    private YearMonth currentYearMonth;
    private JLabel monthLabel;
    private JButton prevButton;
    private JButton nextButton;
    private JTable calendarTable;

    public CalendarApp() {
        currentYearMonth = YearMonth.now();

        setTitle("Calendar App");
        setSize(400, 300);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);

        monthLabel = new JLabel("", JLabel.CENTER);
        calendarTable = new JTable(6, 7);

        prevButton = new JButton("Prev");
        prevButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                currentYearMonth = currentYearMonth.minusMonths(1);
                updateCalendar();
            }
        });

        nextButton = new JButton("Next");
        nextButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                currentYearMonth = currentYearMonth.plusMonths(1);
                updateCalendar();
            }
        });

        updateCalendar();

        setLayout(new BorderLayout());
        add(monthLabel, BorderLayout.NORTH);
        add(new JScrollPane(calendarTable), BorderLayout.CENTER);

        JPanel buttonPanel = new JPanel();
        buttonPanel.add(prevButton);
        buttonPanel.add(nextButton);
        add(buttonPanel, BorderLayout.SOUTH);
    }

    private void updateCalendar() {
        monthLabel.setText(currentYearMonth.getYear() + "年" + currentYearMonth.getMonthValue() + "月");

        LocalDate firstDayOfMonth = currentYearMonth.atDay(1);
        int firstDayOfWeek = firstDayOfMonth.getDayOfWeek().getValue();

        int daysInMonth = currentYearMonth.lengthOfMonth();
        int numRows = (int) Math.ceil((firstDayOfWeek + daysInMonth) / 7.0);

        String[] columnNames = {"周日", "周一", "周二", "周三", "周四", "周五", "周六"};
        Object[][] data = new Object[numRows][7];

        int day = 1 - firstDayOfWeek;
        for (int i = 0; i < numRows; i++) {
            for (int j = 0; j < 7; j++) {
                if (day > 0 && day <= daysInMonth) {
                    data[i][j] = day;
                } else {
                    data[i][j] = "";
                }
                day++;
            }
        }

        calendarTable.setModel(new javax.swing.table.DefaultTableModel(data, columnNames));
        calendarTable.setDefaultRenderer(calendarTable.getColumnClass(0), new CalendarCellRenderer());
    }

    private class CalendarCellRenderer extends DefaultTableCellRenderer {
        @Override
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
            JLabel label = (JLabel) super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
            if (value != null) {
                int day = (int) value;
                if (day == LocalDate.now().getDayOfMonth() && currentYearMonth.equals(YearMonth.now())) {
                    label.setForeground(Color.RED);
                } else {
                    label.setForeground(table.getForeground());
                }
            }
            return label;
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new CalendarApp().setVisible(true);
            }
        });
    }
}

這個示例代碼創建了一個帶有窗體的日歷應用,可以通過Prev和Next按鈕切換月份。日歷表格中,當前日期會以紅色顯示。

在這個示例代碼中,我們使用了Swing的JFrame、JLabel、JButton、JTable等組件來構建窗體和日歷表格。updateCalendar()方法用于更新日歷表格的內容。CalendarCellRenderer類用于自定義單元格的渲染,以便高亮顯示當前日期。

通過運行main()方法,可以啟動日歷應用并顯示窗體。

0
乌鲁木齐县| 竹北市| 永善县| 楚雄市| 宁波市| 望城县| 五寨县| 浦东新区| 昭平县| 绥中县| 凭祥市| 嘉黎县| 云阳县| 亳州市| 康保县| 清原| 隆回县| 巴中市| 天祝| 介休市| 通化市| 乐昌市| 大化| 同江市| 绵竹市| 临清市| 德江县| 巴塘县| 葵青区| 七台河市| 芦溪县| 仁布县| 阿勒泰市| 鸡泽县| 乐都县| 河北区| 尉犁县| 甘肃省| 静海县| 尼木县| 泸州市|