上次公司需要,搞了一个swing的aapplication的demo,重新弄了一般swing,可惜最后无疾而终,可悲.....
最近离职,把以前的东西整理了一下,觉得多少有点用处,写下算是悼念吧!
这是其中日期选择控件的代码,改编自网上某个程序(记不得了),界面如下所示:

源代码:
//datepicker.java
package com.kxsoft.component;
import java.awt.*;
import java.awt.event.*;
import java.util.gregoriancalendar;
import java.util.date;
import java.util.calendar;
import java.text.dateformat;
import java.text.fieldposition;
import javax.swing.*;
import javax.swing.plaf.borderuiresource;
public final class datepicker extends jpanel {
private static final long serialversionuid = 1l;
private static final int startx = 10;
private static final int starty = 60;
private static final font smallfont = new font("dialog", font.plain, 10);
private static final font largefont = new font("dialog", font.plain, 12);
private static final insets insets = new insets(2, 2, 2, 2);
private static final color highlight = new color(255, 255, 204);
private static final color white = new color(255, 255, 255);
private static final color gray = new color(204, 204, 204);
private component selectedday = null;
private gregoriancalendar selecteddate = null;
private gregoriancalendar originaldate = null;
private boolean hideonselect = true;
private final jbutton backbutton = new jbutton();
private final jlabel monthandyear = new jlabel();
private final jbutton forwardbutton = new jbutton();
private final jlabel[] dayheadings = new jlabel[]{
new jlabel("日"),
new jlabel("一"),
new jlabel("二"),
new jlabel("三"),
new jlabel("四"),
new jlabel("五"),
new jlabel("六")};
private final jlabel[][] daysinmonth = new jlabel[][]{
{new jlabel(),
new jlabel(),
new jlabel(),
new jlabel(),
new jlabel(),
new jlabel(),
new jlabel()},
{new jlabel(),
new jlabel(),
new jlabel(),
new jlabel(),
new jlabel(),
new jlabel(),
new jlabel()},
{new jlabel(),
new jlabel(),
new jlabel(),
new jlabel(),
new jlabel(),
new jlabel(),
new jlabel()},
{new jlabel(),
new jlabel(),
new jlabel(),
new jlabel(),
new jlabel(),
new jlabel(),
new jlabel()},
{new jlabel(),
new jlabel(),
new jlabel(),
new jlabel(),
new jlabel(),
new jlabel(),
new jlabel()},
{new jlabel(),
new jlabel(),
new jlabel(),
new jlabel(),
new jlabel(),
new jlabel(),
new jlabel()}
};
private final jbutton todaybutton = new jbutton();
private final jbutton cancelbutton = new jbutton();
public datepicker() {
super();
selecteddate = gettoday();
init();
}
public datepicker(final date initialdate) {
super();
if (null == initialdate)
selecteddate = gettoday();
else
(selecteddate = new gregoriancalendar()).settime(initialdate);
originaldate = new gregoriancalendar(
selecteddate.get(calendar.year),
selecteddate.get(calendar.month),
selecteddate.get(calendar.date));
init();
}
public boolean ishideonselect() {
return hideonselect;
}
public void sethideonselect(final boolean hideonselect) {
if (this.hideonselect != hideonselect) {
this.hideonselect = hideonselect;
initbuttons(false);
}
}
public date getdate() {
if (null != selecteddate)
return selecteddate.gettime();
return null;
}
private void init() {
setlayout(new absolutelayout());
this.setminimumsize(new dimension(161, 226));
this.setmaximumsize(getminimumsize());
this.setpreferredsize(getminimumsize());
this.setborder(new borderuiresource.etchedborderuiresource());
backbutton.setfont(smallfont);
backbutton.settext("<");
backbutton.setmargin(insets);
backbutton.setdefaultcapable(false);
backbutton.addactionlistener(new actionlistener() {
public void actionperformed(final actionevent evt) {
onbackclicked(evt);
}
});
add(backbutton, new absoluteconstraints(10, 10, 20, 20));
monthandyear.setfont(largefont);
monthandyear.sethorizontalalignment(jtextfield.center);
monthandyear.settext(formatdatetext(selecteddate.gettime()));
add(monthandyear, new absoluteconstraints(30, 10, 100, 20));
forwardbutton.setfont(smallfont);
forwardbutton.settext(">");
forwardbutton.setmargin(insets);
forwardbutton.setdefaultcapable(false);
forwardbutton.addactionlistener(new actionlistener() {
public void actionperformed(final actionevent evt) {
onforwardclicked(evt);
}
});
add(forwardbutton, new absoluteconstraints(130, 10, 20, 20));
int x = startx;
for (int ii = 0; ii < dayheadings.length; ii++) {
dayheadings[ii].setopaque(true);
dayheadings[ii].setbackground(color.light_gray);
dayheadings[ii].setforeground(color.white);
dayheadings[ii].sethorizontalalignment(jlabel.center);
add(dayheadings[ii], new absoluteconstraints(x, 40, 21, 21));
x += 20;
}
x = startx;
int y = starty;
for (int ii = 0; ii < daysinmonth.length; ii++) {
for (int jj = 0; jj < daysinmonth[ii].length; jj++) {
daysinmonth[ii][jj].setopaque(true);
daysinmonth[ii][jj].setbackground(white);
daysinmonth[ii][jj].setfont(smallfont);
daysinmonth[ii][jj].sethorizontalalignment(jlabel.center);
daysinmonth[ii][jj].settext("");
daysinmonth[ii][jj].addmouselistener(new mouseadapter() {
public void mouseclicked(final mouseevent evt) {
ondayclicked(evt);
}
});
add(daysinmonth[ii][jj], new absoluteconstraints(x, y, 21, 21));
x += 20;
}
x = startx;
y += 20;
}
initbuttons(true);
calculatecalendar();
}
private void initbuttons(final boolean firsttime) {
if (firsttime) {
final dimension buttonsize = new dimension(68, 24);
todaybutton.settext("今天");
todaybutton.setmargin(insets);
todaybutton.setmaximumsize(buttonsize);
todaybutton.setminimumsize(buttonsize);
todaybutton.setpreferredsize(buttonsize);
todaybutton.setdefaultcapable(true);
todaybutton.setselected(true);
todaybutton.addactionlistener(new actionlistener() {
public void actionperformed(final actionevent evt) {
ontoday(evt);
}
});
cancelbutton.settext("取消");
cancelbutton.setmargin(insets);
cancelbutton.setmaximumsize(buttonsize);
cancelbutton.setminimumsize(buttonsize);
cancelbutton.setpreferredsize(buttonsize);
cancelbutton.addactionlistener(new actionlistener() {
public void actionperformed(final actionevent evt) {
oncancel(evt);
}
});
} else {
this.remove(todaybutton);
this.remove(cancelbutton);
}
if (hideonselect) {
add(todaybutton, new absolut
闽公网安备 35060202000074号