用Swing制作一个简单的万年历。代码如下:
//coding starts here import java.awt.EventQueue; import java.time.DayOfWeek; import java.time.LocalDate; import java.time.temporal.ChronoUnit; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JTextArea; import javax.swing.JTextField; import javax.swing.border.EmptyBorder; import java.awt.Font; import java.awt.Color; /** * 主类。 * @author Hippo */ public class Hippo extends JFrame { private static final long serialVersionUID = -3914982446802151915L; private JPanel contentPane; private JTextField yearField; private static int year; private JLabel prompt1; private final JLabel prompt2 = new JLabel("\u8BF7\u8F93\u51651900\u52309999\u4E4B\u95F4\uFF08\u5305\u62EC\u8FB9\u754C\uFF09\u7684\u5E74\u4EFD\u3002\u975E\u6CD5\u5E74\u4EFD\u5C06\u76F4\u63A5\u5173\u95ED\u7A0B\u5E8F\u3002"); public static void main(String[] args) { EventQueue.invokeLater(() -> { Hippo frame = new Hippo(); frame.setVisible(true); }); } /** * 新建画布。 */ public Hippo() { setResizable(false); setTitle("Hippo Calendar"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLocationRelativeTo(null); setBounds(100, 100, 694, 618); contentPane = new JPanel(); contentPane.setBackground(Color.WHITE); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); contentPane.setLayout(null); JLabel lblYear = new JLabel("Year: "); lblYear.setBounds(10, 10, 54, 15); contentPane.add(lblYear); yearField = new JTextField(); yearField.setBounds(46, 7, 66, 21); contentPane.add(yearField); yearField.setColumns(10); prompt1 = new JLabel(" I need a year..."); prompt1.setFont(new Font("Calibri", Font.PLAIN, 14)); prompt1.setBounds(286, 35, 97, 15); contentPane.add(prompt1); JTextArea textArea = new JTextArea(); textArea.setEditable(false); textArea.setFont(new Font("Courier New", Font.PLAIN, 15)); textArea.setLineWrap(true); textArea.setBounds(10, 57, 213, 522); contentPane.add(textArea); textArea.setText(title()); JTextArea textArea_1 = new JTextArea(); textArea_1.setEditable(false); textArea_1.setText(title()); textArea_1.setLineWrap(true); textArea_1.setFont(new Font("Courier New", Font.PLAIN, 15)); textArea_1.setBounds(246, 56, 213, 523); contentPane.add(textArea_1); JTextArea textArea_2 = new JTextArea(); textArea_2.setEditable(false); textArea_2.setText(title()); textArea_2.setLineWrap(true); textArea_2.setFont(new Font("Courier New", Font.PLAIN, 15)); textArea_2.setBounds(479, 56, 213, 523); contentPane.add(textArea_2); prompt2.setFont(new Font("宋体", Font.PLAIN, 13)); prompt2.setBounds(228, 7, 437, 21); contentPane.add(prompt2); JButton OKButton = new JButton("OK"); OKButton.setBackground(Color.WHITE); OKButton.addActionListener(event -> { try { year = Integer.parseInt(yearField.getText()); if(year < 1900 || year >= 10000) throw new Exception(); } catch(Exception e) { JOptionPane.showMessageDialog(null, "Invalid year. Shutting down now.", "Error", JOptionPane.ERROR_MESSAGE); e.printStackTrace(); System.exit(0); } LocalDate ld = LocalDate.of(year, 1, 1); prompt1.setText("Calendar of " + year); prompt2.setText(year + (ld.isLeapYear() ? "" : "不") +"是一个闰年。"); textArea.setText(title()); textArea_1.setText(title()); textArea_2.setText(title()); textArea.append(getMonthCalendar(1) + getMonthCalendar(4) + getMonthCalendar(7) + getMonthCalendar(10)); textArea_1.append(getMonthCalendar(2) + getMonthCalendar(5) + getMonthCalendar(8) + getMonthCalendar(11)); textArea_2.append(getMonthCalendar(3) + getMonthCalendar(6) + getMonthCalendar(9) + getMonthCalendar(12)); }); OKButton.setBounds(130, 6, 82, 23); contentPane.add(OKButton); } public static String getMonthCalendar(int month) { StringBuilder sb = new StringBuilder(); LocalDate ld = LocalDate.of(year, month, 1); LocalDate aMonthLater = ld.plusMonths(1); int daysInMonth = (int) ld.until(aMonthLater, ChronoUnit.DAYS); int currentRows = 0; boolean newLined = true; for(int day = 1; day <= daysInMonth; day++, ld = ld.plusDays(1)) { DayOfWeek currentWeek = ld.getDayOfWeek(); if(newLined && currentWeek != DayOfWeek.SUNDAY) { for(int j = 0; j < 3 * currentWeek.getValue(); j++) sb.append(" "); newLined = false; } else if(day == 1) newLined = false; sb.append((day < 10 ? "0" : "") + day + " "); if(ld.getDayOfWeek() == DayOfWeek.SATURDAY) { sb.append("\r\n"); currentRows++; } } if(currentRows == 4) sb.append("\r\n\r\n\r\n"); else sb.append("\r\n\r\n"); return sb.toString(); } public static String title() { return "Su Mo Tu We Th Fr Sa\r\n"; } } 在进阶的路上,欢迎各位大侠指正。