import javaawtBorderLayout;
import javaawtColor;
import javaawtDimension;
import javaawtGridLayout;
import javaawtSystemColor;
import javaawteventActionEvent;
import javaawteventKeyEvent;
import javaawteventMouseEvent;
import javautilCalendar;
import javautilGregorianCalendar;
import javautilLocale;
import javautilDate;
import javautilStringTokenizer;
import javaxswingBorderFactory;
import javaxswingJButton;
import javaxswingJFrame;
import javaxswingJLabel;
import javaxswingJPanel;
import javaxswingJTextField;
import javaxswingJToggleButton;
import javaxswingSwingConstants;
import javaxswingUIManager;
/
<p>Title: Swing日历</p>
<p>Description: *** 作日期</p>
@author duxu2004
@version 101
/
class JCalendar extends JPanel{
//动态表示年月日
private int year=0;
private int month=0;
private int day=0;
//主面板
private JPanel Main = new JPanel();
//日面板
private JPanel jPanelDay = new JPanel();
//月面板
private JPanel jPanelMonth = new JPanel();
//年的输入位置
private JTextField Year = new JTextField();
//月的输入位置
private JTextField Month = new JTextField();
//减少月份
private JButton MonthDown = new JButton();
//增加月份
private JButton MonthUp = new JButton();
private JPanel jPanelButton = new JPanel();
//减少年份
private JButton YearDown = new JButton();
//增加年份
private JButton YearUp = new JButton();
//显示日期的位置
private JLabel Out = new JLabel();
//中国时区,以后可以从这里扩展可以设置时区的功能
private Locale l=LocaleCHINESE;
//主日历
private GregorianCalendar cal=new GregorianCalendar(l);
//星期面板
private JPanel weekPanel=new JPanel();
//天按钮组
private JToggleButton[] days=new JToggleButton[42];
//天面板
private JPanel Days = new JPanel();
//标示
private JLabel jLabel1 = new JLabel();
private JLabel jLabel2 = new JLabel();
private JLabel jLabel3 = new JLabel();
private JLabel jLabel4 = new JLabel();
private JLabel jLabel5 = new JLabel();
private JLabel jLabel6 = new JLabel();
private JLabel jLabel7 = new JLabel();
//当前选择的天数按钮
private JToggleButton cur=null;
//月份天数数组,用来取得当月有多少天
// 1 2 3 4 5 6 7 8 9 10 11 12
private int[] mm={31,28,31,30,31,30,31,31,30,31,30,31};
//空日期构造函数
public JCalendar() {
try {
jbInit();
}
catch(Exception e) {
eprintStackTrace();
}
}
//带日期设置的构造函数
public JCalendar(int year, int month, int day) {
calset(year, month, day);
try {
jbInit();
}
catch (Exception e) {
eprintStackTrace();
}
}
//带日历输入的构造函数
public JCalendar(GregorianCalendar calendar) {
cal=calendar;
try {
jbInit();
}
catch (Exception e) {
eprintStackTrace();
}
}
//带日期输入的构造函数
public JCalendar(Date date) {
calsetTime(date);
try {
jbInit();
}
catch (Exception e) {
eprintStackTrace();
}
}
//初始化组件
private void jbInit() throws Exception {
//初始化年、月、日
iniCalender();
thissetLayout(new BorderLayout());
thissetBorder(BorderFactorycreateRaisedBevelBorder());
thissetMaximumSize(new Dimension(200, 200));
thissetMinimumSize(new Dimension(200, 200));
thissetPreferredSize(new Dimension(200, 200));
MainsetLayout(new BorderLayout());
MainsetBackground(SystemColorinfo);
MainsetBorder(null);
OutsetBackground(ColorlightGray);
OutsetHorizontalAlignment(SwingConstantsCENTER);
OutsetMaximumSize(new Dimension(100, 19));
OutsetMinimumSize(new Dimension(100, 19));
OutsetPreferredSize(new Dimension(100, 19));
jLabel1setForeground(Colorred);
jLabel1setHorizontalAlignment(SwingConstantsCENTER);
jLabel1setHorizontalTextPosition(SwingConstantsCENTER);
jLabel1setText("日");
jLabel2setForeground(Colorblue);
jLabel2setHorizontalAlignment(SwingConstantsCENTER);
jLabel2setHorizontalTextPosition(SwingConstantsCENTER);
jLabel2setText("六");
jLabel3setHorizontalAlignment(SwingConstantsCENTER);
jLabel3setHorizontalTextPosition(SwingConstantsCENTER);
jLabel3setText("五");
jLabel4setHorizontalAlignment(SwingConstantsCENTER);
jLabel4setHorizontalTextPosition(SwingConstantsCENTER);
jLabel4setText("四");
jLabel5setHorizontalAlignment(SwingConstantsCENTER);
jLabel5setHorizontalTextPosition(SwingConstantsCENTER);
jLabel5setText("三");
jLabel6setBorder(null);
jLabel6setHorizontalAlignment(SwingConstantsCENTER);
jLabel6setHorizontalTextPosition(SwingConstantsCENTER);
jLabel6setText("二");
jLabel7setBackground(ColorlightGray);
jLabel7setForeground(Colorblack);
jLabel7setBorder(null);
jLabel7setHorizontalAlignment(SwingConstantsCENTER);
jLabel7setHorizontalTextPosition(SwingConstantsCENTER);
jLabel7setText("一");
weekPanelsetBackground(UIManagergetColor("InternalFrameactiveTitleGradient"));
weekPanelsetBorder(BorderFactorycreateEtchedBorder());
weekPanelsetLayout(new GridLayout(1,7));
weekPaneladd(jLabel1, null);
weekPaneladd(jLabel7, null);
weekPaneladd(jLabel6, null);
weekPaneladd(jLabel5, null);
weekPaneladd(jLabel4, null);
weekPaneladd(jLabel3, null);
weekPaneladd(jLabel2, null);
MonthUpsetAlignmentX((float) 00);
MonthUpsetActionMap(null);
jPanelMonthsetBackground(SystemColorinfo);
jPanelMonthsetLayout(new BorderLayout());
jPanelMonthsetBorder(BorderFactorycreateEtchedBorder());
MonthsetBorder(null);
MonthsetHorizontalAlignment(SwingConstantsCENTER);
MonthaddMouseListener(new javaawteventMouseAdapter() {
public void mouseClicked(MouseEvent e) {
Month_mouseClicked(e);
}
});
MonthaddKeyListener(new javaawteventKeyAdapter() {
public void keyPressed(KeyEvent e) {
Month_keyPressed(e);
}
});
MonthDownsetBorder(null);
MonthDownsetText("\u25C4");
MonthDownaddActionListener(new javaawteventActionListener() {
public void actionPerformed(ActionEvent e) {
MonthDown_actionPerformed(e);
}
});
MonthUpsetBorder(null);
MonthUpsetText("\u25BA");
MonthUpaddActionListener(new javaawteventActionListener() {
public void actionPerformed(ActionEvent e) {
MonthUp_actionPerformed(e);
}
});
jPanelButtonsetLayout(null);
jPanelButtonsetBorder(null);
jPanelButtonaddComponentListener(new javaawteventComponentAdapter() {
public void componentResized(javaawteventComponentEvent evt) {
jPanelButtonComponentResized(evt);
}
});
YearsetBorder(BorderFactorycreateEtchedBorder());
YearsetMaximumSize(new Dimension(80, 25));
YearsetMinimumSize(new Dimension(80, 25));
YearsetPreferredSize(new Dimension(80, 25));
YearsetHorizontalAlignment(SwingConstantsCENTER);
YearaddMouseListener(new javaawteventMouseAdapter() {
public void mouseClicked(MouseEvent e) {
Year_mouseClicked(e);
}
});
YearaddKeyListener(new javaawteventKeyAdapter() {
public void keyPressed(KeyEvent e) {
Year_keyPressed(e);
}
});
YearDownsetBorder(null);
YearDownsetMaximumSize(new Dimension(16, 16));
YearDownsetMinimumSize(new Dimension(16, 16));
YearDownsetPreferredSize(new Dimension(16, 16));
YearDownsetSize(new Dimension(16, 16));
YearDownsetText("▼");
YearDownaddActionListener(new javaawteventActionListener() {
public void actionPerformed(ActionEvent e) {
YearDown_actionPerformed(e);
}
});
YearUpsetBorder(null);
YearUpsetMaximumSize(new Dimension(16, 16));
YearUpsetMinimumSize(new Dimension(16, 16));
YearUpsetPreferredSize(new Dimension(16, 16));
YearUpsetSize(new Dimension(16, 16));
YearUpsetText("▲");
YearUpaddActionListener(new javaawteventActionListener() {
public void actionPerformed(ActionEvent e) {
YearUp_actionPerformed(e);
}
});
jPanelDaysetLayout(new BorderLayout());
DayssetLayout(new GridLayout(6,7));
DayssetBackground(SystemColorinfo);
for(int i=0;i<42;i++){
days[i]=new JToggleButton();
days[i]setBorder(null);
days[i]setBackground(SystemColorinfo);
days[i]setHorizontalAlignment(SwingConstantsCENTER);
days[i]setHorizontalTextPosition(SwingConstantsCENTER);
//days[i]setSize(l,l);
days[i]addActionListener(new javaawteventActionListener(){
public void actionPerformed(ActionEvent e) {
day=IntegerparseInt(((JToggleButton)egetSource())getText());
showDate();
showDays();
}
});
Daysadd(days[i]);
}
thisadd(Main, BorderLayoutNORTH);
thisadd(jPanelDay, BorderLayoutCENTER);
thisadd(jPanelMonth, BorderLayoutSOUTH);
Mainadd(Year, BorderLayoutCENTER);
Mainadd(Out, BorderLayoutWEST);
Mainadd(jPanelButton, BorderLayoutEAST);
jPanelButtonadd(YearUp);
jPanelButtonadd(YearDown);
jPanelDayadd(weekPanel,BorderLayoutNORTH);
jPanelDayadd(Days, BorderLayoutCENTER);
jPanelMonthadd(Month, BorderLayoutCENTER);
jPanelMonthadd(MonthDown, BorderLayoutWEST);
jPanelMonthadd(MonthUp, BorderLayoutEAST);
showMonth();
showYear();
showDate();
showDays();
}
//自定义重画年选择面板
void jPanelButtonComponentResized(javaawteventComponentEvent evt){
YearUpsetLocation(0,0);
YearDownsetLocation(0,YearUpgetHeight());
jPanelButtonsetSize(YearUpgetWidth(),YearUpgetHeight()2);
jPanelButtonsetPreferredSize(new Dimension(YearUpgetWidth(),YearUpgetHeight()2));
jPanelButtonupdateUI();
}
//测试用
public static void main(String[] args){
JFrame f=new JFrame();
fsetContentPane(new JCalendar());
fpack();
//fsetResizable(false);
fshow();
}
//增加年份
void YearUp_actionPerformed(ActionEvent e) {
year++;
showYear();
showDate();
showDays();
}
//减少年份
void YearDown_actionPerformed(ActionEvent e) {
year--;
showYear();
showDate();
showDays();
}
//减少月份
void MonthDown_actionPerformed(ActionEvent e) {
month--;
if(month<0) {
month = 11;
year--;
showYear();
}
showMonth();
showDate();
showDays();
}
//增加月份
void MonthUp_actionPerformed(ActionEvent e) {
month++;
if(month==12) {
month=0;
year++;
showYear();
}
showMonth();
showDate();
showDays();
}
//初始化年月日
void iniCalender(){
year=calget(CalendarYEAR);
month=calget(CalendarMONTH);
day=calget(CalendarDAY_OF_MONTH);
}
//刷新月份
void showMonth(){
MonthsetText(IntegertoString(month+1)+"月");
}
//刷新年份
void showYear(){
YearsetText(IntegertoString(year)+"年");
}
//刷新日期
void showDate(){
OutsetText(IntegertoString(year)+"-"+IntegertoString(month+1)+"-"+IntegertoString(day));
}
//重画天数选择面板
void showDays() {
calset(year,month,1);
int firstDayOfWeek = calget(CalendarDAY_OF_WEEK);
int n=mm[month];
if(calisLeapYear(year)&&month==1) n++;
int i=0;
for(;i<firstDayOfWeek-1;i++){
days[i]setEnabled(false);
days[i]setSelected(false);
days[i]setText("");
}
int d=1;
for(;d<=n;d++){
days[i]setText(IntegertoString(d));
days[i]setEnabled(true);
if(d==day) days[i]setSelected(true);
else days[i]setSelected(false);;
i++;
}
for(;i<42;i++){
days[i]setEnabled(false);
days[i]setSelected(false);
days[i]setText("");
}
}
//单击年份面板选择整个年份字符串
void SelectionYear(){
YearsetSelectionStart(0);
YearsetSelectionEnd(YeargetText()length());
}
//单击月份面板选择整个月份字符串
void SelectionMonth(){
MonthsetSelectionStart(0);
MonthsetSelectionEnd(MonthgetText()length());
}
//月份面板响应鼠标单击事件
void Month_mouseClicked(MouseEvent e) {
//SelectionMonth();
inputMonth();
}
//检验输入的月份
void inputMonth(){
String s;
if(MonthgetText()endsWith("月"))
{
s=MonthgetText()substring(0,MonthgetText()length()-1);
}
else s=MonthgetText();
month=IntegerparseInt(s)-1;
thisshowMe();
}
//月份面板键盘敲击事件响应
void Month_keyPressed(KeyEvent e) {
if(egetKeyChar()==10)
inputMonth();
}
//年份面板响应鼠标单击事件
void Year_mouseClicked(MouseEvent e) {
//SelectionYear();
inputYear();
}
//年份键盘敲击事件响应
void Year_keyPressed(KeyEvent e) {
//Systemoutprint(new Integer(egetKeyChar())byteValue());
if(egetKeyChar()==10)
inputYear();
}
//检验输入的年份字符串
void inputYear() {
String s;
if(YeargetText()endsWith("年"))
{
s=YeargetText()substring(0,YeargetText()length()-1);
}
else s=YeargetText();
year=IntegerparseInt(s);
thisshowMe();
}
//以字符串形式返回日期,yyyy-mm-dd
public String getDate(){return OutgetText();}
//以字符串形式输入日期,yyyy-mm-dd
public void setDate(String date){
if(date!=null){
StringTokenizer f = new StringTokenizer(date, "-");
if(fhasMoreTokens())
year = IntegerparseInt(fnextToken());
if(fhasMoreTokens())
month = IntegerparseInt(fnextToken());
if(fhasMoreTokens())
day = IntegerparseInt(fnextToken());
calset(year,month,day);
}
thisshowMe();
}
//以日期对象形式输入日期
public void setTime(Date date){
calsetTime(date);
thisiniCalender();
thisshowMe();
}
//返回日期对象
public Date getTime(){return calgetTime();}
//返回当前的日
public int getDay() {
return day;
}
//设置当前的日
public void setDay(int day) {
thisday = day;
calset(thisyear,thismonth,thisday);
thisshowMe();
}
//设置当前的年
public void setYear(int year) {
thisyear = year;
calset(thisyear,thismonth,thisday);
thisshowMe();
}
//返回当前的年
public int getYear() {
return year;
}
//返回当前的月
public int getMonth() {
return month;
}
//设置当前的月
public void setMonth(int month) {
thismonth = month;
calset(thisyear,thismonth,thisday);
thisshowMe();
}
//刷新
public void showMe(){
thisshowDays();
thisshowMonth();
thisshowYear();
thisshowDate();
}
}
public class TestJCalendar {
public static void main(String[] args) {
JFrame f=new JFrame();
fsetContentPane(new JCalendar());
fpack();
//fsetResizable(false);
fshow();
}
}
你的问题就是把一周的第一天设置为星期一?
那你贴这么多代码干嘛,一句话的事儿吗不是。建议你多看一下api
Calendar里面的方法就有啊, setFirstDayOfWeek
import javautilCalendar;
import javautilDate;
import javautilScanner;
public class MyCalendar
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(Systemin);
String reg = "^(\\d+)[^\\d]+((0[1-9])|(1[012]))$";
while(true)
{
Systemoutprintln("输入年月(年和月用非数字隔开:如20151)(什么都不输入直接退出)");
String line = scannernextLine()trim();
if(""equals(line))
{
scannerclose();
break;
}
if(!linematches(reg))
{
continue;
}
int year = IntegerparseInt(linereplaceAll(reg, "$1"));
int month = IntegerparseInt(linereplaceAll(reg, "$2"));
Systemoutprintln("日\t一\t二\t三\t四\t五\t六");
Calendar calendar = CalendargetInstance();
// 这个月的1号是星期几
calendarset(year, month - 1, 1);
int day = calendarget(CalendarDAY_OF_WEEK);
int start = CalendarSUNDAY;
calendaradd(CalendarDATE, -day + start);
while(start < day)
{
Systemoutprint(calendarget(CalendarDATE) + "\t");
calendaradd(CalendarDATE, 1);
start++;
}
calendarset(year, month - 1, 1);
Date now = calendargetTime();
calendarset(year, month, 1);
Date next = calendargetTime();
for(Date cur = now; curbefore(next);)
{
calendarsetTime(cur);
int x = calendarget(CalendarDATE);
String tmp = x < 10 "0" + x : x + "";
Systemoutprint(tmp + "\t");
if(calendarget(CalendarDAY_OF_WEEK) == CalendarSATURDAY)
{
Systemoutprintln();
}
calendaradd(CalendarDATE, 1);
cur = calendargetTime();
}
calendaradd(CalendarDATE, -1);
int to = calendarget(CalendarDAY_OF_WEEK);
int end = CalendarSATURDAY;
while(to < end)
{
calendaradd(CalendarDATE, 1);
int x = calendarget(CalendarDATE);
String tmp = x < 10 "0" + x : x + "";
Systemoutprint(tmp + "\t");
to++;
}
Systemoutprintln("\r\n");
}
}
}
Calendar
cal=CalendargetInstance();
获取一个calendar实例。这个实例就代表当前系统的时间。然后你可以调用calDATE
calYEAR等获得日,年等的值。调用calset(int
year,int
month,int
day)即可设置你想要的日期的日历。
set
(int year,
int month,
int date) 设置日历字段
YEAR、MONTH
和
DAY_OF_MONTH
的值。
以上就是关于怎样用java编写日历全部的内容,包括:怎样用java编写日历、java核心技术的日历程序,模仿着写出来发现一个星期的第一天是星期日,试了各种方法都改不了,附代码、日历输出程序java控制流程等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)