21xrx.com
2024-12-28 13:23:15 Saturday
登录
文章检索 我的文章 写文章
java编写日历代码教程
2023-06-15 16:38:41 深夜i     --     --
Java 日历程序 GUI组件

在Java编程中,编写一个日历程序是一个很有意义的项目。无论是在个人还是商业开发中,这种类型的应用程序都是很有用的。下面是一个简单的Java编写日历程序的教程,让你了解如何在Java中编写一个日历程序。

首先我们需要知道Java中的Date和Calendar类来获取日期和时间。然后可以使用Java GUI组件来显示日历。我们需要一个视窗、标签、按钮来构造整个应用。用户可以在界面上选择年和月,然后单击按钮来显示日历。这个日历应该可以清晰地显示每个月的所有日期,让用户可以方便地浏览。

下面是一个简单的Java代码示例,可以用于实现这个日历程序:


import java.awt.BorderLayout;

import java.awt.Color;

import java.awt.Container;

import java.awt.EventQueue;

import java.awt.Font;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.text.DateFormatSymbols;

import java.util.Calendar;

import java.util.GregorianCalendar;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.SwingConstants;

public class CalendarApp extends JFrame {

  private static final long serialVersionUID = 1L;

  private JLabel monthLabel;

  private JButton[] dayButtons;

  private Calendar cal = new GregorianCalendar();

  

  public CalendarApp() {

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    setTitle("Java日历");

    setLocation(100, 50);

    setSize(700, 450);

    setResizable(false);

        

    JPanel mainPanel = new JPanel();

    mainPanel.setLayout(new BorderLayout(5, 5));

    mainPanel.setBackground(Color.gray);

    

    monthLabel = new JLabel("", SwingConstants.CENTER);

    monthLabel.setFont(new Font("SansSerif", Font.BOLD, 24));

    JPanel buttonPanel = new JPanel();

    buttonPanel.setLayout(new BorderLayout(5, 5));

    

    String[] dayNames = new DateFormatSymbols().getShortWeekdays();

    JPanel dayPanel = new JPanel();

    dayPanel.setBackground(Color.white);

    dayPanel.setLayout(new BorderLayout(0, 0));

    JPanel[] panels = new JPanel[7];

    for (int i = 0; i < 7; i++) {

      panels[i] = new JPanel();

      panels[i].setBackground(Color.white);

      dayPanel.add(panels[i], BorderLayout.CENTER);

      JLabel lab = new JLabel(dayNames[i + 1], SwingConstants.CENTER);

      panels[i].add(lab);

    }

    

    dayButtons = new JButton[42];

    JPanel[] dayPanel2 = new JPanel[6];

    for (int i = 0; i < 6; i++) {

      dayPanel2[i] = new JPanel();

      dayPanel2[i].setBackground(Color.white);

      dayPanel2[i].setLayout(new BorderLayout(0, 0));

      for (int j = 0; j < 7; j++) {

        JButton but = new JButton();

        but.setFont(new Font("Arial", Font.PLAIN, 10));

        but.addActionListener(new ActionListener() {

          public void actionPerformed(ActionEvent ae) {

            JButton but = (JButton) ae.getSource();

            String[] arr = but.getText().split(" ");

            cal.set(Calendar.DAY_OF_MONTH, Integer.parseInt(arr[1]));

            new DayFrame(cal).setVisible(true);

          }

        });

        

        dayButtons[(i * 7) + j] = but;

        JLabel lab = new JLabel();

        lab.setFont(new Font("Arial", Font.PLAIN, 10));

        JPanel pan = new JPanel(new BorderLayout());

        pan.setBackground(Color.white);

        pan.add(lab, BorderLayout.NORTH);

        pan.add(but, BorderLayout.CENTER);

        dayPanel2[i].add(pan, BorderLayout.CENTER);

      }

      buttonPanel.add(dayPanel2[i], BorderLayout.CENTER);

    }

    

    mainPanel.add(monthLabel, BorderLayout.PAGE_START);

    mainPanel.add(buttonPanel, BorderLayout.CENTER);

    mainPanel.add(dayPanel, BorderLayout.PAGE_END);

    

    Container con = getContentPane();

    con.add(mainPanel);

        

    setVisible(true);

  }

  

  private void refreshCalendar() {

    cal.set(Calendar.DAY_OF_MONTH, 1);

    String monthString = cal.getDisplayName(Calendar.MONTH, Calendar.LONG, null);

    monthLabel.setText(monthString + ", " + cal.get(Calendar.YEAR));

    

    int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);

    int daysInMonth = cal.getActualMaximum(Calendar.DAY_OF_MONTH);

    

    for (int i = 0; i < dayButtons.length; i++) {

      dayButtons[i].setText("");

    }

    for (int i = 0; i < daysInMonth; i++) {

      int day = i + 1;

      dayButtons[(dayOfWeek - 1) + i].setText(day + "");

    }

  }

  

  private class DayFrame extends JFrame {

    private static final long serialVersionUID = 1L;

    private Calendar cal;

    public DayFrame(Calendar cal) {

      setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

      setTitle("日");

      setLocation(200, 100);

      setSize(200, 150);

      setResizable(false);

      this.cal = cal; 

      JLabel lab = new JLabel(cal.get(Calendar.DAY_OF_MONTH) + "");

      lab.setFont(new Font("SansSerif", Font.BOLD, 30));

      add(lab);

      setVisible(true);

    }

  }

  

  class ButtonListener implements ActionListener {

    public void actionPerformed(ActionEvent e) {

      if(e.getActionCommand().equals("<")) {

        cal.add(Calendar.MONTH, -1);

      }

      else if(e.getActionCommand().equals(">")) {

        cal.add(Calendar.MONTH, 1);

      }

      refreshCalendar();

    }

  }

  

  public static void main(String[] args) {

    EventQueue.invokeLater(new Runnable() {

      public void run() {

        new CalendarApp().setVisible(true);

      }

    });

  }

}

这个日历程序主要使用了Java的GUI组件来实现,代码结构清晰明了,易于理解,可以为Java初学者提供很好的学习参考。

  
  

评论区

{{item['qq_nickname']}}
()
回复
回复