写日历的时候出现的问题
我就是设置当天的颜色是兰色的可是,随着年和月的变化,兰色的日期也就变的多了起来,不知道怎么搞的,如今天是5号,以后你改年和月可是每个月的5号都是兰色!
大家帮我看看我的这个方法那里错了?/
public void setToday(){//设置当天的颜色为兰色
for(int count=getDayweek();count<getDayweek()+getMaxDay();count++)
if(btn[count].getActionCommand().trim().equals(String.valueOf(getToday())))
btn[count].setForeground(Color.BLUE);
}
原代码我也贴上大家帮忙看看谢谢了啊!
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.event.*;
class MyCalendar extends JFrame{
private final String []week={" 日"," 一"," 二"," 三"," 四"," 五"," 六"};
private GregorianCalendar now;
private static final GregorianCalendar nows=new GregorianCalendar();
private int days,year,month,dayweek;//得到当月的天数,年,月,星期几
private int today; //得到今天的日期
private JSpinner yearJ; //年份微调器
private JComboBox monthC; //月份的组合框
private JLabel yearLbl; //年标签
private JLabel monthLbl; //月标签
private JLabel dateLbl;
private JLabel []lbl=new JLabel[7];
private JButton[] btn=new JButton[42];
private JPanel panel_ym; //放置年和月份的面板
private JPanel panel_day;// 放置天数的面板
public int getToday(){
today=nows.get(Calendar.DAY_OF_MONTH);
return today;
}
public MyCalendar(){
setTitle("Calendar Applecation");
setSize(400,220);//设置大小
setResizable(false);//大小为不可调整
Container c=getContentPane();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addWindowListener(new WindowAdapter(){//关闭窗口释放系统的资源
public void windowClosing(WindowEvent e){
dispose();
System.exit(0);
}});
panel_ym=new JPanel();
panel_day=new JPanel();
monthC=new JComboBox();
yearLbl=new JLabel("年");
yearLbl.setFont(new Font("宋体",Font.BOLD,18));
yearLbl.setForeground(Color.BLUE);
monthLbl=new JLabel("月");
monthLbl.setFont(new Font("宋体",Font.BOLD,18));
monthLbl.setForeground(Color.BLUE);
dateLbl =new JLabel("日期:");
dateLbl.setFont(new Font("宋体",Font.BOLD,18));
dateLbl.setForeground(Color.BLUE);
JLabel lbls=new JLabel(" ");
yearJ =new JSpinner();//滑块
now =new GregorianCalendar();//得到系统的当前日期
//Calendar nows=(Calendar)(now.clone());
//today = nows.get(Calendar.DAY_OF_MONTH);//得到今天的日期
for(int i=1;i<13;i++) //月份组合框框添加1-12月
monthC.addItem(""+i);
month=now.get(Calendar.MONTH);//得到当前系统的月份
monthC.setSelectedIndex(month); //默认为当前系统的月份
monthC.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
setDate();
//setToday();
}});
//设置数字的格式为"0000"
yearJ.setEditor(new JSpinner.NumberEditor(yearJ, "0000"));
yearJ.setValue(new Integer(now.get(Calendar.YEAR)));//设定年份列表为当前年份
yearJ.addChangeListener(new ChangeListener() { //添加当滑块的数值发生变化
public void stateChanged(ChangeEvent changeEvent) {
setDate();
// setToday();
}
});
panel_ym.add(dateLbl);//添加控件
panel_ym.add(yearJ);
panel_ym.add(yearLbl);
panel_ym.add(lbls);
panel_ym.add(monthC);
panel_ym.add(monthLbl);
c.add(panel_ym,BorderLayout.NORTH);
c.add(panel_day,BorderLayout.CENTER);
panel_day.setLayout(new GridLayout(7,7,1,1));//网格布局
for(int i=0;i<7;i++){ //添加并设置头标签
lbl[i]=new JLabel();
lbl[i].setFont(new Font("楷体",Font.BOLD,18));
lbl[i].setForeground(Color.blue);
lbl[i].setText(week[i]);
panel_day.add(lbl[i]);
}
for(int i=0;i<42;i++){ //添加日期按扭
btn[i]=new JButton();
panel_day.add(btn[i]);
btn[i].setFont(new Font("宋体",Font.BOLD,16));
}
setDate();
}
public int getMaxDay(){ //得到当前月的最大天数
now=new GregorianCalendar(getYear(),getMonth(),1);
days=now.getActualMaximum(Calendar.DAY_OF_MONTH);
return days;
}
public int getMonth(){//得到当前的月份
return monthC.getSelectedIndex();
}
public int getYear(){ //得到当前的年份
year=((Integer)yearJ.getValue()).intValue();//得到控制器的数值
return year;
}
public int getDayweek(){ //从星期天开始
now=new GregorianCalendar(getYear(),getMonth(),today);//设置当月的头一天
now.set(Calendar.DAY_OF_MONTH,1);
dayweek=now.get(Calendar.DAY_OF_WEEK);//得到当月的头一天是星期几
return dayweek;
}
public void setDate(){
now=new GregorianCalendar(getYear(),getMonth(),1);
for(int i=0;i<42;i++){//全部按扭置为空
btn[i].setText("");
if(i%7==0||i==6||i==13||i==20||i==27||i==34||i==41)//设置星期天字颜色为红色
btn[i].setForeground(Color.red);}
int count=getDayweek()-1;
for(int i=1;i<=getMaxDay();i++){//添加日期到上面
btn[count++].setText(String.valueOf(i));
}
setToday();
}
public void setToday(){//设置当天的颜色为兰色
for(int count=getDayweek();count<getDayweek()+getMaxDay();count++)
if(btn[count].getActionCommand().trim().equals(String.valueOf(getToday())))
btn[count].setForeground(Color.BLUE);
}
}
public class MyCalendarTest{
public static void main(String []args){
Toolkit kit=Toolkit.getDefaultToolkit();
Dimension screen=kit.getScreenSize();//得到屏幕的对象
int width=screen.width;//屏幕宽度
int height=screen.height;//屏幕高度
MyCalendar frame=new MyCalendar();
frame.setLocation(width/4,height/4);//显示在屏幕的位置
frame.show();//显示窗口
}
}
大家帮我看看我的这个方法那里错了?/
public void setToday(){//设置当天的颜色为兰色
for(int count=getDayweek();count<getDayweek()+getMaxDay();count++)
if(btn[count].getActionCommand().trim().equals(String.valueOf(getToday())))
btn[count].setForeground(Color.BLUE);
}
原代码我也贴上大家帮忙看看谢谢了啊!
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.event.*;
class MyCalendar extends JFrame{
private final String []week={" 日"," 一"," 二"," 三"," 四"," 五"," 六"};
private GregorianCalendar now;
private static final GregorianCalendar nows=new GregorianCalendar();
private int days,year,month,dayweek;//得到当月的天数,年,月,星期几
private int today; //得到今天的日期
private JSpinner yearJ; //年份微调器
private JComboBox monthC; //月份的组合框
private JLabel yearLbl; //年标签
private JLabel monthLbl; //月标签
private JLabel dateLbl;
private JLabel []lbl=new JLabel[7];
private JButton[] btn=new JButton[42];
private JPanel panel_ym; //放置年和月份的面板
private JPanel panel_day;// 放置天数的面板
public int getToday(){
today=nows.get(Calendar.DAY_OF_MONTH);
return today;
}
public MyCalendar(){
setTitle("Calendar Applecation");
setSize(400,220);//设置大小
setResizable(false);//大小为不可调整
Container c=getContentPane();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addWindowListener(new WindowAdapter(){//关闭窗口释放系统的资源
public void windowClosing(WindowEvent e){
dispose();
System.exit(0);
}});
panel_ym=new JPanel();
panel_day=new JPanel();
monthC=new JComboBox();
yearLbl=new JLabel("年");
yearLbl.setFont(new Font("宋体",Font.BOLD,18));
yearLbl.setForeground(Color.BLUE);
monthLbl=new JLabel("月");
monthLbl.setFont(new Font("宋体",Font.BOLD,18));
monthLbl.setForeground(Color.BLUE);
dateLbl =new JLabel("日期:");
dateLbl.setFont(new Font("宋体",Font.BOLD,18));
dateLbl.setForeground(Color.BLUE);
JLabel lbls=new JLabel(" ");
yearJ =new JSpinner();//滑块
now =new GregorianCalendar();//得到系统的当前日期
//Calendar nows=(Calendar)(now.clone());
//today = nows.get(Calendar.DAY_OF_MONTH);//得到今天的日期
for(int i=1;i<13;i++) //月份组合框框添加1-12月
monthC.addItem(""+i);
month=now.get(Calendar.MONTH);//得到当前系统的月份
monthC.setSelectedIndex(month); //默认为当前系统的月份
monthC.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
setDate();
//setToday();
}});
//设置数字的格式为"0000"
yearJ.setEditor(new JSpinner.NumberEditor(yearJ, "0000"));
yearJ.setValue(new Integer(now.get(Calendar.YEAR)));//设定年份列表为当前年份
yearJ.addChangeListener(new ChangeListener() { //添加当滑块的数值发生变化
public void stateChanged(ChangeEvent changeEvent) {
setDate();
// setToday();
}
});
panel_ym.add(dateLbl);//添加控件
panel_ym.add(yearJ);
panel_ym.add(yearLbl);
panel_ym.add(lbls);
panel_ym.add(monthC);
panel_ym.add(monthLbl);
c.add(panel_ym,BorderLayout.NORTH);
c.add(panel_day,BorderLayout.CENTER);
panel_day.setLayout(new GridLayout(7,7,1,1));//网格布局
for(int i=0;i<7;i++){ //添加并设置头标签
lbl[i]=new JLabel();
lbl[i].setFont(new Font("楷体",Font.BOLD,18));
lbl[i].setForeground(Color.blue);
lbl[i].setText(week[i]);
panel_day.add(lbl[i]);
}
for(int i=0;i<42;i++){ //添加日期按扭
btn[i]=new JButton();
panel_day.add(btn[i]);
btn[i].setFont(new Font("宋体",Font.BOLD,16));
}
setDate();
}
public int getMaxDay(){ //得到当前月的最大天数
now=new GregorianCalendar(getYear(),getMonth(),1);
days=now.getActualMaximum(Calendar.DAY_OF_MONTH);
return days;
}
public int getMonth(){//得到当前的月份
return monthC.getSelectedIndex();
}
public int getYear(){ //得到当前的年份
year=((Integer)yearJ.getValue()).intValue();//得到控制器的数值
return year;
}
public int getDayweek(){ //从星期天开始
now=new GregorianCalendar(getYear(),getMonth(),today);//设置当月的头一天
now.set(Calendar.DAY_OF_MONTH,1);
dayweek=now.get(Calendar.DAY_OF_WEEK);//得到当月的头一天是星期几
return dayweek;
}
public void setDate(){
now=new GregorianCalendar(getYear(),getMonth(),1);
for(int i=0;i<42;i++){//全部按扭置为空
btn[i].setText("");
if(i%7==0||i==6||i==13||i==20||i==27||i==34||i==41)//设置星期天字颜色为红色
btn[i].setForeground(Color.red);}
int count=getDayweek()-1;
for(int i=1;i<=getMaxDay();i++){//添加日期到上面
btn[count++].setText(String.valueOf(i));
}
setToday();
}
public void setToday(){//设置当天的颜色为兰色
for(int count=getDayweek();count<getDayweek()+getMaxDay();count++)
if(btn[count].getActionCommand().trim().equals(String.valueOf(getToday())))
btn[count].setForeground(Color.BLUE);
}
}
public class MyCalendarTest{
public static void main(String []args){
Toolkit kit=Toolkit.getDefaultToolkit();
Dimension screen=kit.getScreenSize();//得到屏幕的对象
int width=screen.width;//屏幕宽度
int height=screen.height;//屏幕高度
MyCalendar frame=new MyCalendar();
frame.setLocation(width/4,height/4);//显示在屏幕的位置
frame.show();//显示窗口
}
}
...全文
相关推荐
自己写了个JS日历
NULL 博文链接:https://huahongluo.iteye.com/blog/1461072
【uniapp3】分享一个自己写的h5日历组件
分享一下自己基于uniapp写的日历组件。如果不太满足你的需求,可以自己改造。 实现分析:代码: uniapp写的 vue2和vue3应该都能用,样式注意使用了sass。 使用,记得引用先。 结束了。本来想写更多呢,结果代码字数太多,太卡了,有兴趣自己看吧。
C语言写日历
C语言写日历 通过标准年的校订,输入一个标准年之后的年和月,求出该月的日历。 博主没有使用数组,很适合C语言初学者。 下面是代码#include <stdio.h> int main() { int start_year=1970; int start_week=4; int year,month,week,day,sum; int i,n,x,j,m,k,p,o; p=1; j=0;...
【C++】打印日历
在我们的手机或电脑上都有日历,如下图: 如上图的样式我们用代码也可实现,在Linux操作系统中就有这样的命令:cal 一、代码实现: 1.首先,1990年1月1号是星期一,我们选这一天做标尺。 2.打印指定年份对应的指定月份的日历,我们要先判断这一年是闰年还是平年,然后判断月份,最后求出指定年份的指定月份距1990年1月1号总共多少天。 3.根据总天数确定月份,然后打印这...
用brew写的日历代码
瞎写的些日历代码,,希望各位看看,刚学brew不久,,希望各位大侠能多多指正。


Java SE

