快上网专注成都网站设计 成都网站制作 成都网站建设
成都网站建设公司服务热线:028-86922220

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

java可视化日历代码 java设计日历软件

用java控制台实现万年历-要求打印当前年月日的日历表格,要求对当天的日期单独标示

以下是用java swing编写的日历,很好用,在我所做的系统里就能够正常的使用

我们提供的服务有:网站建设、成都网站设计、微信公众号开发、网站优化、网站认证、黄骅ssl等。为成百上千家企事业单位解决了网站和推广的问题。提供周到的售前咨询和贴心的售后服务,是有科学管理、有技术的黄骅网站制作公司

接下来 是具体代码:

package Demo;

import java.awt.BorderLayout;

import java.awt.Color;

import java.awt.Dimension;

import java.awt.FlowLayout;

import java.awt.Font;

import java.awt.Frame;

import java.awt.GridLayout;

import java.awt.Point;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.MouseAdapter;

import java.awt.event.MouseEvent;

import java.text.ParseException;

import java.text.SimpleDateFormat;

import java.util.Calendar;

import java.util.Date;

import javax.swing.JButton;

import javax.swing.JDialog;

import javax.swing.JFrame;

import javax.swing.JFormattedTextField;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.JSpinner;

import javax.swing.SpinnerNumberModel;

import javax.swing.SwingConstants;

import javax.swing.SwingUtilities;

import javax.swing.border.LineBorder;

import javax.swing.event.ChangeEvent;

import javax.swing.event.ChangeListener;

public class DateChooser extends JPanel implements ActionListener,

ChangeListener {

/**

*

*/

private static final long serialVersionUID = 1L;

int startYear = 1980;

int lastYear = 2050;

int width = 270;

int height = 200;

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;

Color rbBorderColor = Color.red;

Color rbButtonColor = Color.pink;

Color rbBtFontColor = Color.red;

JDialog dialog;

JSpinner yearSpin;

JSpinner monthSpin;

JSpinner hourSpin;

JSpinner minuteSpin;

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

JFormattedTextField jFormattedTextField;

Calendar c = getCalendar();

DateChooser(JFormattedTextField jftf) {

jFormattedTextField = jftf;

setLayout(new BorderLayout());

setBorder(new LineBorder(backGroundColor, 2));

setBackground(backGroundColor);

JPanel topYearAndMonth = createYearAndMonthPanal();

add(topYearAndMonth, BorderLayout.NORTH);

JPanel centerWeekAndDay = createWeekAndDayPanal();

add(centerWeekAndDay, BorderLayout.CENTER);

}

private JPanel createYearAndMonthPanal() {

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);

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);

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("分");

minuteLabel.setForeground(controlTextColor);

result.add(minuteLabel);

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.CENTER);

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.CENTER);

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;

numberButton.addMouseListener(new MouseAdapter() {

public void mouseClicked(MouseEvent e) {

if (e.getClickCount() == 2) {

closeAndSetDate();

}

}

});

result.add(numberButton);

actionCommandId++;

}

return result;

}

private JDialog createDialog(Frame owner) {

JDialog result = new JDialog(owner, "日期时间选择", true);

result.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);

result.getContentPane().add(this, BorderLayout.CENTER);

result.pack();

result.setSize(width, height);

return result;

}

public void showDateChooser(Point position) {

Object tmpobj=SwingUtilities.getWindowAncestor(jFormattedTextField);

if(tmpobj.getClass().isInstance(new JDialog())||tmpobj.getClass().getSuperclass().isInstance(new JDialog()))

{

JDialog ownerdialog = (JDialog) SwingUtilities

.getWindowAncestor(jFormattedTextField);

Frame owner = (Frame) SwingUtilities.getWindowAncestor(ownerdialog);

if (dialog == null || dialog.getOwner() != owner) {

dialog = createDialog(owner);

}

dialog.setLocation(getAppropriateLocation(owner, position));

}

else if(tmpobj.getClass().isInstance(new JFrame())||tmpobj.getClass().getSuperclass().isInstance(new JFrame()))

{

JFrame ownerFrame = (JFrame) SwingUtilities

.getWindowAncestor(jFormattedTextField);

if (dialog == null || dialog.getOwner() != ownerFrame) {

dialog = createDialog(ownerFrame);

}

dialog.setLocation(getAppropriateLocation(ownerFrame, position));

}

flushWeekAndDay();

dialog.setVisible(true);

}

Point getAppropriateLocation(Frame owner, Point position) {

Point result = new Point(position);

Point p = owner.getLocation();

int offsetX = (position.x + width) - (p.x + owner.getWidth());

int offsetY = (position.y + height) - (p.y + owner.getHeight());

if (offsetX 0) {

result.x -= offsetX;

}

if (offsetY 0) {

result.y -= offsetY;

}

return result;

}

public void closeAndSetDate() {

setDate(c.getTime());

dialog.dispose();

}

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 getSelectedMinute() {

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

}

private void dayColorUpdate(boolean isOldDay) {

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

c.set(Calendar.DAY_OF_MONTH, 1);

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() {

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);

}

public void stateChanged(ChangeEvent e) {

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

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

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

return;

}

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

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

return;

}

dayColorUpdate(true);

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

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

}

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

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

}

flushWeekAndDay();

}

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());

c.set(Calendar.DAY_OF_MONTH, newDay);

}

public void setDate(Date date) {

jFormattedTextField.setText(getDefaultDateFormat().format(date));

}

public Date getDate() {

try {

String dateString = jFormattedTextField.getText();

return getDefaultDateFormat().parse(dateString);

} catch (ParseException e) {

return getNowDate();

} catch (Exception ee) {

return getNowDate();

}

}

private static Date getNowDate() {

return Calendar.getInstance().getTime();

}

private static SimpleDateFormat getDefaultDateFormat() {

return new SimpleDateFormat("yyyy-MM-dd HH:mm");

}

}

测试程序:

package Demo;

import java.awt.Point;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JButton;

import javax.swing.JFormattedTextField;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

public class DemoForDateChooser extends JFrame {

private JFormattedTextField formattedTextField;

/**

* Launch the application

* @param args

*/

public static void main(String args[]) {

try {

DemoForDateChooser frame = new DemoForDateChooser();

frame.setVisible(true);

} catch (Exception e) {

e.printStackTrace();

}

}

/**

* Create the frame

*/

public DemoForDateChooser() {

super();

setTitle("日期选择框");

getContentPane().setLayout(null);

setBounds(100, 100, 383, 137);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

final JPanel panel = new JPanel();

panel.setLayout(null);

panel.setBounds(0, 0, 375, 107);

getContentPane().add(panel);

formattedTextField = new JFormattedTextField();

formattedTextField.setBounds(68, 48, 175, 23);

final JButton button = new JButton();

button.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent arg0) {

DateChooser mDateChooser=new DateChooser(formattedTextField);

Point p = button.getLocationOnScreen();

p.y = p.y + 30;

mDateChooser.showDateChooser(p);

formattedTextField.requestFocusInWindow();

}

});

button.setText("选择日期");

button.setBounds(249, 47, 99, 23);

panel.add(button);

final JLabel label = new JLabel();

label.setText("日期:");

label.setBounds(10, 47, 71, 23);

panel.add(label);

panel.add(formattedTextField);

//

}

}


当前名称:java可视化日历代码 java设计日历软件
浏览地址:http://6mz.cn/article/dspjcdo.html

其他资讯