您好,欢迎光临本网站![请登录][注册会员]  
文件名称: java日历控件java
  所属分类: Java
  开发工具:
  文件大小: 2kb
  下载次数: 0
  上传时间: 2010-07-09
  提 供 者: amb***
 详细说明: import java.awt.BorderLayout; import java.awt.Color; import java.awt.Component; import java.awt.Container; import java.awt.FlowLayout; import java.awt.Font; import java.awt.Graphics; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.FocusEvent; import java.awt.event.FocusListener; import java.util.Calendar; import javax.swing.JApplet; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax. swing.JSpinner; import javax.swing.JTable; import javax.swing.JTextField; import javax.swing.ListSelectionModel; import javax.swing.Timer; import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeListener; import javax.swing.table.AbstractTableModel; import javax.swing.table.TableCellRenderer; import javax.swing.table.TableModel; //日历 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.yellow; 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; int delay = 1000; public MyCalendar() { cPane = (JPanel) getContentPane(); } public void init() { setSize(350, 300); cPane.setLayout(new BorderLayout()); 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 9; } 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); // 窗体添加事件监听,监听秒表的触发 ActionListener taskPerformer = new ActionListener() { public void actionPerformed(ActionEvent evt) { repaint(); } }; new Timer(delay, taskPerformer).start(); } public void paint(Graphics g) { int hh, mm, ss; Calendar now; String st; // 获取时间 now = Calendar.getInstance(); hh = now.get(Calendar.HOUR_OF_DAY);// 小时 mm = now.get(Calendar.MINUTE);// 分钟 ss = now.get(Calendar.SECOND);// 秒 g.setColor(Color.WHITE); g.fillRect(5, 250, 150, 30);// 填充的矩形 g.setColor(Color.BLACK); if (hh < 10) st = "0" + hh; else st = "" + hh; if (mm < 10) st = st + ":0" + mm; else st = st + ":" + mm; if (ss < 10) st = st + ":0" + ss; else st = st + ":" + ss; { g.setFont(new Font("华文楷体", Font.BOLD, 16)); g.drawString("系统时间:" + st, 10, 270); } } 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); daysTable.setColumnSelectionInterval(0,0); } 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); MyCalendar myCalendar = new MyCalendar(); 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 { private JTextField text; private JSpinner spinner; private Timer timer; private JButton button; public void focusGained(FocusEvent e) // 获得焦点时 { if (e.getSource() == text) { timer.stop(); } } public void focusLost(FocusEvent e) // 失去焦点时 { if (e.getSource() == text) { timer.restart(); } } public void stateChanged(ChangeEvent e) { if (e.getSource() == spinner) { timer.setDelay(new Integer("" + spinner.getValue())); // 设置延时的时间间隔 } } public void actionPerformed(ActionEvent e) // 定时器定时执行事件 { if (e.getSource() == button) ; else { String temp = text.getText(); temp = temp.substring(1) + temp.substring(0, 1); text.setText(temp); } } public void buttondown(ActionEvent e) // 单击事件 { if (e.getSource() == button) { } ; } } } ...展开收缩
(系统自动生成,下载前可以参看下载内容)

下载文件列表

相关说明

  • 本站资源为会员上传分享交流与学习,如有侵犯您的权益,请联系我们删除.
  • 本站是交换下载平台,提供交流渠道,下载内容来自于网络,除下载问题外,其它问题请自行百度
  • 本站已设置防盗链,请勿用迅雷、QQ旋风等多线程下载软件下载资源,下载后用WinRAR最新版进行解压.
  • 如果您发现内容无法下载,请稍后再次尝试;或者到消费记录里找到下载记录反馈给我们.
  • 下载后发现下载的内容跟说明不相乎,请到消费记录里找到下载记录反馈给我们,经确认后退回积分.
  • 如下载前有疑问,可以通过点击"提供者"的名字,查看对方的联系方式,联系对方咨询.
 相关搜索: java
 输入关键字,在本站1000多万海量源码库中尽情搜索: