📄 datebean.java
字号:
package beantest;import java.awt.*;import java.util.*;import javax.swing.JLabel;/** * Title: 制作一个JavaBean * Description: 这个工程中我们制作了一个用于日期显示的JavaBean * Copyright: Copyright (c) 2001 * Company: * @author * @version 1.0 */public class DateBean extends JLabel { BorderLayout borderLayout1 = new BorderLayout(); private Color fontColor; private int style; private boolean useMonthString; private Calendar c = Calendar.getInstance(); private int m, d, y, e; private String month, day, year, era, month_str; public static final int MONTH_DAY_YEAR = 1; public static final int MONTH_DAY_YEAR_ERA = 2; public static final int YEAR_MONTH_DAY = 3; public static final int MONTH_YEAR = 4; public static final int DAY_MONTH_YEAR = 5; public static final String[] allMonths = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; private transient Vector styleListeners; public DateBean() { try { jbInit(); } catch(Exception ex) { ex.printStackTrace(); } } private void jbInit() throws Exception { this.setLayout(borderLayout1); m = c.get(Calendar.MONTH) + 1; d = c.get(Calendar.DATE); y = c.get(Calendar.YEAR); e = c.get(Calendar.ERA); if(m <= 9) month = "0" + String.valueOf(m); else month = String.valueOf(m); if(d <=9) day = "0" + String.valueOf(d); else day = String.valueOf(d); year = String.valueOf(y); month_str = allMonths[m - 1]; // AD or BC -- it might matter when Java builds time machines! if(e == 1) era = "AD"; else era = "BC"; setStyle(MONTH_DAY_YEAR_ERA); useMonthString = false; } public void setFontColor(Color newFontColor) { super.setForeground(newFontColor); } public Color getFontColor() { return super.getForeground() ; } public void setStyle(int newStyle) { if(newStyle!=this.style ){ StyleEvent e=new StyleEvent(this); e.setNewStyle(newStyle); e.setOldStyle(this.style); this.fireStyleChanged(e); this.style = newStyle; switch(style){ case MONTH_DAY_YEAR: if(useMonthString) this.setText(month_str + " " + day + ", " + year); else this.setText(month + " / " + day + " / " + year); break; case MONTH_DAY_YEAR_ERA: if(useMonthString) this.setText(month_str + " " + day + ", " + year + " " + era); else this.setText(month + " / " + day + " / " + year + " " + era); break; case YEAR_MONTH_DAY: if(useMonthString) this.setText(year + " " + month_str + " " + day); else this.setText(year + " / " + month + " / " + day); break; case MONTH_YEAR: if(useMonthString) this.setText(month_str + " " + year); else this.setText(month + " " + year); break; case DAY_MONTH_YEAR: if(useMonthString) this.setText(day + " " + month_str + " " + year); else this.setText(day + " / " + month + " / " + year); break; default: this.setText("invalid"); } } } public int getStyle() { return style; } public void setUseMonthString(boolean useMonthString) { this.useMonthString = useMonthString; this.setStyle(this.getStyle()); } public boolean isUseMonthString() { return useMonthString; } public void setFont(Font newfont) { super.setFont(newfont); } public Font getFont() { return super.getFont(); } public void setBackground(Color newbackground) { super.setBackground(newbackground); } public Color getBackground() { return super.getBackground(); } public synchronized void removeStyleListener(StyleListener l) { if (styleListeners != null && styleListeners.contains(l)) { Vector v = (Vector) styleListeners.clone(); v.removeElement(l); styleListeners = v; } } public synchronized void addStyleListener(StyleListener l) { Vector v = styleListeners == null ? new Vector(2) : (Vector) styleListeners.clone(); if (!v.contains(l)) { v.addElement(l); styleListeners = v; } } protected void fireStyleChanged(StyleEvent e) { if (styleListeners != null) { Vector listeners = styleListeners; int count = listeners.size(); for (int i = 0; i < count; i++) { ((StyleListener) listeners.elementAt(i)).styleChanged(e); } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -