package Calendar;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.Border;
import javax.swing.border.EtchedBorder;
import javax.swing.*;
//图形用户界面设计
public class Calendarshowing {
static Calendar c = Calendar.getInstance();
static int year = c.get(Calendar.YEAR), month = c.get(Calendar.MONTH)+1,monthday = 0;
static String[] weekdaystr = { "日", "一", "二", "三", "四", "五", "六" };//设置星期日.一.二.三.四.五.六
JFrame frame = new JFrame("日历表");
// JPanel panel = new JPanel();
static JLabel[]dayLabel = new JLabel[49];//是7行7列的GridLayout布局,pCenter的中放置49个标签,用来显示日历
static JPanel showPanel;
//放置两个按钮:nextMonth和previousMonth按钮,单击nextMonth(下一月),可以显示当前月的下一个月的日历;单击previousMonth(上一月)按钮,可以显示当前月的上一个月的日历
JButton Prebutton = new JButton("上一月");//查看上一月的特定按钮
JButton Nextbutton = new JButton("下一月");//查看下一月的特定按钮
JButton Cbutton = new JButton("查询( 请输入正确的年和月 )");//查询的特定按钮
JLabel showLabel = new JLabel();
JLabel YearLabel=new JLabel("年");//查询中输入年的特定按钮
JLabel MonthLabel=new JLabel("月");//查询中输入月的特定按钮
JTextArea YearText=new JTextArea("2017");//查询中输入年的初始值
JTextArea MonthText=new JTextArea("4");//查询中输入月的初始值
static Border Border01 = BorderFactory.createLineBorder(Color.BLUE, 1);
public Calendarshowing() {
//frame.setResizable(false);
showPanel = new JPanel();
frame.setBounds(400, 200, 500, 400);//窗体所在位置、大小
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container content = frame.getContentPane();
final JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout());
Cbutton.addActionListener(new PressButton());
Prebutton.addActionListener(new PressButton());
Nextbutton.addActionListener(new PressButton());
buttonPanel.add(Prebutton);
buttonPanel.add(Nextbutton);
buttonPanel.add(YearText);
buttonPanel.add(YearLabel);
buttonPanel.add(MonthText);
buttonPanel.add(MonthLabel);
buttonPanel.add(Cbutton);
content.add(buttonPanel, BorderLayout.NORTH);
final GridLayout gridLayout = new GridLayout(0, 7);
gridLayout.setHgap(10);
gridLayout.setVgap(10);
showPanel.setLayout(gridLayout);
showLabel.setHorizontalAlignment(JLabel.CENTER);
showLabel.setText("<html><Font size = '6',color = 'RED'>"+"日历: "+"<html><Font size='4',color='Green'>"+year+"年"+month+"月"+"</Font></html>");
showLabel.setToolTipText("今天是公元"+c.get(Calendar.YEAR)+"年"+(c.get(Calendar.MONTH)+1)+"月"+c.get(Calendar.DATE)+"日");
content.add(showLabel,BorderLayout.SOUTH);
content.add(showPanel, BorderLayout.CENTER);
frame.setVisible(true);
}
public class PressButton implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
JButton button = (JButton)e.getSource();
if (button == Prebutton)
{
month--;
if (month <= 0){
year--;
month+=12;
}
}
else{
if(button == Nextbutton){
month++;
if (month > 12){
year++;
month-=12;
}
}
else{
// System.out.println(Double.valueOf(CText.getText()));
// System.out.println(c.get(Calendar.YEAR));
while(Double.valueOf(YearText.getText())*12+Double.valueOf(MonthText.getText())<year*12+month){
month--;
if (month <= 0){
year--;
month+=12;
}
}
while(Double.valueOf(YearText.getText())*12+Double.valueOf(MonthText.getText())>year*12+month){
month++;
if (month >12){
year++;
month-=12;
}
}
}
}
showPanel.removeAll();
showPanel.updateUI();
new showing();
showLabel.setHorizontalAlignment(JLabel.CENTER);
showLabel.setText("<html><Font size = '6',color = 'RED'>"+"日历: "+"<html><Font size='4',color='Green'>"+year+"年"+month+"月"+"</Font></html>");
showLabel.setToolTipText("今天是公元"+c.get(Calendar.YEAR)+"年"+(c.get(Calendar.MONTH)+1)+"月"+c.get(Calendar.DATE)+"日");
}
}
static class showing{
public boolean isLeapYear(int year1) // 判断该年是否为闰年
{
return ((year1 % 4 == 0 && year1 % 100 != 0) || (year1 % 400 == 0));
}
public int firstDay() // 用来计算并判断该年第一天是星期几
{
long n = year * 365;
for (int i = 1; i < year; i++){
if (isLeapYear(i)){
n++;
}
}
return (int)n%7;
}
public void printWeek() // 设置图形界面的表头
{
for (int i = 0; i < 7; i++) {
dayLabel[i] = new JLabel(weekdaystr[i],JLabel.CENTER);
dayLabel[i].setBackground(Color.BLACK);
dayLabel[i].setForeground(Color.RED);
showPanel.add(dayLabel[i]);
}
}
public int getMonthDay(int m) // 获取每个月的天数
{
switch (m) {
//月份为1、3、5、7、8、10、12,天数为31;
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
return 31;
//月份为4、6、9、11,天数为30;
case 4:
case 6:
case 9:
case 11:
return 30;
//闰年2月份,天数为29,非闰年,天数为28
case 2:
if (isLeapYear(year))
return 29;
else
return 28;
default:
return 0;
}
}
public int getDays(){
monthday = 0;
for (int i = 1;i<month;i++)
monthday+=getMonthDay(i);
return (firstDay()+(monthday%7))%7;
}
public showing(){
int n =1 ;
printWeek();
int k = getDays();
int Year = c.get(Calendar.YEAR);
int Month = c.get(Calendar.MONTH);
Month += 1;
int Date = c.get(Calendar.DATE);
for (int j = 7;n<=getMonthDay(month);j++){
if (j < k+7){
dayLabel[j] = new JLabel();
showPanel.add(dayLabel[j]);
}
else{
dayLabel[j] = new JLabel(""+n,JLabel.CENTER);
dayLabel[j].setToolTipText("星期"+weekdaystr[(j)%7]);
if (Year == year&&Month==month&&Date == n){
dayLabel[j].setText("<html><Font size = '6',color = 'BLUE'>"+n+"</Font></html>");
dayLabel[(Date-1)%7].setForeground(Color.BLUE);
}
showPanel.add(dayLabel[j]);
n++;
}
}
showPanel.setBorder(Border01);
}
}
public static void main(String[] args) {
Calendarshowing c = new Calendarshowing();
showing s = new Calendarshowing.showing();
}
}
用java写的一个日历程序,新手看不懂,有没有大佬帮忙注释一下每个模块的意思和作用!!!
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
1条回答 默认 最新
- BW_bear 2019-01-02 01:43关注
只简单注解了大类的作用
ps:这代码哪里找的。。。写的真不好看。。
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.Border;
import javax.swing.border.EtchedBorder;
import javax.swing.*;//图形用户界面设计
public class Calendarshowing {
static Calendar c = Calendar.getInstance();
static int year = c.get(Calendar.YEAR), month = c.get(Calendar.MONTH)+1,monthday = 0;
static String[] weekdaystr = { "日", "一", "二", "三", "四", "五", "六" };//设置星期日.一.二.三.四.五.六
JFrame frame = new JFrame("日历表");
// JPanel panel = new JPanel();
static JLabel[]dayLabel = new JLabel[49];//是7行7列的GridLayout布局,pCenter的中放置49个标签,用来显示日历
static JPanel showPanel;
//放置两个按钮:nextMonth和previousMonth按钮,单击nextMonth(下一月),可以显示当前月的下一个月的日历;单击previousMonth(上一月)按钮,可以显示当前月的上一个月的日历
JButton Prebutton = new JButton("上一月");//查看上一月的特定按钮
JButton Nextbutton = new JButton("下一月");//查看下一月的特定按钮
JButton Cbutton = new JButton("查询( 请输入正确的年和月 )");//查询的特定按钮
JLabel showLabel = new JLabel();
JLabel YearLabel=new JLabel("年");//查询中输入年的特定按钮
JLabel MonthLabel=new JLabel("月");//查询中输入月的特定按钮
JTextArea YearText=new JTextArea("2017");//查询中输入年的初始值
JTextArea MonthText=new JTextArea("4");//查询中输入月的初始值
static Border Border01 = BorderFactory.createLineBorder(Color.BLUE, 1);/**
-
构造子,用来创建视图,添加个各种控件
*/
public Calendarshowing() {
//frame.setResizable(false);
showPanel = new JPanel();
frame.setBounds(400, 200, 500, 400);//窗体所在位置、大小
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container content = frame.getContentPane();
final JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout());Cbutton.addActionListener(new PressButton());
Prebutton.addActionListener(new PressButton());
Nextbutton.addActionListener(new PressButton());
buttonPanel.add(Prebutton);
buttonPanel.add(Nextbutton);
buttonPanel.add(YearText);
buttonPanel.add(YearLabel);
buttonPanel.add(MonthText);
buttonPanel.add(MonthLabel);
buttonPanel.add(Cbutton);
content.add(buttonPanel, BorderLayout.NORTH);final GridLayout gridLayout = new GridLayout(0, 7);
gridLayout.setHgap(10);
gridLayout.setVgap(10);
showPanel.setLayout(gridLayout);
showLabel.setHorizontalAlignment(JLabel.CENTER);
showLabel.setText(""+"日历: "+""+year+"年"+month+"月"+"");
showLabel.setToolTipText("今天是公元"+c.get(Calendar.YEAR)+"年"+(c.get(Calendar.MONTH)+1)+"月"+c.get(Calendar.DATE)+"日");
content.add(showLabel,BorderLayout.SOUTH);
content.add(showPanel, BorderLayout.CENTER);
frame.setVisible(true);
}/**
自定义按钮 设置按钮控件的功能
-
@author BW_bear
*
*/
public class PressButton implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
JButton button = (JButton)e.getSource();
if (button == Prebutton)
{
month--;
if (month <= 0){
year--;
month+=12;
}
}
else{
if(button == Nextbutton){
month++;
if (month > 12){
year++;
month-=12;
}
}
else{
// System.out.println(Double.valueOf(CText.getText()));
// System.out.println(c.get(Calendar.YEAR));
while(Double.valueOf(YearText.getText())*12+Double.valueOf(MonthText.getText()) month--;
if (month year--;
month+=12;
}
}
while(Double.valueOf(YearText.getText())*12+Double.valueOf(MonthText.getText())>year*12+month){
month++;
if (month >12){
year++;
month-=12;
}
}
}
}
showPanel.removeAll();
showPanel.updateUI();
new showing();
showLabel.setHorizontalAlignment(JLabel.CENTER);
showLabel.setText(""+"日历: "+""+year+"年"+month+"月"+"");
showLabel.setToolTipText("今天是公元"+c.get(Calendar.YEAR)+"年"+(c.get(Calendar.MONTH)+1)+"月"+c.get(Calendar.DATE)+"日");
}
}/**
处理时间格式
其中构造子中进行了获取时间,调整格式输出
-
@author BW_bear
*
*/
static class showing{public boolean isLeapYear(int year1) // 判断该年是否为闰年
{
return ((year1 % 4 == 0 && year1 % 100 != 0) || (year1 % 400 == 0));
}public int firstDay() // 用来计算并判断该年第一天是星期几
{
long n = year * 365;
for (int i = 1; i < year; i++){
if (isLeapYear(i)){
n++;
}
}
return (int)n%7;
}public void printWeek() // 设置图形界面的表头
{
for (int i = 0; i < 7; i++) {
dayLabel[i] = new JLabel(weekdaystr[i],JLabel.CENTER);
dayLabel[i].setBackground(Color.BLACK);
dayLabel[i].setForeground(Color.RED);
showPanel.add(dayLabel[i]);
}
}public int getMonthDay(int m) // 获取每个月的天数
{
switch (m) {
//月份为1、3、5、7、8、10、12,天数为31;
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
return 31;
//月份为4、6、9、11,天数为30;
case 4:
case 6:
case 9:
case 11:
return 30;
//闰年2月份,天数为29,非闰年,天数为28
case 2:
if (isLeapYear(year))
return 29;
else
return 28;
default:
return 0;
}
}public int getDays(){
monthday = 0;
for (int i = 1;i<month;i++)
monthday+=getMonthDay(i);
return (firstDay()+(monthday%7))%7;
}public showing(){
int n =1 ;
printWeek();
int k = getDays();
int Year = c.get(Calendar.YEAR);
int Month = c.get(Calendar.MONTH);
Month += 1;
int Date = c.get(Calendar.DATE);
for (int j = 7;n<=getMonthDay(month);j++){
if (j < k+7){
dayLabel[j] = new JLabel();
showPanel.add(dayLabel[j]);
}
else{
dayLabel[j] = new JLabel(""+n,JLabel.CENTER);
dayLabel[j].setToolTipText("星期"+weekdaystr[(j)%7]);
if (Year == year&&Month==month&&Date == n){
dayLabel[j].setText(""+n+"");
dayLabel[(Date-1)%7].setForeground(Color.BLUE);
}
showPanel.add(dayLabel[j]);
n++;
}
}
showPanel.setBorder(Border01);
}
}public static void main(String[] args) {
Calendarshowing c = new Calendarshowing();
showing s = new Calendarshowing.showing();
}
}
解决 无用评论 打赏 举报 -
悬赏问题
- ¥15 彩灯控制电路,会的加我QQ1482956179
- ¥200 相机拍直接转存到电脑上 立拍立穿无线局域网传
- ¥15 (关键词-电路设计)
- ¥15 如何解决MIPS计算是否溢出
- ¥15 vue中我代理了iframe,iframe却走的是路由,没有显示该显示的网站,这个该如何处理
- ¥15 操作系统相关算法中while();的含义
- ¥15 CNVcaller安装后无法找到文件
- ¥15 visual studio2022中文乱码无法解决
- ¥15 关于华为5g模块mh5000-31接线问题
- ¥15 keil L6007U报错