• PUDN用户
    了解作者
  • Java
    开发工具
  • 16KB
    文件大小
  • zip
    文件格式
  • 0
    收藏次数
  • 1 积分
    下载积分
  • 13
    下载次数
  • 2005-09-03 02:53
    上传日期
java时间控件
Datepick.zip
  • src
  • icon
  • icon1.gif
    376B
  • icon2.gif
    382B
  • oriflamme
  • component
  • DatePicker.java
    5.2KB
  • DateChooser.java
    9.2KB
  • META-INF
  • MANIFEST.MF
    71B
  • icon
  • icon1.gif
    376B
  • icon2.gif
    382B
  • oriflamme
  • component
  • icon
  • icon1.gif
    376B
  • icon2.gif
    382B
  • DatePicker.class
    5.9KB
  • DateChooser.class
    7.9KB
  • icon
  • icon1.gif
    376B
  • icon2.gif
    382B
  • www.pudn.com.txt
    218B
内容介绍
package oriflamme.component; import java.awt.*; import java.awt.event.*; import java.util.*; import javax.swing.*; import javax.swing.border.*; public class DateChooser extends JDialog implements ItemListener, ActionListener { private static final String[] MONTHS = new String[]{ "1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月" }; private static final String[] DAYS = new String[]{ "日", "一", "二", "三", "四", "五", "六" }; public static final Color BUTTON_BLACK = new Color(225, 235, 255); public static final Color PANRL_BLACK = new Color(245, 254, 245); public static final Color WEEK_BLACK = new Color(130, 163, 243); private static final Color DAYS_FOREGROUND = Color.BLACK; private static final Color DAYS_BACKGROUND = BUTTON_BLACK; private static final Color SELECTED_DAY_FOREGROUND = Color.WHITE; private static final Color SELECTED_DAY_BACKGROUND = new Color(60, 60, 160); private static final Border EMPTY_BORDER = BorderFactory.createEmptyBorder(0, 0, 0, 0); private static final int FIRST_YEAR = 1900; private static final int LAST_YEAR = 2100; private GregorianCalendar calendar; private JButton[][] days; private JPanel daysGrid; private JComboBox month; private JComboBox year; private JComboBox minute; private JComboBox hour; private Dimension mDimension = new Dimension(45, 20); private int offset; private int lastDay; private JButton day; private JButton curBtn; private boolean okClicked; //构建函数 public DateChooser(Dialog owner, String title) { super(owner, title, true); construct(); } public DateChooser(Dialog owner) { super(owner, true); construct(); } public DateChooser(Frame owner, String title) { super(owner, title, true); construct(); } public DateChooser(Frame owner) { super(owner, true); construct(); } private void construct() { calendar = new GregorianCalendar(); //this.setSize(400, 250); //this.setUndecorated(true); // month = new JComboBox(MONTHS); month.addItemListener(this); // year = new JComboBox(); for (int i = FIRST_YEAR; i <= LAST_YEAR; i++) year.addItem(Integer.toString(i)); year.addItemListener(this); // minute = new JComboBox(); for (int i = 0; i < 6; i++) minute.addItem(Integer.toString(i * 10) + "分"); // hour = new JComboBox(); for (int i = 0; i < 24; i++) hour.addItem(Integer.toString(i) + "时"); // days = new JButton[7][7]; for (int i = 0; i < 7; i++) { days[0][i] = new JButton(DAYS[i]); days[0][i].setPreferredSize(mDimension); days[0][i].setBorder(EMPTY_BORDER); days[0][i].setBackground(WEEK_BLACK); days[0][i].setForeground(Color.WHITE); days[0][i].setFocusPainted(false); days[0][i].setContentAreaFilled(false); } curBtn = new JButton("还原"); curBtn.addActionListener(this); initElement(); JPanel monthYear = new JPanel(); monthYear.add(year); monthYear.add(month); monthYear.add(hour); monthYear.add(minute); monthYear.add(curBtn); daysGrid = new JPanel(new GridLayout(7, 7, 1, 0)); for (int i = 0; i < 7; i++) for (int j = 0; j < 7; j++) { daysGrid.add(days[i][j]); } daysGrid.setBackground(new Color(60, 60, 160)); daysGrid.setBorder(BorderFactory.createLineBorder(new Color(60, 60, 160), 2)); JPanel daysPanel = new JPanel(); daysPanel.add(daysGrid); Container dialog = getContentPane(); dialog.add("North", monthYear); dialog.add("Center", daysPanel); month.setBackground(BUTTON_BLACK); //month.setBorder(BorderFactory.createLineBorder(color4, 1)); year.setBackground(BUTTON_BLACK); //year.setBorder(BorderFactory.createLineBorder(color4, 1)); minute.setBackground(BUTTON_BLACK); //minute.setBorder(BorderFactory.createLineBorder(color4, 1)); hour.setBackground(BUTTON_BLACK); //hour.setBorder(BorderFactory.createLineBorder(color4, 1)); monthYear.setBackground(PANRL_BLACK); daysPanel.setBackground(PANRL_BLACK); curBtn.setBackground(BUTTON_BLACK); pack(); setResizable(false); } private void initElement() { for (int i = 1; i < 7; i++) for (int j = 0; j < 7; j++) { days[i][j] = new JButton(""); days[i][j].addActionListener(this); days[i][j].setBackground(DAYS_BACKGROUND); days[i][j].setForeground(DAYS_FOREGROUND); days[i][j].setBorder(EMPTY_BORDER); days[i][j].setPreferredSize(mDimension); days[i][j].setFocusPainted(false); } } private int getSelectedDay() { if (day == null) return -1; try { return Integer.parseInt(day.getText()); } catch (NumberFormatException e) { } return -1; } private void setSelected(JButton newDay) { if (day != null) { day.setForeground(DAYS_FOREGROUND); day.setBackground(DAYS_BACKGROUND); } day = newDay; day.setForeground(SELECTED_DAY_FOREGROUND); day.setBackground(SELECTED_DAY_BACKGROUND); } private void setSelected(int newDay) { setSelected(days[(newDay + offset - 1) / 7 + 1][(newDay + offset - 1) % 7]); } private void update() { int iday = getSelectedDay(); for (int i = 0; i < 7; i++) { days[1][i].setText(""); days[5][i].setText(""); days[6][i].setText(""); } calendar.set(Calendar.DATE, 1); calendar.set(Calendar.MONTH, month.getSelectedIndex() + Calendar.JANUARY); calendar.set(Calendar.YEAR, year.getSelectedIndex() + FIRST_YEAR); offset = calendar.get(Calendar.DAY_OF_WEEK) - Calendar.SUNDAY; lastDay = calendar.getActualMaximum(Calendar.DATE); for (int i = 0; i < lastDay; i++) { days[(i + offset) / 7 + 1][(i + offset) % 7].setText(String.valueOf(i + 1)); days[(i + offset) / 7 + 1][(i + offset) % 7].setEnabled(true); } if (iday != -1) { if (iday > lastDay) iday = lastDay; setSelected(iday); } } public void actionPerformed(ActionEvent e) { JButton day = (JButton) e.getSource(); if (day == curBtn) { calendar.setTime(new Date()); int _day = calendar.get(Calendar.DATE); int _month = calendar.get(Calendar.MONTH); int _year = calendar.get(Calendar.YEAR); year.setSelectedIndex(_year - FIRST_YEAR); month.setSelectedIndex(_month - Calendar.JANUARY); setSelected(_day); } else if (!
评论
    相关推荐