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(\ 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() {
x
public int getRowCount() { return 9; }
public int getColumnCount() { return 7; }
public Object getValueAt(int row, int column) { if (row == 0) {
return getHeader(column); }
row--; Calendar calendar = (Calendar) zhoutao.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);
xi
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),
xii
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 != null) {
calendar.set(Calendar.DAY_OF_MONTH, ((Integer)obj).intValue()); } }
}
public static void main(String[] args) { JFrame frame = new JFrame(\简易时间日历\
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); zhoutao myCalendar = new zhoutao(); myCalendar.init();
frame.getContentPane().add(myCalendar); frame.setLocation(330,80); frame.setSize(360, 212);
frame.setVisible(true); } //滚动字
public static class RollbyJFrame extends JFrame implements ActionListener, FocusListener, javax.swing.event.ChangeListener {
xiii
private JTextField text; private JSpinner spinner; private Timer timer; private JButton button;
public RollbyJFrame() {
super(\滚动时间\
this.setSize(360,100);
this.setBackground(java.awt.Color.lightGray); this.setLocation(700,120); Container c=getContentPane();
JButton button=new JButton(\修改速度\ this.add(button,\
button.addActionListener(this); Calendar now = Calendar.getInstance(); int hour=now.get(Calendar.HOUR);
int minute=now.get(Calendar.MINUTE); int year=now.get(Calendar.YEAR); int month=now.get(Calendar.MONTH);
int day=now.get(Calendar.DAY_OF_MONTH);
text = new JTextField(\ Hello 当前时间是: \ this.add(text,\
text.addFocusListener(this); //注册焦点事件监听器
timer = new Timer(136,this); timer.start(); JPanel panel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
this.add(panel,\ spinner = new JSpinner();
spinner.setValue(timer.getDelay()); panel.add(spinner);
spinner.addChangeListener(this); this.setVisible(true); }
public void focusGained(FocusEvent e) //获得焦点时 {
if (e.getSource()==text) {
timer.stop(); } }
public void focusLost(FocusEvent e) //失去
xiv
百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说综合文库java简单日历(3)在线全文阅读。
相关推荐: