需求: 用户输入年份和月份,打印这个月对应的日历信息(1900年1月是周1) 分析: 1.1号是周几? (求出中间的总天数=[1900,2022)年的总天数+当年[1,12)月的总天数 + 1) % 2.这个月有多少天?
1.窗口界面类
import javax.swing.*;
/**
* 主窗口
*/
public class DateTimeFrame extends JFrame {
//年份框
private JLabel yearLabel = null;
private JTextField yearText = null;
//月份
private JLabel monthLabel = null;
private JTextField monthText = null;
//查询
private JButton queryButton = null;
//多行文本框
private JTextArea textArea = null;
public DateTimeFrame(){
init();
}
public void init(){
this.setTitle("日历查询");
this.setSize(600,400);
this.setResizable(false);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//构建窗口
yearLabel = new JLabel("年份:");
yearText = new JTextField();
monthLabel = new JLabel("月份:");
monthText = new JTextField();
queryButton = new JButton("查询");
textArea = new JTextArea(6,20);
JPanel panel = new JPanel();
panel.setLayout(null);
this.add(panel);
//年份
yearLabel.setBounds(130,50,50,30);
panel.add(yearLabel);
yearText.setBounds(230,50,100,30);
panel.add(yearText);
//月份
monthLabel.setBounds(130,120,50,30);
panel.add(monthLabel);
monthText.setBounds(230,120,100,30);
panel.add(monthText);
//查询
queryButton.setBounds(350,120,80,30);
panel.add(queryButton);
//添加查询按钮监听器
queryButton.addActionListener(new QueryButton_ActionLiner(this));
//多行文本框
textArea.setLocation(20,180);
textArea.setSize(550,130);
panel.add(textArea);
this.setVisible(true);
}
public static void main(String[] args) {
new DateTimeFrame();
}
public JTextField getYearText() {
return yearText;
}
public JTextField getMonthText() {
return monthText;
}
public JTextArea getTextArea() {
return textArea;
}
}
2.查询按钮监听器类
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
* 查询监听器
*/
public class QueryButton_ActionLiner implements ActionListener {
private DateTimeFrame frame = null;
public QueryButton_ActionLiner(DateTimeFrame frame){
this.frame = frame;
}
@Override
public void actionPerformed(ActionEvent e) {
//获取年份和月份
int year = 0,month = 0;
if (!((frame.getYearText().getText().trim()).equals(""))){
year = Integer.parseInt(frame.getYearText().getText().trim());
}
if (!((frame.getMonthText().getText().trim()).equals(""))){
month = Integer.parseInt(frame.getMonthText().getText().trim());
}
System.out.println(year+"年"+month+"月");
//2.计算出年总天数--[1900,2022)年的总天数
int years = 0;//保存年的总天数
for (int i = 1900; i < year; i++){
if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0){
years += 366;
}else{
years += 365;
}
}
//1900 - 2022年总天数为44560
//System.out.println("年的总天数:"+years+"天");
//3.计算出当年该月的总天数--[1,12)月的总天数
int months = 0;//保存月的总天数
for (int i = 1; i < month; i++){
if (i==1||i==3||i==5||i==7||i==8||i==10||i==12){
months += 31;
}else if (i == 2){
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0){
months += 29;
}else{
months += 28;
}
}else{
months += 30;
}
}
//System.out.println("月的总天数:"+months+"天");
//4.根据2和3计算出1号是周几?
int week = (1 + years + months) % 7;
//System.out.println(year+"年"+month+"月是周"+week);
//5.这个月有多少天?
int days = 0;//保存当月的总天数
if (month==1||month==3||month==5||month==7||month==8||month==10||month==12){
days = 31;
} else if (month == 2) {
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0){
days = 29;
}else{
days = 28;
}
}else{
days = 30;
}
//System.out.println(month+"月有"+days+"天");
//按日历格式输出
System.out.println("日\t一\t二\t三\t四\t五\t六");
frame.getTextArea().append("日\t一\t二\t三\t四\t五\t六\n");
//先打印1号之前的空格--是周几就打印几个
for (int i = 1; i <= week; i++){
System.out.print("\t");
frame.getTextArea().append("\t");
}
//再打印后序日期数
for (int i = 1; i <= days; i++){
System.out.print(i);
frame.getTextArea().append(""+i);
if (( i + week ) % 7 == 0){
System.out.print("\n");
frame.getTextArea().append("\n");
}else {
System.out.print("\t");
frame.getTextArea().append("\t");
}
}
//将日历数据输入到JTextArea组件中显示
JTextArea textArea = frame.getTextArea();
}
}