📄 datebean.java~7~
字号:
package chapter8;
import java.util.*;
import java.awt.*;
import javax.swing.*;
public class DateBean extends JLabel{
private Calendar c = Calendar.getInstance();
private int m, d, y, e;
private String month, day, year, era, month_str;
// three of the five properties surfaced by the bean. Font and Background
// are the other two and they are inherited from the parent object and don't
// need local representation
private boolean useMonthString;
private Color fontColor;
private int style;
private int align;
private boolean inverse;
// constants used by bean and it's BeanInfo and Customizer
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"};
public DateBean() {
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;
}
/**
* fontColor makes more descriptive sense then foreground and we'll hide
* foreground with BeanInfo
*
* @param newFontColor Color
*/
public void setFontColor(Color newFontColor) {
super.setForeground(newFontColor);
}
public Color getFontColor() {
return super.getForeground();
}
public void setFont(Font newFont) {
super.setFont(newFont);
}
public Font getFont() {
return super.getFont();
}
/**
* just overridden from the parent
*
* @param newBackground Color
*/
public void setBackground(Color newBackground) {
super.setBackground(newBackground);
}
public Color getBackground() {
return super.getBackground();
}
/**
* style is handled in the designer with a new Enumeration Property from the
* BeanInfo
*
* @param newStyle int
*/
public void setStyle(int newStyle) {
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 x ){
useMonthString = x;
this.setStyle(this.getStyle());
}
public void setInverse(boolean inverse) {
this.inverse = inverse;
if(inverse)
{
super.setBackground(Color.RED);
}
else
{
super.setBackground(Color.WHITE);
}
}
public void setAlign(int align)
{
this.align = align;
if (align == 1) {
super.setHorizontalAlignment(LEFT);
}
else if(align == 2)
{
super.setHorizontalAlignment(CENTER);
}
else
{
super.setHorizontalAlignment(RIGHT);
}
}
public boolean getUseMonthString(){
return useMonthString;
}
public boolean isInverse() {
return inverse;
}
public int getAlign()
{
return align;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -