Java Swing单击文本框弹出日历组件
java 弹窗方法

java 弹窗方法Java作为一种广泛应用于各种领域的编程语言,提供了丰富的图形用户界面(GUI)组件。
在Java中,弹窗是一种常见的交互方式,可以用来提示用户、获取用户输入或显示信息。
本文将介绍Java弹窗方法的分类、原理以及常见组件的使用方法,并通过实例进行演示。
一、弹窗方法的概述在Java中,弹窗方法主要有两种:1.自带弹窗:Java自带的Swing和JavaFX库提供了许多弹窗组件,如JOptionPane的showMessageDialog、showInputDialog等。
2.自定义弹窗:通过Java的图形界面组件(如JFrame、JDialog等)和事件处理机制来实现。
二、Java弹窗方法的分类与原理1.自带弹窗Java自带的弹窗方法主要位于JOptionPane库中。
这些方法可以方便地创建各种类型的对话框,如信息提示框、输入框等。
以下是几个常用的自带弹窗方法:- showMessageDialog(parent,message,title,buttons):显示一个带有指定信息、标题和按钮的对话框。
- showInputDialog(parent,message,title,defaultValue):显示一个带有指定信息、标题和默认值的输入框。
- showConfirmDialog(parent,message,title,options):显示一个带有指定信息、标题和选项的确认对话框。
2.自定义弹窗自定义弹窗是通过创建JFrame或JDialog组件来实现。
首先创建一个包含弹窗内容的JPanel,然后设置JPanel的边界约束,最后添加到JFrame或JDialog中。
以下是自定义弹窗的简单示例:```javaJFrame frame = new JFrame("自定义弹窗");frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);frame.setSize(300, 200);JPanel panel = new JPanel();panel.setLayout(new BorderLayout());JLabel label = new JLabel("请输入姓名:");JTextField textField = new JTextField(20);panel.add(label, BorderLayout.WEST);panel.add(textField, BorderLayout.EAST);frame.getContentPane().add(panel);frame.setVisible(true);```三、常见Java弹窗组件及使用方法1.JFrame:创建一个独立的窗口,可以设置大小、标题和关闭操作等。
java 弹窗方法

java 弹窗方法在过去的学习和工作中,我们可能会遇到各种各样的弹窗需求,如消息提示、窗口交互等。
Java作为一种广泛应用于企业级开发的编程语言,提供了多种弹窗方法。
本文将为您介绍Java弹窗方法的分类与原理,常见Java弹窗工具箱,并通过实例演示与代码解析,帮助您更好地理解和应用这些弹窗方法。
1.弹窗方法的概述在Java中,弹窗方法主要包括两类:自带弹窗和自定义弹窗。
自带弹窗是由Java自带的Swing组件库提供的,如JOptionPane类的showMessageDialog、showInputDialog等方法。
自定义弹窗则是通过Java 编写界面组件来实现,如使用JavaFX、Swing等框架创建窗口。
2.Java弹窗方法的分类与原理(1)自带弹窗Java自带的弹窗方法主要位于JOptionPane类中,包括以下几种:- showMessageDialog:显示一个包含指定消息的对话框。
- showInputDialog:显示一个包含指定消息和输入框的对话框,用户可以输入内容并确认。
- showConfirmDialog:显示一个包含指定消息的确认对话框,用户可以选择“是”或“否”。
- showOptionDialog:显示一个包含指定消息的选择对话框,用户可以从多个选项中选择一个。
(2)自定义弹窗自定义弹窗主要是通过Java编写界面组件来实现,常见的有以下几种方法:- 使用JavaFX:通过JavaFX框架创建窗口、按钮、文本框等组件,搭建需要的弹窗界面。
- 使用Swing:通过Swing框架创建窗口、按钮、文本框等组件,搭建需要的弹窗界面。
- 使用第三方库:如使用Electron、React Native等库实现跨平台弹窗。
3.常见Java弹窗工具箱介绍在Java中,有一些开源的弹窗工具箱可以帮助我们快速搭建弹窗界面,如下:- Apache APR:一个基于Java的弹窗组件库,提供了丰富的弹窗样式和功能。
JavaSwing日历控件

Java swing日历控件,界面精美,使用方便,能用于个人作品首先,声明一点,这个控件是我从一套据说价值九百多美元的swing类包中提取出来,并加以修改的,所以:
一,你不能把该控件用于任何商业产品中。
二,该控件没有源代码。
(DatePicker.java除外,这个是我加进去的)
三,该控件可以用于你的个人作品中。
截图如下:
使用之前请把datepicker.jar导入classpath中
使用方法如下:
DatePicker datepick = new DatePicker();
或者
datepick = new DatePicker(date,DefaultFormat,font,dimension);//自定义参数值
//各参数含义如下
//参数1 设置该控件的初始值(可选)
//参数2 设置日期的格式(可选)
//参数3 设置该控件的字体(可选)
//参数3 设置该控件的大小(可选)
//该控件继承自JComponent ,它继承了Jcomponent的属性
datepick.setTimePanleVisible(true);//设置时钟面板可见
atepick.setHightlightdays(hilightDays, Color.red);//设置一个月份中需要高亮显示的日子
datepick.setDisableddays(disabledDays);//设置一个月份中不需要的日子,呈灰色显示。
java jpopupmenu方法 -回复

java jpopupmenu方法-回复Java JPopupMenu方法是Java Swing库中的一种组件,它提供了弹出菜单的功能。
在本文中,我们将一步一步地回答有关JPopupMenu的问题,并深入讨论其使用方法和示例。
1. 什么是JPopupMenu?JPopupMenu是Java Swing库中的一个类,它表示弹出菜单。
它是一个轻量级组件,可以在鼠标右键单击或其他用户交互方式触发时出现。
JPopupMenu可以包含菜单项、子菜单和分隔符。
2. 如何创建和显示JPopupMenu?要创建JPopupMenu,我们可以使用其默认构造函数:javaJPopupMenu popupMenu = new JPopupMenu();要显示JPopupMenu,我们可以通过以下方法将其与组件关联:javacomponent.setComponentPopupMenu(popupMenu);其中,component是指定的组件,可以是任何Swing组件,例如JButton或JTextField。
当用户右键单击该组件时,JPopupMenu将出现。
3. 如何向JPopupMenu添加菜单项?我们可以使用JPopupMenu的add()方法添加菜单项:javaJMenuItem menuItem = new JMenuItem("菜单项标签"); popupMenu.add(menuItem);在上面的示例中,我们创建了一个名为"菜单项标签"的菜单项,并将其添加到JPopupMenu中。
可以通过添加多个菜单项来构建弹出菜单。
4. 如何向JPopupMenu添加子菜单?与添加菜单项类似,我们可以使用JPopupMenu的add()方法添加子菜单:javaJMenu submenu = new JMenu("子菜单标签");popupMenu.add(submenu);在上面的示例中,我们创建了一个名为"子菜单标签"的子菜单,并将其添加到JPopupMenu中。
java日历小程序

java日历小程序简介:Java日历小程序是一个基于Java语言开发的工具,用于显示和管理日期和时间。
它提供了一种简单而直观的方式来查看、创建和编辑日历事件,并提供了一些额外的功能,如提醒、重复事件和时间段的计算等。
本文将详细介绍Java日历小程序的功能、使用方法和设计原理。
功能:1. 显示日历:Java日历小程序可以显示当前月份的日历,并以日、周或月的形式展示。
用户可以通过界面上的上下翻页按钮切换月份,方便查看不同日期的日历。
2. 创建和编辑事件:用户可以通过Java日历小程序创建新的日历事件,并为每个事件指定标题、日期、时间和地点等详细信息。
程序还提供了事件编辑功能,允许用户修改已有事件的信息。
3. 提醒功能:Java日历小程序可以设置事件的提醒功能,提醒用户在指定的时间前提醒。
用户可以选择在事件开始前几分钟、几小时或几天提醒,并可以自定义提醒方式,如弹窗、声音或邮件通知等。
4. 重复事件:用户可以为事件设置重复功能,使事件在指定的日期间隔内重复发生。
可以选择每天、每周、每月或每年重复,并可以设置重复的结束日期。
5. 时间段计算:Java日历小程序可以计算两个日期之间的时间段,如计算两个事件之间的天数、小时数或分钟数等。
用户可以选择计算方式,并获取精确的时间段结果。
使用方法:1. 下载和安装:用户可以从官方网站上下载Java日历小程序的安装包,然后按照安装向导进行安装。
安装完成后,用户可以在桌面或开始菜单中找到程序的快捷方式。
2. 打开程序:双击程序的快捷方式,Java日历小程序将启动并显示当前月份的日历。
用户可以通过界面上的按钮和菜单来使用各种功能。
3. 创建事件:用户可以点击界面上的“添加事件”按钮,弹出一个对话框,输入事件的详细信息,如标题、日期、时间和地点等。
点击“确定”按钮后,事件将被创建并显示在日历上。
4. 编辑事件:用户可以在日历上点击已有的事件,弹出一个对话框,允许用户修改事件的信息。
java课程设计简单日历(word文档良心出品)

j a v a课程设计报告题目:JA V A简易时间日历程序****:***学号:**********专业班级:信101****:***课程设计题目题目说明通过编写一个基于JAVA的应用系统综合实例,自定义一个日历组件显示日期和时间并进行适当的功能扩充,实践Java语言编程技术。
系统设计1 设计目标一个完整的程序应具有以下功能:1)显示当月日历、当前日期、当前时间;2)可查寻任意月以及任意年的日历;3)使用图形化界面能够弹出对话框;5)正常退出程序。
2 设计思想设计一个类用来构成日历系统的主窗口,然后编写一个框架类显示时间和提示信息。
在设计中应用了多种容器和控件。
系统模块划分图1:简易日历的程序结构图初始化:public void init()完成界面初始化,形成一个以挂历形式显示当前日期的窗口。
日历描述:(1)public void updateView()改变日期后完成更新界面;(2)抽象类java.util.Calendar获取系统日期并传递日期数据而且在人工改变日期后得出当天是周几;(3)public static void main(String[] args)主函数完成系统各算法的调用并对主窗口的一些属性进行设置;滚动时间:将时间以文本的形式在文本框中滚动播出,并能改变滚动的速度。
4. 使用类及接口仅仅简单说明类的功能,详细资料请参看《JavaTM 2 Platform Standard Ed. 6》的电子文档,常规的接口与包则省略不屑。
//以下是日历程序块中使用的类package fancy;import java.awt.*;import java.awt.event.*;import java.util.*; //主要用此包中的日期和时间类import javax.swing.*;import javax.swing.event.*;import javax.swing.table.*;//以下是对滚动时间程序块所使用的类和接口,用到定时器类Timerimport java.awt.Color;import java.awt.FlowLayout;import java.awt.event.ActionListener;import java.awt.event.ActionEvent;import java.awt.event.FocusListener;、//用于监听鼠标单击(焦点改变)事件import java.awt.event.FocusEvent;//用于响应鼠标单击(焦点改变)事件、import javax.swing.JFrame;import javax.swing.JTextField;import javax.swing.JSpinner;//让用户从一个有序序列中选择一个数字或者一个对象值的单行输入字段。
SWT实现弹出日历控件

SWT实现弹出日历控件SWT实现弹出日历控件2010-08-25 10:01:48| 分类:Eclipse 插件开发|字号订阅实现像网页上的那种用户单击一个Text框然后框下面出现一个日历控件,点击Text框以外的地方日历消失,怎么实现?Java代码import org.eclipse.swt.SWT; import org.eclipse.swt.events.MouseAdapter; import org.eclipse.swt.events.MouseEvent; importorg.eclipse.swt.graphics.Color; importorg.eclipse.swt.graphics.Point; importorg.eclipse.swt.widgets.Display; importorg.eclipse.swt.widgets.Shell; importorg.eclipse.swt.widgets.Text; public class DateTimeDemo extends Shell { private static Display display; private Text text;public static void main(String args[]){ try { display =Display.getDefault(); DateTimeDemo shell = new DateTimeDemo(display, SWT.SHELL_TRIM); shell.open(); yout();while(!shell.isDisposed()){ if(!display.readAndDispatch()){ display.sleep();} }display.dispose(); }catch(Exception e){ e.printStackTrace(); } } public DateTimeDemo(Display display, int style) { super(display, style); createContents(); } protected void createContents(){ setText("DateTime");setSize(471, 140); text = new Text(this, SWT.BORDER); text.setEditable(false); text.setBackground(new Color(display, 255, 255, 255)); text.setBounds(122, 41, 228, 25);text.addMouseListener(new MouseAdapter(){ public void mouseUp(MouseEvent e) { DTDialog dialog =DTDialog.getInstance(DateTimeDemo.this);dialog.open(); } }); } public Point getDtLocation() { return new Point(text.getLocation().x +DateTimeDemo.this.getLocation().x,text.getLocation().y + DateTimeDemo.this.getLocation().y + 60); } public void setDate(String str) { text.setText(str); }@Override protected void checkSubclass(){ // Disable the check that prevents subclassing of SWT components } } Java代码import org.eclipse.swt.SWT; importorg.eclipse.swt.events.FocusAdapter; importorg.eclipse.swt.events.FocusEvent; importorg.eclipse.swt.events.SelectionAdapter; importorg.eclipse.swt.events.SelectionEvent; importyout.FillLayout; importorg.eclipse.swt.widgets.DateTime; importorg.eclipse.swt.widgets.Dialog; importorg.eclipse.swt.widgets.Display; importorg.eclipse.swt.widgets.Shell; public class DTDialog extends Dialog { private Objectresult; private Shell shell; private DateTime dateTime; private DateTimeDemo parent; private static DTDialog instance; public static DTDialog getInstance(DateTimeDemo parent) { if(instance == null){ instance = newDTDialog(parent); } return instance; } privateDTDialog(DateTimeDemo parent){ super(parent, SWT.NO_TRIM);this.parent = parent; } public Object open() { if(shell == null ||shell.isDisposed()){ createContents();shell.open(); yout();Display display = getParent().getDisplay();while(!shell.isDisposed()){ if(!display.readAndDispatch()){ display.sleep();} }display.dispose(); } else{ shell.setLocation(parent.getDtLocation()); shell.setVisible(true);shell.setFocus(); } return result; } protected void createContents() { shell = newShell(getParent(), SWT.NO_TRIM);shell.setLayout(new FillLayout());shell.setSize(272, 140);shell.setLocation(parent.getDtLocation());shell.setText("SWT Dialog"); dateTime = new DateTime(shell, SWT.CALENDAR);dateTime.addSelectionListener(new SelectionAdapter() { public voidwidgetSelected(SelectionEvent e){ parent.setDate(formatDt());} });dateTime.addFocusListener(new FocusAdapter(){ public void focusLost(FocusEvent e) { parent.setDate(formatDt()); shell.setVisible(false); } });} private String formatDt(){ return dateTime.getYear() + "-" + dateTime.getMonth() + "-" + dateTime.getDay(); }public Shell getShell() { return shell; } }。
一个java Swing做的日历控件

tbCalendar .getTableHeader().setResizingAllowed(false);
/////设置列大小不可改变
nowDate=new JLabel();
nowDate.setText("今天是:"+gc.get(Calendar.YEAR)+"年"+
public void keyReleased(KeyEvent e) {
super.keyReleased(e);
if(e.getKeyCode()==KeyEvent.VK_ENTER)
btnYearDown=new JButton("▼");
//年份减按钮
btnYearDown.setFont(font);
btnYearDown.setMargin(new Insets(0,0,0,0));
btnYearUp.setBounds(40, 6, 20, 10);
//按钮定位
btnYearDown.setBounds(40, 16, 20, 10);
import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
isSelected, boolean hasFocus,
int row, int column) {
Object o=tbCalendar.getValueAt(row,
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
Java Swing单击文本框弹出日历组件2011-04-07 16:12package com.niit.swing2;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; //RoundBox文字色Color rbBorderColor = Color.red; //RoundBox边框色Color rbButtonColor = Color.pink; //RoundBox按钮色Color rbBtFontColor = Color.red; //RoundBox按钮文字色JDialog dialog; //用于显示日期选择控件JSpinner yearSpin; //调节年份的JSpinnerJSpinner monthSpin; //调节月份JSpinner hourSpin; //调节小时JSpinner minuteSpin;//调节分钟JButton[][] daysButton = new JButton[6][7];//用于显示当前月份每一天所对应的星期的按钮数组JFormattedTextField jFormattedTextField;//显示当前选择日期的有格式输入框Calendar c = getCalendar();Calendar cal = Calendar.getInstance();int currentDay=cal.get(Calendar.DAY_OF_MONTH);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);}/**** 功能描述:用于创建年份及月份显示面板* @return 用于创建年份及月份显示面板JPanel对象*/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;}/**** 功能描述:用于创建当前月份中每一天按对应星期排列所在面板* @return*/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);elsecell.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);elsenumberButton.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);// System.out.println(day+"-----day-----");c.set(Calendar.DAY_OF_MONTH, currentDay);// System.out.println("当前日期day:"+c.get(Calendar.DATE));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);elsedaysButton[i][j].setForeground(todayBackColor);}private void flushWeekAndDay() {c.set(Calendar.DAY_OF_MONTH, currentDay);int maxDayNo = c.getActualMaximum(Calendar.DAY_OF_MONTH);int dayNo = 2 - c.get(Calendar.DAY_OF_WEEK);// System.out.println("某月日期值范围:"+dayNo+"----"+maxDayNo);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();// System.out.println(dateString+"---------");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");}}----------------------------------------------------------MyTest-----------------------------------------package com.niit.swing2;import java.awt.Point;import java.awt.event.MouseEvent;import java.awt.event.MouseListener;import javax.swing.JFormattedTextField;import javax.swing.JFrame;import javax.swing.JLabel;public class MyTest {private JFrame jf;private JFormattedTextField tfDate;private JLabel lb;public static void main(String[] args) {new MyTest().init();public void init(){jf=new JFrame("日期组件");jf.setLayout(null);jf.setBounds(10, 10, 300, 200);jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);lb=new JLabel("选择日期:");lb.setBounds(15, 60, 80, 30);initTfDate();//初始化文本框jf.add(tfDate);jf.add(lb);jf.setVisible(true);}/** initTfDate 初始化JFormattedTextField tfDate;*/public void initTfDate(){tfDate=new JFormattedTextField();tfDate.setBounds(100, 60, 120, 23);tfDate.addMouseListener(new MouseListener(){public void mouseClicked(MouseEvent mouseevent) { //添加文本框单击事件// TODO Auto-generated method stub//System.out.println("------MyTest-----");DateChooser mDateChooser=new DateChooser(tfDate);//设置DateChooser弹出位置Point p = tfDate.getLocationOnScreen();p.y = p.y + 30;mDateChooser.showDateChooser(p);tfDate.requestFocusInWindow();}public void mouseEntered(MouseEvent mouseevent) {// TODO Auto-generated method stub}public void mouseExited(MouseEvent mouseevent) {// TODO Auto-generated method stub}public void mousePressed(MouseEvent mouseevent) {// TODO Auto-generated method stub}public void mouseReleased(MouseEvent mouseevent) {// TODO Auto-generated method stub}}); }}。