JAVA日历
效果图如下:
1 import java.awt.*;2 import java.awt.event.*;
3 import java.util.*;
4
5 import javax.swing.*;
6 import javax.swing.event.*;
7 import javax.swing.table.*;
8
9 public class MyCalendar extends JApplet
10 {
11 //定义星期天到星期六
12 public static final String WEEK_SUN = "周日";
13 public static final String WEEK_MON = "周一";
14 public static final String WEEK_TUE = "周二";
15 public static final String WEEK_WED = "周三";
16 public static final String WEEK_THU = "周四";
17 public static final String WEEK_FRI = "周五";
18 public static final String WEEK_SAT = "周六";
19 //设置背景颜色
20 public static final Color background = Color.white;
21 //设置前景颜色
22 public static final Color foreground = Color.black;
23 //设置题头星期的背景颜色和前景颜色
24 public static final Color headerBackground = Color.blue;
25 public static final Color headerForeground = Color.white;
26 //设置被选中的日期的背景颜色和前景颜色
27 public static final Color selectedBackground = Color.red;
28 public static final Color selectedForeground = Color.white;
29
30 private JPanel cPane;
31 private JLabel yearsLabel;//年
32 private JSpinner yearsSpinner;//年调控
33 private JLabel monthsLabel;//月
34 private JComboBox monthsComboBox;//月下拉框
35 private JTable daysTable;//用来显示日期的table
36 private AbstractTableModel daysModel;
37 private Calendar calendar;
38 //构造方法初始化panel
39
40 public MyCalendar()
41 {
42 cPane = (JPanel) getContentPane();
43 }
44 //初始化,对所有的空间进行布局
45 public void init()
46 {
47
48 cPane.setLayout(new BorderLayout()); //使用Border布局管理器
49 calendar = Calendar.getInstance(); //获取当前时间
50 calendar = Calendar.getInstance();
51 yearsLabel = new JLabel("Year: ");
52 yearsSpinner = new JSpinner();
53 yearsSpinner.setEditor(new JSpinner.NumberEditor(yearsSpinner,"0000"));
54 yearsSpinner.setValue(new Integer(calendar.get(Calendar.YEAR)));
55 //增加监听 监听年份的改变
56 yearsSpinner.addChangeListener(new ChangeListener()
57 {
58 public void stateChanged(ChangeEvent changeEvent)
59 {
60 int day = calendar.get(Calendar.DAY_OF_MONTH);
61 calendar.set(Calendar.DAY_OF_MONTH, 1);
62 calendar.set(Calendar.YEAR,((Integer)yearsSpinner.getValue()).intValue());
63 int maxDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
64 calendar.set(Calendar.DAY_OF_MONTH, day>maxDay?maxDay:day);
65 updateView();
66 }
67 });
68
69 JPanel yearMonthPanel = new JPanel();
70 cPane.add(yearMonthPanel,BorderLayout.NORTH);
71 yearMonthPanel.setLayout(new BorderLayout());
72 yearMonthPanel.add(new JPanel(),BorderLayout.CENTER);
73 JPanel yearPanel = new JPanel();
74 yearMonthPanel.add(yearPanel,BorderLayout.WEST);
75 yearPanel.setLayout(new BorderLayout());
76 yearPanel.add(yearsLabel,BorderLayout.WEST);
77 yearPanel.add(yearsSpinner,BorderLayout.CENTER);
78
79 monthsLabel = new JLabel("Month: ");
80 //向月份下拉框中增加内容
81 monthsComboBox = new JComboBox();
82 for(int i=1; i<=12; i++)
83 {
84 monthsComboBox.addItem(new Integer(i));
85 }
86 monthsComboBox.setSelectedIndex(calendar.get(Calendar.MONTH));
87 //监听月份的改变
88 monthsComboBox.addActionListener(new ActionListener() {
89
90 @Override
91 public void actionPerformed(ActionEvent actionEvevt) {
92 // TODO Auto-generated method stub
93 int day = calendar.get(Calendar.DAY_OF_MONTH);
94 calendar.set(Calendar.DAY_OF_MONTH,1);
95 calendar.set(Calendar.MONTH,monthsComboBox.getSelectedIndex());
96 int maxDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
97 calendar.set(Calendar.DAY_OF_MONTH,day>maxDay?maxDay:day);
98 updateView();
99 }
100 });
101
102
103 JPanel monthPanel = new JPanel();
104 yearMonthPanel.add(monthPanel,BorderLayout.EAST);
105 monthPanel.setLayout(new BorderLayout());
106 monthPanel.add(monthsLabel,BorderLayout.WEST);
107 monthPanel.add(monthsComboBox,BorderLayout.CENTER);
108
109 daysModel = new AbstractTableModel() {
110
111 @Override
112 public Object getValueAt(int row, int column) {
113 // TODO Auto-generated method stub
114 if(row==0)
115 {
116 return getHeader(column);
117 }
118 row--;
119 Calendar calendar = (Calendar)MyCalendar.this.calendar.clone();
120 calendar.set(Calendar.DAY_OF_MONTH,1);
121 int dayCount = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
122 int moreDayCount = calendar.get(Calendar.DAY_OF_WEEK)-1; //每个月第一天从哪一列开始
123 int index = row*7 + column;
124 int dayIndex = index - moreDayCount + 1;
125 if(index<moreDayCount||dayIndex>dayCount)
126 {
127 return null;
128 }
129 else
130 {
131 return new Integer(dayIndex);
132 }
133 }
134 //设置行7
135 @Override
136 public int getRowCount() {
137 // TODO Auto-generated method stub
138 return 7;
139 }
140 //设置列7
141 @Override
142 public int getColumnCount() {
143 // TODO Auto-generated method stub
144 return 7;
145 }
146 };
147 daysTable = new CalendarTable(daysModel,calendar);
148 //设置每个cell可以被选中
149 daysTable.setCellSelectionEnabled(true);
150 daysTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
151 daysTable.setDefaultRenderer(daysTable.getColumnClass(0),new TableCellRenderer() {
152 @Override
153 public Component getTableCellRendererComponent(JTable table,Object value,
154 boolean isSelected,boolean hasFocus,int row,int column) {
155 String text = (value == null )? "" : value.toString();
156 JLabel cell = new JLabel(text);
157 cell.setOpaque(true);
158 if(row == 0)
159 {
160 cell.setForeground(headerForeground);
161 cell.setBackground(headerBackground);
162 }
163 else
164 {
165 if(isSelected)
166 {
167 cell.setForeground(selectedForeground);
168 cell.setBackground(selectedBackground);
169 }
170 else
171 {
172 cell.setForeground(foreground);
173 cell.setBackground(background);
174 }
175 }
176 return cell;
177 }
178 });
179 updateView();
180 cPane.add(daysTable,BorderLayout.CENTER);
181 }
182
183 public static String getHeader(int index)
184 {
185 switch(index)
186 {
187 case 0:
188 return WEEK_SUN;
189 case 1:
190 return WEEK_MON;
191 case 2:
192 return WEEK_TUE;
193 case 3:
194 return WEEK_WED;
195 case 4:
196 return WEEK_THU;
197 case 5:
198 return WEEK_FRI;
199 case 6:
200 return WEEK_SAT;
201 default:
202 return null;
203 }
204 }
205
206 public void updateView()
207 {
208 daysModel.fireTableDataChanged();
209 daysTable.setRowSelectionInterval(calendar.get(Calendar.WEEK_OF_MONTH),
210 calendar.get(Calendar.WEEK_OF_MONTH));
211 daysTable.setColumnSelectionInterval(calendar.get(Calendar.DAY_OF_WEEK)-1,
212 calendar.get(Calendar.DAY_OF_WEEK)-1);
213 }
214 //设置日历的table
215 public static class CalendarTable extends JTable
216 {
217 private Calendar calendar;
218 public CalendarTable(TableModel model,Calendar calendar)
219 {
220 super(model);
221 this.calendar = calendar;
222 }
223 public void changeSelection(int row,int column,boolean toggle,boolean extend)
224 {
225 super.changeSelection(row, column, toggle, extend);
226 if(row == 0)
227 {
228 return;
229 }
230 Object obj = getValueAt(row,column);
231 if(obj != null)
232 {
233 calendar.set(Calendar.DAY_OF_MONTH,((Integer)obj).intValue());
234 }
235 }
236 }
237 //让applet作为一个可执行的程序来运行
238 public static void main(String[] args)
239 {
240 JFrame frame = new JFrame("ShuBing日历");
241 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
242 MyCalendar myCalendar = new MyCalendar();
243 myCalendar.init();
244 frame.getContentPane().add(myCalendar);
245 frame.setSize(300,200);
246 frame.setVisible(true);
247 }
248 }
以上是 JAVA日历 的全部内容, 来源链接: utcz.com/z/390816.html