import java.awt.BorderLayout;
成都創(chuàng)新互聯(lián)公司長(zhǎng)期為上千家客戶提供的網(wǎng)站建設(shè)服務(wù),團(tuán)隊(duì)從業(yè)經(jīng)驗(yàn)10年,關(guān)注不同地域、不同群體,并針對(duì)不同對(duì)象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺(tái),與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為鳳翔企業(yè)提供專業(yè)的成都網(wǎng)站制作、成都網(wǎng)站建設(shè),鳳翔網(wǎng)站改版等技術(shù)服務(wù)。擁有十多年豐富建站經(jīng)驗(yàn)和眾多成功案例,為您定制開發(fā)。
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Locale;
import java.util.Date;
import java.util.StringTokenizer;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JToggleButton;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
/**
* pTitle: Swing日歷/p
* pDescription: 操作日期/p
* @author duxu2004
* @version 1.0.1
*/
class JCalendar extends JPanel{
//動(dòng)態(tài)表示年月日
private int year=0;
private int month=0;
private int day=0;
//主面板
private JPanel Main = new JPanel();
//日面板
private JPanel jPanelDay = new JPanel();
//月面板
private JPanel jPanelMonth = new JPanel();
//年的輸入位置
private JTextField Year = new JTextField();
//月的輸入位置
private JTextField Month = new JTextField();
//減少月份
private JButton MonthDown = new JButton();
//增加月份
private JButton MonthUp = new JButton();
private JPanel jPanelButton = new JPanel();
//減少年份
private JButton YearDown = new JButton();
//增加年份
private JButton YearUp = new JButton();
//顯示日期的位置
private JLabel Out = new JLabel();
//中國時(shí)區(qū),以后可以從這里擴(kuò)展可以設(shè)置時(shí)區(qū)的功能
private Locale l=Locale.CHINESE;
//主日歷
private GregorianCalendar cal=new GregorianCalendar(l);
//星期面板
private JPanel weekPanel=new JPanel();
//天按鈕組
private JToggleButton[] days=new JToggleButton[42];
//天面板
private JPanel Days = new JPanel();
//標(biāo)示
private JLabel jLabel1 = new JLabel();
private JLabel jLabel2 = new JLabel();
private JLabel jLabel3 = new JLabel();
private JLabel jLabel4 = new JLabel();
private JLabel jLabel5 = new JLabel();
private JLabel jLabel6 = new JLabel();
private JLabel jLabel7 = new JLabel();
//當(dāng)前選擇的天數(shù)按鈕
private JToggleButton cur=null;
//月份天數(shù)數(shù)組,用來取得當(dāng)月有多少天
// 1 2 3 4 5 6 7 8 9 10 11 12
private int[] mm={31,28,31,30,31,30,31,31,30,31,30,31};
//空日期構(gòu)造函數(shù)
public JCalendar() {
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
//帶日期設(shè)置的構(gòu)造函數(shù)
public JCalendar(int year, int month, int day) {
cal.set(year, month, day);
try {
jbInit();
}
catch (Exception e) {
e.printStackTrace();
}
}
//帶日歷輸入的構(gòu)造函數(shù)
public JCalendar(GregorianCalendar calendar) {
cal=calendar;
try {
jbInit();
}
catch (Exception e) {
e.printStackTrace();
}
}
//帶日期輸入的構(gòu)造函數(shù)
public JCalendar(Date date) {
cal.setTime(date);
try {
jbInit();
}
catch (Exception e) {
e.printStackTrace();
}
}
//初始化組件
private void jbInit() throws Exception {
//初始化年、月、日
iniCalender();
this.setLayout(new BorderLayout());
this.setBorder(BorderFactory.createRaisedBevelBorder());
this.setMaximumSize(new Dimension(200, 200));
this.setMinimumSize(new Dimension(200, 200));
this.setPreferredSize(new Dimension(200, 200));
Main.setLayout(new BorderLayout());
Main.setBackground(SystemColor.info);
Main.setBorder(null);
Out.setBackground(Color.lightGray);
Out.setHorizontalAlignment(SwingConstants.CENTER);
Out.setMaximumSize(new Dimension(100, 19));
Out.setMinimumSize(new Dimension(100, 19));
Out.setPreferredSize(new Dimension(100, 19));
jLabel1.setForeground(Color.red);
jLabel1.setHorizontalAlignment(SwingConstants.CENTER);
jLabel1.setHorizontalTextPosition(SwingConstants.CENTER);
jLabel1.setText("日");
jLabel2.setForeground(Color.blue);
jLabel2.setHorizontalAlignment(SwingConstants.CENTER);
jLabel2.setHorizontalTextPosition(SwingConstants.CENTER);
jLabel2.setText("六");
jLabel3.setHorizontalAlignment(SwingConstants.CENTER);
jLabel3.setHorizontalTextPosition(SwingConstants.CENTER);
jLabel3.setText("五");
jLabel4.setHorizontalAlignment(SwingConstants.CENTER);
jLabel4.setHorizontalTextPosition(SwingConstants.CENTER);
jLabel4.setText("四");
jLabel5.setHorizontalAlignment(SwingConstants.CENTER);
jLabel5.setHorizontalTextPosition(SwingConstants.CENTER);
jLabel5.setText("三");
jLabel6.setBorder(null);
jLabel6.setHorizontalAlignment(SwingConstants.CENTER);
jLabel6.setHorizontalTextPosition(SwingConstants.CENTER);
jLabel6.setText("二");
jLabel7.setBackground(Color.lightGray);
jLabel7.setForeground(Color.black);
jLabel7.setBorder(null);
jLabel7.setHorizontalAlignment(SwingConstants.CENTER);
jLabel7.setHorizontalTextPosition(SwingConstants.CENTER);
jLabel7.setText("一");
weekPanel.setBackground(UIManager.getColor("InternalFrame.activeTitleGradient"));
weekPanel.setBorder(BorderFactory.createEtchedBorder());
weekPanel.setLayout(new GridLayout(1,7));
weekPanel.add(jLabel1, null);
weekPanel.add(jLabel7, null);
weekPanel.add(jLabel6, null);
weekPanel.add(jLabel5, null);
weekPanel.add(jLabel4, null);
weekPanel.add(jLabel3, null);
weekPanel.add(jLabel2, null);
MonthUp.setAlignmentX((float) 0.0);
MonthUp.setActionMap(null);
jPanelMonth.setBackground(SystemColor.info);
jPanelMonth.setLayout(new BorderLayout());
jPanelMonth.setBorder(BorderFactory.createEtchedBorder());
Month.setBorder(null);
Month.setHorizontalAlignment(SwingConstants.CENTER);
Month.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(MouseEvent e) {
Month_mouseClicked(e);
}
});
Month.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyPressed(KeyEvent e) {
Month_keyPressed(e);
}
});
MonthDown.setBorder(null);
MonthDown.setText("\u25C4");
MonthDown.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
MonthDown_actionPerformed(e);
}
});
MonthUp.setBorder(null);
MonthUp.setText("\u25BA");
MonthUp.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
MonthUp_actionPerformed(e);
}
});
jPanelButton.setLayout(null);
jPanelButton.setBorder(null);
jPanelButton.addComponentListener(new java.awt.event.ComponentAdapter() {
public void componentResized(java.awt.event.ComponentEvent evt) {
jPanelButtonComponentResized(evt);
}
});
Year.setBorder(BorderFactory.createEtchedBorder());
Year.setMaximumSize(new Dimension(80, 25));
Year.setMinimumSize(new Dimension(80, 25));
Year.setPreferredSize(new Dimension(80, 25));
Year.setHorizontalAlignment(SwingConstants.CENTER);
Year.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(MouseEvent e) {
Year_mouseClicked(e);
}
});
Year.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyPressed(KeyEvent e) {
Year_keyPressed(e);
}
});
YearDown.setBorder(null);
YearDown.setMaximumSize(new Dimension(16, 16));
YearDown.setMinimumSize(new Dimension(16, 16));
YearDown.setPreferredSize(new Dimension(16, 16));
YearDown.setSize(new Dimension(16, 16));
YearDown.setText("▼");
YearDown.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
YearDown_actionPerformed(e);
}
});
YearUp.setBorder(null);
YearUp.setMaximumSize(new Dimension(16, 16));
YearUp.setMinimumSize(new Dimension(16, 16));
YearUp.setPreferredSize(new Dimension(16, 16));
YearUp.setSize(new Dimension(16, 16));
YearUp.setText("▲");
YearUp.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
YearUp_actionPerformed(e);
}
});
jPanelDay.setLayout(new BorderLayout());
Days.setLayout(new GridLayout(6,7));
Days.setBackground(SystemColor.info);
for(int i=0;i42;i++){
days[i]=new JToggleButton();
days[i].setBorder(null);
days[i].setBackground(SystemColor.info);
days[i].setHorizontalAlignment(SwingConstants.CENTER);
days[i].setHorizontalTextPosition(SwingConstants.CENTER);
//days[i].setSize(l,l);
days[i].addActionListener(new java.awt.event.ActionListener(){
public void actionPerformed(ActionEvent e) {
day=Integer.parseInt(((JToggleButton)e.getSource()).getText());
showDate();
showDays();
}
});
Days.add(days[i]);
}
this.add(Main, BorderLayout.NORTH);
this.add(jPanelDay, BorderLayout.CENTER);
this.add(jPanelMonth, BorderLayout.SOUTH);
Main.add(Year, BorderLayout.CENTER);
Main.add(Out, BorderLayout.WEST);
Main.add(jPanelButton, BorderLayout.EAST);
jPanelButton.add(YearUp);
jPanelButton.add(YearDown);
jPanelDay.add(weekPanel,BorderLayout.NORTH);
jPanelDay.add(Days, BorderLayout.CENTER);
jPanelMonth.add(Month, BorderLayout.CENTER);
jPanelMonth.add(MonthDown, BorderLayout.WEST);
jPanelMonth.add(MonthUp, BorderLayout.EAST);
showMonth();
showYear();
showDate();
showDays();
}
//自定義重畫年選擇面板
void jPanelButtonComponentResized(java.awt.event.ComponentEvent evt){
YearUp.setLocation(0,0);
YearDown.setLocation(0,YearUp.getHeight());
jPanelButton.setSize(YearUp.getWidth(),YearUp.getHeight()*2);
jPanelButton.setPreferredSize(new Dimension(YearUp.getWidth(),YearUp.getHeight()*2));
jPanelButton.updateUI();
}
//測(cè)試用
public static void main(String[] args){
JFrame f=new JFrame();
f.setContentPane(new JCalendar());
f.pack();
//f.setResizable(false);
f.show();
}
//增加年份
void YearUp_actionPerformed(ActionEvent e) {
year++;
showYear();
showDate();
showDays();
}
//減少年份
void YearDown_actionPerformed(ActionEvent e) {
year--;
showYear();
showDate();
showDays();
}
//減少月份
void MonthDown_actionPerformed(ActionEvent e) {
month--;
if(month0) {
month = 11;
year--;
showYear();
}
showMonth();
showDate();
showDays();
}
//增加月份
void MonthUp_actionPerformed(ActionEvent e) {
month++;
if(month==12) {
month=0;
year++;
showYear();
}
showMonth();
showDate();
showDays();
}
//初始化年月日
void iniCalender(){
year=cal.get(Calendar.YEAR);
month=cal.get(Calendar.MONTH);
day=cal.get(Calendar.DAY_OF_MONTH);
}
//刷新月份
void showMonth(){
Month.setText(Integer.toString(month+1)+"月");
}
//刷新年份
void showYear(){
Year.setText(Integer.toString(year)+"年");
}
//刷新日期
void showDate(){
Out.setText(Integer.toString(year)+"-"+Integer.toString(month+1)+"-"+Integer.toString(day));
}
//重畫天數(shù)選擇面板
void showDays() {
cal.set(year,month,1);
int firstDayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
int n=mm[month];
if(cal.isLeapYear(year)month==1) n++;
int i=0;
for(;ifirstDayOfWeek-1;i++){
days[i].setEnabled(false);
days[i].setSelected(false);
days[i].setText("");
}
int d=1;
for(;d=n;d++){
days[i].setText(Integer.toString(d));
days[i].setEnabled(true);
if(d==day) days[i].setSelected(true);
else days[i].setSelected(false);;
i++;
}
for(;i42;i++){
days[i].setEnabled(false);
days[i].setSelected(false);
days[i].setText("");
}
}
//單擊年份面板選擇整個(gè)年份字符串
void SelectionYear(){
Year.setSelectionStart(0);
Year.setSelectionEnd(Year.getText().length());
}
//單擊月份面板選擇整個(gè)月份字符串
void SelectionMonth(){
Month.setSelectionStart(0);
Month.setSelectionEnd(Month.getText().length());
}
//月份面板響應(yīng)鼠標(biāo)單擊事件
void Month_mouseClicked(MouseEvent e) {
//SelectionMonth();
inputMonth();
}
//檢驗(yàn)輸入的月份
void inputMonth(){
String s;
if(Month.getText().endsWith("月"))
{
s=Month.getText().substring(0,Month.getText().length()-1);
}
else s=Month.getText();
month=Integer.parseInt(s)-1;
this.showMe();
}
//月份面板鍵盤敲擊事件響應(yīng)
void Month_keyPressed(KeyEvent e) {
if(e.getKeyChar()==10)
inputMonth();
}
//年份面板響應(yīng)鼠標(biāo)單擊事件
void Year_mouseClicked(MouseEvent e) {
//SelectionYear();
inputYear();
}
//年份鍵盤敲擊事件響應(yīng)
void Year_keyPressed(KeyEvent e) {
//System.out.print(new Integer(e.getKeyChar()).byteValue());
if(e.getKeyChar()==10)
inputYear();
}
//檢驗(yàn)輸入的年份字符串
void inputYear() {
String s;
if(Year.getText().endsWith("年"))
{
s=Year.getText().substring(0,Year.getText().length()-1);
}
else s=Year.getText();
year=Integer.parseInt(s);
this.showMe();
}
//以字符串形式返回日期,yyyy-mm-dd
public String getDate(){return Out.getText();}
//以字符串形式輸入日期,yyyy-mm-dd
public void setDate(String date){
if(date!=null){
StringTokenizer f = new StringTokenizer(date, "-");
if(f.hasMoreTokens())
year = Integer.parseInt(f.nextToken());
if(f.hasMoreTokens())
month = Integer.parseInt(f.nextToken());
if(f.hasMoreTokens())
day = Integer.parseInt(f.nextToken());
cal.set(year,month,day);
}
this.showMe();
}
//以日期對(duì)象形式輸入日期
public void setTime(Date date){
cal.setTime(date);
this.iniCalender();
this.showMe();
}
//返回日期對(duì)象
public Date getTime(){return cal.getTime();}
//返回當(dāng)前的日
public int getDay() {
return day;
}
//設(shè)置當(dāng)前的日
public void setDay(int day) {
this.day = day;
cal.set(this.year,this.month,this.day);
this.showMe();
}
//設(shè)置當(dāng)前的年
public void setYear(int year) {
this.year = year;
cal.set(this.year,this.month,this.day);
this.showMe();
}
//返回當(dāng)前的年
public int getYear() {
return year;
}
//返回當(dāng)前的月
public int getMonth() {
return month;
}
//設(shè)置當(dāng)前的月
public void setMonth(int month) {
this.month = month;
cal.set(this.year,this.month,this.day);
this.showMe();
}
//刷新
public void showMe(){
this.showDays();
this.showMonth();
this.showYear();
this.showDate();
}
}
public class TestJCalendar {
public static void main(String[] args) {
JFrame f=new JFrame();
f.setContentPane(new JCalendar());
f.pack();
//f.setResizable(false);
f.show();
}
}
你的問題就是把一周的第一天設(shè)置為星期一?
那你貼這么多代碼干嘛,一句話的事兒?jiǎn)岵皇?。建議你多看一下api
Calendar里面的方法就有啊, setFirstDayOfWeek
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.Calendar;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class DateCalendar extends JFrame implements ItemListener{
private static final long serialVersionUID = 1L;
private JComboBox year;
private JComboBox month;
private JLabel l[];
Calendar c=Calendar.getInstance();
public DateCalendar() {
year=new JComboBox();
for (int i = 1990; i 2020; i++) {
year.addItem(i+"");
}
month=new JComboBox();
for (int i = 1; i =12; i++) {
month.addItem(i+"");
}
JPanel p=new JPanel();
p.add(new JLabel("請(qǐng) 選 擇:"));
p.add(year);
p.add(new JLabel("年 "));
p.add(month);
p.add(new JLabel("日 "));
JPanel p1=new JPanel();
l=new JLabel[49];
for (int i = 0; i l.length; i++) {
l[i]=new JLabel("");
l[i].setHorizontalAlignment(JLabel.CENTER);
p1.add(l[i]);
}
l[0].setText("日");
l[1].setText("一");
l[2].setText("二");
l[3].setText("三");
l[4].setText("四");
l[5].setText("五");
l[6].setText("六");
year.addItemListener(this);
month.addItemListener(this);
p1.setLayout(new GridLayout(7,7));
this.init();
this.getContentPane().add(p1);
this.getContentPane().add(p,BorderLayout.NORTH);
this.setSize(200, 300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.pack();
this.setLocation(400, 200);
this.setVisible(true);
}
//第一次實(shí)例化對(duì)象時(shí),把面板信息顯示為當(dāng)前月的信息
public void init(){
nian=c.get(Calendar.YEAR);
yue=c.get(Calendar.MONTH)+1;
ri=c.get(Calendar.DAY_OF_MONTH);
year.setSelectedItem(nian+"");
month.setSelectedItem(yue+"");
this.setCalendar();//調(diào)用顯示面板信息設(shè)置
}
int nian,yue,ri;
//下拉事件
public void itemStateChanged(ItemEvent e) {
c.set(Calendar.YEAR,Integer.parseInt(year.getSelectedItem().toString()));
c.set(Calendar.MONTH,Integer.parseInt(month.getSelectedItem().toString())-1);
this.setCalendar();
}
//設(shè)置顯示面板的信息
public void setCalendar(){
for (int i = 7; i l.length; i++) {
l[i].setText("");
l[i].setForeground(Color.BLACK);
}
c.set(Calendar.DAY_OF_MONTH, 1);
for (int i = 1; i =this.getDay(c.get(Calendar.YEAR), c.get(Calendar.MONTH)+1); i++) {
l[5+i+c.get(Calendar.DAY_OF_WEEK)].setText(i+"");
if(nian==c.get(Calendar.YEAR)yue==c.get(Calendar.MONTH)+1ri==i){
l[5+i+c.get(Calendar.DAY_OF_WEEK)].setForeground(Color.BLUE);
}
}
}
//判斷天數(shù)
public int getDay(int y,int m){
if(m==1||m==3||m==5||m==7||m==8||m==10||m==12){
return 31;
}
if(m==4||m==6||m==9||m==11){
return 30;
}
if(y%4==0y%100!=0||y%400==0){
return 29;
}else{
return 28;
}
}
public static void main(String[] args){
new DateCalendar().setTitle("日歷顯示");
}
}
源代碼:
//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);
}
網(wǎng)站題目:java日歷界面源代碼 javaswing日歷窗口
網(wǎng)頁網(wǎng)址:http://jinyejixie.com/article36/dosedsg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站排名、關(guān)鍵詞優(yōu)化、搜索引擎優(yōu)化、虛擬主機(jī)、網(wǎng)站制作、網(wǎng)站內(nèi)鏈
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)