📄 datejavabean.java~12~
字号:
package javabeantestapp;import java.awt.*;import javax.swing.JLabel;import java.util.*;public class DateJavaBean extends JLabel { BorderLayout borderLayout1 = new BorderLayout(); private java.awt.Color fontColor; private int style; private java.awt.Font font; 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 DateJavaBean() { 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 = Boolean.FALSE; } public java.awt.Color getFontColor() { return fontColor; } public void setFontColor(java.awt.Color fontColor) { this.fontColor = fontColor; super.setForeground(fontColor); } public int getStyle() { return style; } 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.booleanValue()) this.setText(month_str + " " + day + ", " + year); else this.setText(month + " / " + day + " / " + year); break; case MONTH_DAY_YEAR_ERA: if(useMonthString.booleanValue()) this.setText(month_str + " " + day + ", " + year + " " + era); else this.setText(month + " / " + day + " / " + year + " " + era); break; case YEAR_MONTH_DAY: if(useMonthString.booleanValue()) this.setText(year + " " + month_str + " " + day); else this.setText(year + " / " + month + " / " + day); break; case MONTH_YEAR: if(useMonthString.booleanValue()) this.setText(month_str + " " + year); else this.setText(month + " " + year); break; case DAY_MONTH_YEAR: if(useMonthString.booleanValue()) this.setText(day + " " + month_str + " " + year); else this.setText(day + " / " + month + " / " + year); break; default: this.setText("invalid"); } } } public Boolean getUseMonthString() { return useMonthString; } public void setUseMonthString(Boolean useMonthString) { this.useMonthString = useMonthString; this.setStyle(this.getStyle()); } public java.awt.Font getFont() { return font; } public void setFont(java.awt.Font font) { this.font = font; } 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 + -