永发信息网

你回答的:“编写JAVA程序实现一个简单的日历”这个问题,能不能在代码中加入一些解释呀,谢了!!

答案:3  悬赏:60  手机版
解决时间 2021-01-30 12:22
万分感谢!!!由于我的账号刚创的,所以财富少,所以悬赏少点请不要见怪哦……
是这个代码,因为不能超过500字所以没能把你发的全部显示在这里
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;

public class MyCalendar extends JApplet {

public static final String WEEK_SUN = "SUN";
public static final String WEEK_MON = "MON";
public static final String WEEK_TUE = "TUE";
public static final String WEEK_WED = "WED";
public static final String WEEK_THU = "THU";
public static final String WEEK_FRI = "FRI";
public static final String WEEK_SAT = "SAT";

public static final Color background = Color.white;
public static final Color foreground = Color.black;
public static final Color headerBackground = Color.blue;
public static final Color headerForeground = Color.white;
public static final Color selectedBackground = Color.blue;
public static final Color selectedForeground = Color.white;
最佳答案
*FileCalendarBean.java
*author:***
*Date:2007-12-823:37
*返回某年某月的日历的字符串数组
*/
importjava.util.Calendar;
publicclassCalendarBean...{
intyear=2007,month=0;
privateintmonthDays[]=...{31,28,31,30,31,30,31,31,30,31,30,31};

//设置年份
publicvoidsetYear(intyear)...{
this.year=year;
}

//获取年份
publicintgetYear()...{
returnyear;
}

//设置月份
publicvoidsetMonth(intmonth)...{
this.month=month>0?month:1;
}

//获取月份
publicintgetMonth()...{
returnmonth;
}

//判断是否为闰年
privatestaticbooleanisLeap(intyear)...{
return((year%4==0)&&(year%100==0))||(year%400==0);
}

//获得当前年月的日历所组成字符串数组
publicString[][]getCalendar()...{
Stringdata[][]=newString[6][7];

Calendarcale=Calendar.getInstance();
cale.set(year,month-1,1);

//该月1号是星期几
intweekDay=cale.get(Calendar.DAY_OF_WEEK)-1;

//该月的天数
intday=monthDays[month-1];
if(month==2&&isLeap(year))day=29;

intnextDay=1;

//添加7a686964616fe59b9ee7ad9431333264623138字符串数组
for(intk=0;k<6;k++)...{
if(k==0)
for(intj=weekDay;j<7;j++)...{
data[k][j]=""+nextDay;
nextDay++;
}
else
for(intj=0;j<7&&nextDay<=day;j++)...{
data[k][j]=""+nextDay;
nextDay++;
}
}
returndata;
}

}


importjavax.swing.*;
importjavax.swing.tree.*;
importjava.awt.*;
importjava.awt.event.*;
importjavax.swing.event.*;

publicclassCalenderFrameextendsJFrameimplementsItemListener,TreeSelectionListener...{
JTabletable;
JTreetree=null;
DefaultMutableTreeNoderoot;
Objectname[]=...{"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
JComboBoxyearList;
CalendarBeancale;
Stringrili[][];
Stringitem[]=...{"2006","2007","2008","2009","2010"};
JSplitPanesplit;
intyear=2006;
intmonth=0;
JScrollPanescrollTree,scrollTable;

publicCalenderFrame()...{
cale=newCalendarBean();

//添加年份列表
yearList=newJComboBox();
for(intk=0;k<item.length;k++)
yearList.addItem(item[k]);
yearList.addItemListener(this);

//添加月份树
root=newDefaultMutableTreeNode(item[0]);
tree=newJTree(root);
add(newJScrollPane(tree),BorderLayout.WEST);
tree.addTreeSelectionListener(this);

//设置年月及该月日历
cale.setYear(year);
cale.setMonth(month);
rili=cale.getCalendar();
table=newJTable(rili,name);

//添加到窗口
scrollTree=newJScrollPane(tree);
scrollTable=newJScrollPane(table);
split=newJSplitPane(JSplitPane.HORIZONTAL_SPLIT,true,scrollTree,scrollTable);
add(yearList,BorderLayout.NORTH);
add(split,BorderLayout.CENTER);
updateYear(year);

//设置窗口属性
setSize(580,350);
setVisible(true);
split.setDividerLocation(0.5);
validate();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

//更新年份及月份树
privatevoidupdateYear(intyear)...{
cale.setYear(year);
root=newDefaultMutableTreeNode(String.valueOf(year));

DefaultMutableTreeNodemonths[]=newDefaultMutableTreeNode[13];
for(inti=0;i<12;i++)...{
months[i]=newDefaultMutableTreeNode(""+(i+1));
root.add(months[i]);
}
split.remove(scrollTree);
tree=newJTree(root);
tree.addTreeSelectionListener(this);
scrollTree=newJScrollPane(tree);
split.add(scrollTree,JSplitPane.LEFT);
split.setDividerLocation(0.5);
validate();
}

//年份列表的监听器
publicvoiditemStateChanged(ItemEvente)...{
StringyearStr=yearList.getSelectedItem().toString().trim();
intyear=Integer.parseInt(yearStr);

updateYear(year);
}

//树结点的监听器
publicvoidvalueChanged(TreeSelectionEvente)...{
DefaultMutableTreeNodemonthNode=(DefaultMutableTreeNode)tree.getLastSelectedPathComponent();
if(monthNode.isLeaf())...{
month=Integer.parseInt(monthNode.toString().trim());
cale.setMonth(month);
rili=cale.getCalendar();
split.remove(scrollTable);
table=newJTable(rili,name);
scrollTable=newJScrollPane(table);
split.add(scrollTable,JSplitPane.RIGHT);
split.setDividerLocation(0.5);
validate();
System.out.println("validateok");
}

}

publicstaticvoidmain(Stringargs[])...{
newCalenderFrame();
}
}
全部回答
import java.awt.*; import java.awt.event.*; import java.util.*; import javax.swing.*; import javax.swing.event.*; import javax.swing.table.*; public class MyCalendar extends JApplet { public static final String WEEK_SUN = "SUN"; public static final String WEEK_MON = "MON"; public static final String WEEK_TUE = "TUE"; public static final String WEEK_WED = "WED"; public static final String WEEK_THU = "THU"; public static final String WEEK_FRI = "FRI"; public static final String WEEK_SAT = "SAT"; public static final Color background = Color.white; public static final Color foreground = Color.black; public static final Color headerBackground = Color.blue; public static final Color headerForeground = Color.white; public static final Color selectedBackground = Color.blue; public static final Color selectedForeground = Color.white; private JPanel cPane; private JLabel yearsLabel; private JSpinner yearsSpinner; private JLabel monthsLabel; private JComboBox monthsComboBox; private JTable daysTable; private AbstractTableModel daysModel; private Calendar calendar; public MyCalendar() { cPane = (JPanel) getContentPane(); } public void init() { cPane.setLayout(new BorderLayout()); calendar = Calendar.getInstance(); calendar = Calendar.getInstance(); yearsLabel = new JLabel("Year: "); yearsSpinner = new JSpinner(); yearsSpinner.setEditor(new JSpinner.NumberEditor(yearsSpinner, "0000")); yearsSpinner.setValue(new Integer(calendar.get(Calendar.YEAR))); yearsSpinner.addChangeListener(new ChangeListener() { public void stateChanged(ChangeEvent changeEvent) { int day = calendar.get(Calendar.DAY_OF_MONTH); calendar.set(Calendar.DAY_OF_MONTH, 1); calendar.set(Calendar.YEAR, ((Integer) yearsSpinner.getValue()).intValue()); int maxDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH); calendar.set(Calendar.DAY_OF_MONTH, day > maxDay ? maxDay : day); updateView(); } }); JPanel yearMonthPanel = new JPanel(); cPane.add(yearMonthPanel, BorderLayout.NORTH); yearMonthPanel.setLayout(new BorderLayout()); yearMonthPanel.add(new JPanel(), BorderLayout.CENTER); JPanel yearPanel = new JPanel(); yearMonthPanel.add(yearPanel, BorderLayout.WEST); yearPanel.setLayout(new BorderLayout()); yearPanel.add(yearsLabel, BorderLayout.WEST); yearPanel.add(yearsSpinner, BorderLayout.CENTER); monthsLabel = new JLabel("Month: "); monthsComboBox = new JComboBox(); for (int i = 1; i <= 12; i++) { monthsComboBox.addItem(new Integer(i)); } monthsComboBox.setSelectedIndex(calendar.get(Calendar.MONTH)); monthsComboBox.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { int day = calendar.get(Calendar.DAY_OF_MONTH); calendar.set(Calendar.DAY_OF_MONTH, 1); calendar.set(Calendar.MONTH, monthsComboBox.getSelectedIndex()); int maxDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH); calendar.set(Calendar.DAY_OF_MONTH, day > maxDay ? maxDay : day); updateView(); } }); JPanel monthPanel = new JPanel(); yearMonthPanel.add(monthPanel, BorderLayout.EAST); monthPanel.setLayout(new BorderLayout()); monthPanel.add(monthsLabel, BorderLayout.WEST); monthPanel.add(monthsComboBox, BorderLayout.CENTER); daysModel = new AbstractTableModel() { public int getRowCount() { return 7; } public int getColumnCount() { return 7; } public Object getValueAt(int row, int column) { if (row == 0) { return getHeader(column); } row--; Calendar calendar = (Calendar) MyCalendar.this.calendar.clone(); calendar.set(Calendar.DAY_OF_MONTH, 1); int dayCount = calendar.getActualMaximum(Calendar.DAY_OF_MONTH); int moreDayCount = calendar.get(Calendar.DAY_OF_WEEK) - 1; int index = row * 7 + column; int dayIndex = index - moreDayCount + 1; if (index < moreDayCount || dayIndex > dayCount) { return null; } else { return new Integer(dayIndex); } } }; daysTable = new CalendarTable(daysModel, calendar); daysTable.setCellSelectionEnabled(true); daysTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); daysTable.setDefaultRenderer(daysTable.getColumnClass(0), new TableCellRenderer() { public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { String text = (value == null) ? "" : value.toString(); JLabel cell = new JLabel(text); cell.setOpaque(true); if (row == 0) { cell.setForeground(headerForeground); cell.setBackground(headerBackground); } else { if (isSelected) { cell.setForeground(selectedForeground); cell.setBackground(selectedBackground); } else { cell.setForeground(foreground); cell.setBackground(background); } } return cell; } }); updateView(); cPane.add(daysTable, BorderLayout.CENTER); } public static String getHeader(int index) { switch (index) { case 0: return WEEK_SUN; case 1: return WEEK_MON; case 2: return WEEK_TUE; case 3: return WEEK_WED; case 4: return WEEK_THU; case 5: return WEEK_FRI; case 6: return WEEK_SAT; default: return null; } } public void updateView() { daysModel.fireTableDataChanged(); daysTable.setRowSelectionInterval(calendar.get(Calendar.WEEK_OF_MONTH), calendar.get(Calendar.WEEK_OF_MONTH)); daysTable.setColumnSelectionInterval(calendar.get(Calendar.DAY_OF_WEEK) - 1, calendar.get(Calendar.DAY_OF_WEEK) - 1); } public static class CalendarTable extends JTable { private Calendar calendar; public CalendarTable(TableModel model, Calendar calendar) { super(model); this.calendar = calendar; } public void changeSelection(int row, int column, boolean toggle, boolean extend) { super.changeSelection(row, column, toggle, extend); if (row == 0) { return; } Object obj = getValueAt(row, column); if (obj !e69da5e6ba907a6431333264623165= null) { calendar.set(Calendar.DAY_OF_MONTH, ((Integer)obj).intValue()); } } } public static void main(String[] args) { JFrame frame = new JFrame("Calendar Application"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); MyCalendar myCalendar = new MyCalendar(); myCalendar.init(); frame.getContentPane().add(myCalendar); frame.setSize(240, 172); frame.show(); } }

额 好撒!

等着!!

java 编写一个日历应该不难吧

我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
请问在刚开业酒店做出纳每天应该做些什么事?
怎样快递笔记本
普祥里社区居委会地址在哪,我要去那里办事
身高:165 22岁的女孩子 体重多少算好看 ? 5
彼岸Faramita清吧地址在什么地方,想过去办事
在1909年中国人自行设计和施工的首条铁路通车
取室的意思是什么啊?知道的请说下!
皇岗口岸到虎门怎么坐车
鱼溪鱼川味怎样做?
能量和能有区别吗?有这样两句话:物体能对外做
落户口必须要全家一起落吗?
为什么ML后第二天大腿酸酸的
国宜北里社区居委会地址好找么,我有些事要过
嘉美醇红酒坊地址在什么地方,想过去办事
十一想参观葛洲坝。。应该怎么办??有什么注
推荐资讯
.net什么版本的和哪个操作系统?哪个版本?搭配
第一次做,结果还没进去就射出来了,有些射到
【坑距】马桶各个坑距分别用什么字母表示
先3题,一会还有,麻烦了
中国工商银行24小时自助银行(横街分理处)(横
风火轮轮滑运动会馆地址有知道的么?有点事想
山西哪里能直接办理到真的电工证焊工证?花钱
瑞康体检(红十字体检中心)地址在什么地方,我
羊年11.01出生的孩子属啥命
“低保家庭”和“低收入家庭”的区别
诸葛一中是不是初中每年老师都会换
大会堂休闲茶楼地址在什么地方,我要处理点事
正方形一边上任一点到这个正方形两条对角线的
阴历怎么看 ?