java swing 显示时间_java swing 日期时间 控件

pAAAAAElFTkSuQmCC

package custome;

import java.awt.BorderLayout;

import java.awt.Color;

import java.awt.Dimension;

import java.awt.FlowLayout;

import java.awt.Font;

import java.awt.GridLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.text.ParseException;

import java.text.SimpleDateFormat;

import java.util.Calendar;

import java.util.Date;

import javax.swing.JButton;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.JSpinner;

import javax.swing.SpinnerNumberModel;

import javax.swing.SwingConstants;

import javax.swing.border.LineBorder;

import javax.swing.event.ChangeEvent;

import javax.swing.event.ChangeListener;

public class PanDateChooser extends JPanel implements ActionListener, ChangeListener {

int startYear = 1980; // 默认【最小】显示年份

int lastYear = 2050; // 默认【最大】显示年份

int width = 390; // 界面宽度

int height = 210; // 界面高度

Color backGroundColor = Color.gray; // 底色

// 月历表格配色----------------//

Color palletTableColor = Color.white; // 日历表底色

Color todayBackColor = Color.orange; // 今天背景色

Color weekFontColor = Color.blue; // 星期文字色

Color dateFontColor = Color.black; // 日期文字色

Color weekendFontColor = Color.red; // 周末文字色

// 控制条配色------------------//

Color controlLineColor = Color.pink; // 控制条底色

Color controlTextColor = Color.white; // 控制条标签文字色

Color rbFontColor = Color.white; // RoundBox文字色

Color rbBorderColor = Color.red; // RoundBox边框色

Color rbButtonColor = Color.pink; // RoundBox按钮色

Color rbBtFontColor = Color.red; // RoundBox按钮文字色

JSpinner yearSpin;

JSpinner monthSpin;

JSpinner daySpin;

JSpinner hourSpin;

JSpinner minuteSpin;

JSpinner secondSpin;

SimpleDateFormat df;

JButton[][] daysButton = new JButton[6][7];

String selectdate="";

public PanDateChooser(SimpleDateFormat df,int width,int height) {

this.df=df;

this.width=width;

this.height=height;

setLayout(new BorderLayout());

setBorder(new LineBorder(backGroundColor, 2));

setBackground(backGroundColor);

JPanel topYearAndMonth = createYearAndMonthPanal(df);

add(topYearAndMonth, BorderLayout.NORTH);

JPanel centerWeekAndDay = createWeekAndDayPanal();

add(centerWeekAndDay, BorderLayout.CENTER);

flushWeekAndDay();

}

private JPanel createYearAndMonthPanal(SimpleDateFormat df) {

Calendar c = getCalendar();

int currentYear = c.get(Calendar.YEAR);

int currentMonth = c.get(Calendar.MONTH) + 1;

int currentHour = c.get(Calendar.HOUR_OF_DAY);

int currentMinute = c.get(Calendar.MINUTE);

int currentSecond = c.get(Calendar.SECOND);

JPanel result = new JPanel();

result.setLayout(new FlowLayout());

result.setBackground(controlLineColor);

yearSpin = new JSpinner(new SpinnerNumberModel(currentYear, startYear, lastYear, 1));

yearSpin.setPreferredSize(new Dimension(48, 20));

yearSpin.setName("Year");

yearSpin.setEditor(new JSpinner.NumberEditor(yearSpin, "####"));

yearSpin.addChangeListener(this);

result.add(yearSpin);

JLabel yearLabel = new JLabel("年");

yearLabel.setForeground(controlTextColor);

result.add(yearLabel);

monthSpin = new JSpinner(new SpinnerNumberModel(currentMonth, 1, 12, 1));

monthSpin.setPreferredSize(new Dimension(35, 20));

monthSpin.setName("Month");

monthSpin.addChangeListener(this);

result.add(monthSpin);

JLabel monthLabel = new JLabel("月");

monthLabel.setForeground(controlTextColor);

result.add(monthLabel);

//如果这里要能够选择,会要判断很多东西,比如每个月分别由多少日,以及闰年问题.所以,就干脆把Enable设为false

daySpin = new JSpinner(new SpinnerNumberModel(currentMonth, 1, 31, 1));

daySpin.setPreferredSize(new Dimension(35, 20));

daySpin.setName("Day");

daySpin.addChangeListener(this);

daySpin.setEnabled(false);

daySpin.setToolTipText("请在下面的日历面板中进行选择哪一天!");

result.add(daySpin);

JLabel dayLabel = new JLabel("日");

dayLabel.setForeground(controlTextColor);

result.add(dayLabel);

hourSpin = new JSpinner(new SpinnerNumberModel(currentHour, 0, 23, 1));

hourSpin.setPreferredSize(new Dimension(35, 20));

hourSpin.setName("Hour");

hourSpin.addChangeListener(this);

result.add(hourSpin);

JLabel hourLabel = new JLabel("时");

hourLabel.setForeground(controlTextColor);

result.add(hourLabel);

minuteSpin = new JSpinner(new SpinnerNumberModel(currentMinute, 0, 59, 1));

minuteSpin.setPreferredSize(new Dimension(35, 20));

minuteSpin.setName("Minute");

minuteSpin.addChangeListener(this);

result.add(minuteSpin);

JLabel minuteLabel = new JLabel("分");

hourLabel.setForeground(controlTextColor);

if(!df.toPattern().contains("mm"))

{

minuteSpin.setVisible(false);

minuteLabel.setVisible(false);

}

result.add(minuteLabel);

secondSpin = new JSpinner(new SpinnerNumberModel(currentSecond, 0, 59, 1));

secondSpin.setPreferredSize(new Dimension(35, 20));

secondSpin.setName("Second");

secondSpin.addChangeListener(this);

result.add(secondSpin);

JLabel secondLabel = new JLabel("秒");

hourLabel.setForeground(controlTextColor);

if(!df.toPattern().contains("ss"))

{

secondSpin.setVisible(false);

secondLabel.setVisible(false);

}

result.add(secondLabel);

return result;

}

private JPanel createWeekAndDayPanal() {

String colname[] = {"日", "一", "二", "三", "四", "五", "六"};

JPanel result = new JPanel();

// 设置固定字体,以免调用环境改变影响界面美观

result.setFont(new Font("宋体", Font.PLAIN, 12));

result.setLayout(new GridLayout(7, 7));

result.setBackground(Color.white);

JLabel cell;

for (int i = 0; i < 7; i++) {

cell = new JLabel(colname[i]);

cell.setHorizontalAlignment(JLabel.RIGHT);

if (i == 0 || i == 6) {

cell.setForeground(weekendFontColor);

} else {

cell.setForeground(weekFontColor);

}

result.add(cell);

}

int actionCommandId = 0;

for (int i = 0; i < 6; i++) {

for (int j = 0; j < 7; j++) {

JButton numberButton = new JButton();

numberButton.setBorder(null);

numberButton.setHorizontalAlignment(SwingConstants.RIGHT);

numberButton.setActionCommand(String.valueOf(actionCommandId));

numberButton.addActionListener(this);

numberButton.setBackground(palletTableColor);

numberButton.setForeground(dateFontColor);

if (j == 0 || j == 6) {

numberButton.setForeground(weekendFontColor);

} else {

numberButton.setForeground(dateFontColor);

}

daysButton[i][j] = numberButton;

result.add(numberButton);

actionCommandId++;

}

}

return result;

}

public Date getDate() {

try {

return df.parse(selectdate);

} catch (ParseException e) {

return getNowDate();

}

}

private static Date getNowDate() {

return Calendar.getInstance().getTime();

}

private Calendar getCalendar() {

Calendar result = Calendar.getInstance();

result.setTime(getDate());

return result;

}

private int getSelectedYear() {

return ((Integer) yearSpin.getValue()).intValue();

}

private int getSelectedMonth() {

return ((Integer) monthSpin.getValue()).intValue();

}

private int getSelectedHour() {

return ((Integer) hourSpin.getValue()).intValue();

}

private int getSelectedMinite() {

return ((Integer) minuteSpin.getValue()).intValue();

}

private int getSelectedSecond() {

return ((Integer) secondSpin.getValue()).intValue();

}

private void dayColorUpdate(boolean isOldDay) {

Calendar c = getCalendar();

int day = c.get(Calendar.DAY_OF_MONTH);

c.set(Calendar.DAY_OF_MONTH, 1);

daySpin.setValue(day);

int actionCommandId = day - 2 + c.get(Calendar.DAY_OF_WEEK);

int i = actionCommandId / 7;

int j = actionCommandId % 7;

if (isOldDay) {

daysButton[i][j].setForeground(dateFontColor);

} else {

daysButton[i][j].setForeground(todayBackColor);

}

}

private void flushWeekAndDay() {

Calendar c = getCalendar();

c.set(Calendar.DAY_OF_MONTH, 1);

int maxDayNo = c.getActualMaximum(Calendar.DAY_OF_MONTH);

int dayNo = 2 - c.get(Calendar.DAY_OF_WEEK);

for (int i = 0; i < 6; i++) {

for (int j = 0; j < 7; j++) {

String s = "";

if (dayNo >= 1 && dayNo <= maxDayNo) {

s = String.valueOf(dayNo);

}

daysButton[i][j].setText(s);

dayNo++;

}

}

dayColorUpdate(false);

}

/**

* 选择日期时的响应事件

*/

@Override

public void stateChanged(ChangeEvent e) {

JSpinner source = (JSpinner) e.getSource();

Calendar c = getCalendar();

if (source.getName().equals("Hour")) {

c.set(Calendar.HOUR_OF_DAY, getSelectedHour());

selectdate=df.format(c.getTime());

return;

}

if (source.getName().equals("Minute")) {

c.set(Calendar.MINUTE, getSelectedMinite());

selectdate=df.format(c.getTime());

return;

}

if (source.getName().equals("Second")) {

c.set(Calendar.SECOND, getSelectedSecond());

selectdate=df.format(c.getTime());

return;

}

dayColorUpdate(true);

if (source.getName().equals("Year")) {

c.set(Calendar.YEAR, getSelectedYear());

} else if (source.getName().equals("Month")) {

c.set(Calendar.MONTH, getSelectedMonth() - 1);

}

selectdate= df.format(c.getTime());

flushWeekAndDay();

}

/**

* 选择日期时的响应事件

*/

@Override

public void actionPerformed(ActionEvent e) {

JButton source = (JButton) e.getSource();

if (source.getText().length() == 0) {

return;

}

dayColorUpdate(true);

source.setForeground(todayBackColor);

int newDay = Integer.parseInt(source.getText());

Calendar c = getCalendar();

c.set(Calendar.DAY_OF_MONTH, newDay);

selectdate= df.format(c.getTime());

//把daySpin中的值也变了

daySpin.setValue(Integer.valueOf(newDay));

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值