导航:首页 > 编程语言 > javaswing日历

javaswing日历

发布时间:2022-08-15 07:58:36

java swing 日历控件怎么实现 最好是源码

不知道你是否听说过swingx?它是swinglabs的项目,虽然现在已不再继续,但它提供了非常多好用的Swing扩展组件。其中包括JXDatepicker(日期输入控件),JXMonthView(月视图日历控件)。

swingx你可以到maven库里面搜索Jar包(google "maven repos",出现很多maven库,随便找一个,搜索swingx,找最新的下载。如果你使用maven管理项目,那更好办了,把swingx放入依赖里面即可)。

JXDatepicker的使用方法:
// JXDatePicker提供了编辑器供用户直接输入日期,也提供了右侧按钮,点击按钮出现日期选择期供用户选择日期。
JXDatePicker datePicker = new JXDatePicker();
JXDatePicker datePicker1 = new JXDatePicker(new java.util.Date());
java.util.Date date = datePicker.getDate(); // 用户输入或选择日期后得到日期值
java.util.Date date1 = datePicker1.getDate(); // 用户输入或选择日期后得到日期值
//当然,你也可以指定用户输入日期的格式。
JXDatePicker datePicker2 = new JXDatePicker();
datePicker2.setFormats("yyyy.M.d", "yyyy-M-d");//可同时设置多种格式。setFormats(String...format)

JXMonthView的使用方法:
JXMonthView monthView = new JXMonthView();
monthView.setUnselectableDates(new java.util.Date());// 设置多个不可选择的日期,setUnselectableDates(java.util.Date ... date)
Set<java.util.Date> unselectable = monthView.getSelectionModel().getUnselectableDates();// 获得当前有哪些不可选择日期
monthView.setFlaggedDates(date1,date2)// 设置多个被标记了的日期
Set<java.util.Date> flaggedDates = monthView.getFlaggedDates();// 获得被标记的日期
Calendar calendar = monthView.getCalendar();// 获得日历对象
还有一些可set,get的属性罗列如下:
selectionMode
traversable
firstDayOfWeek
还有更多用法,看API吧。如果使用Maven,你可以自动下载javadoc和javasource。

⑵ 如何直接用java Swing写日期下拉列表框

DateDropFrame extends JFrame
给year和month添加一个ItemListener来实时计算指定年和月中有多少天就可以了.
代码是用Swing写的,不过看你的图,不像是Swing界面.
/**
* Create the frame.
*/
public DateDropFrame() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new FlowLayout(FlowLayout.LEFT));

final JComboBox year = new JComboBox();
year.setModel(new DefaultComboBoxModel(getModel(start, end)));
contentPane.add(year);

final JComboBox month = new JComboBox();
month.setModel(new DefaultComboBoxModel(getModel(1, 12)));
contentPane.add(month);

final JComboBox day = new JComboBox();
contentPane.add(day);

year.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
setDay(year, month, day);
}
});
month.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
setDay(year, month, day);
}
});
setDay(year, month, day);
setContentPane(contentPane);
}

/**
* calculate days in select month & year
*/
private void setDay(JComboBox year, JComboBox month, JComboBox day) {
int y = Integer.parseInt((String) year.getSelectedItem());
int m = Integer.parseInt((String) month.getSelectedItem());
Calendar c = Calendar.getInstance();
c.set(Calendar.YEAR, y);
c.set(Calendar.MONTH, m - 1);
int days = c.getActualMaximum(Calendar.DAY_OF_MONTH);
day.setModel(new DefaultComboBoxModel(getModel(1, days)));

}

/**
* get String array [start, end]
*/
private String[] getModel(int start, int end) {
String[] m = new String[end - start + 1];
for (int i = 0; i < m.length; i++) {
m[i] = String.valueOf(i + start);
}
return m;
}

⑶ 求Java 日历的小程序的源代码

当在一段代码块定义一个变量时,Java就在栈中 为这个变量分配内存空间,当该变量退出该作用域后,Java会自动释放掉为该变量所分配的内存空间,该内存空间可以立即被另作他用。

Java内存分配中的堆

堆内存用来存放由new创建的对象和数组。 在堆中分配的内存,由Java虚拟机的自动垃圾回收器来管理。

⑷ 求一个java swing的日历控件,点击日期能够显示到文本框上的

12306 铁路官网上用的就是这个控件,希望对你有所帮助。

⑸ java用在下拉框里添加日历,记住一定用swing做的,怎么做啊,一点就弹出日历那样,好吧,求大神

将这个包解压 将文件放到WebContent中 然后到jsp 导入

<script type="text/javascript" src="/Rebates/My97DatePicker/WdatePicker.js"></script>

这句代码 由于 我的是WebContent下面还有个Rebates文件夹 所以路径是这样

使用的时候

<input value="${startTime }" id="stime" name="startTime" onclick="WdatePicker({startDate:'%y-%M-01 10:00:00',dateFmt:'yyyy-MM-dd HH:mm:ss',alwaysUseStartDate:true})">

onclick事件 就可以了


⑹ 一个做好的javascript脚本(日历程序)怎么样运用在java Swing界面的某些控件中

这个是2套东西,javascript是运行在浏览器中的,不能用在swing变成中。
你应该找一个java swing 版本的日历控件,也好找。

⑺ java编写windows系统的日历和时钟

import java.lang.Carlendar;
import java.util.Date;

class Mytime extends Thread{ //继承 Thread 类,实现多线程

Thread thread = new Thread();
thread.Start();
public void fun()
{
while(true)
{
Date date = new Date();
thread.Sleep(1000); //设置一秒钟提取一次
txtTime.setText(date) //将时间显示到文本框里,还要提取一下,你自己搞了
}
}
}

日历不写了,
这是动态时间,
你有电脑就要自己调试一下,
我敢肯定是这样做的,
我现在忘记了一些,
这样不行你把thread.Start();
放到while里面去,
但是你改一改绝对会成功,
因为我做了
祝你成功

⑻ 怎么用java写日历

以下是两个类,请楼主分别存成两个java文件:
其中
MainFrame.java是显示日历程序,Clock.java是日历计算程序。编译后运行MainFrame这个类即可。

1.MainFrame.java
---
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Date;
import java.util.Calendar;

import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class MainFrame extends JFrame {
/** *//**
*
*/
private static final long serialVersionUID = 1L;
JPanel panel = new JPanel(new BorderLayout());
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel(new GridLayout(7, 7));
JPanel panel3 = new JPanel();
JLabel[] label = new JLabel[49];
JLabel y_label = new JLabel("年份");
JLabel m_label = new JLabel("月份");
JComboBox com1 = new JComboBox();
JComboBox com2 = new JComboBox();
int re_year, re_month;
int x_size, y_size;
String year_num;
Calendar now = Calendar.getInstance(); // 实例化Calendar

MainFrame() {
super("万年历");
setSize(300, 350);
x_size = (int) (Toolkit.getDefaultToolkit().getScreenSize().getWidth());
y_size = (int) (Toolkit.getDefaultToolkit().getScreenSize().getHeight());
setLocation((x_size - 300) / 2, (y_size - 350) / 2);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel1.add(y_label);
panel1.add(com1);
panel1.add(m_label);
panel1.add(com2);
for (int i = 0; i < 49; i++) {
label[i] = new JLabel("", JLabel.CENTER);// 将显示的字符设置为居中
panel2.add(label[i]);
}
panel3.add(new Clock(this));
panel.add(panel1, BorderLayout.NORTH);
panel.add(panel2, BorderLayout.CENTER);
panel.add(panel3, BorderLayout.SOUTH);
panel.setBackground(Color.white);
panel1.setBackground(Color.white);
panel2.setBackground(Color.white);
panel3.setBackground(Color.white);
Init();
com1.addActionListener(new ClockAction());
com2.addActionListener(new ClockAction());

setContentPane(panel);
setVisible(true);
setResizable(false);
}

class ClockAction implements ActionListener {
public void actionPerformed(ActionEvent arg0) {
int c_year, c_month, c_week;
c_year = Integer.parseInt(com1.getSelectedItem().toString()); // 得到当前所选年份
c_month = Integer.parseInt(com2.getSelectedItem().toString()) - 1; // 得到当前月份,并减1,计算机中的月为0-11
c_week = use(c_year, c_month); // 调用函数use,得到星期几
Resetday(c_week, c_year, c_month); // 调用函数Resetday
}
}

public void Init() {
int year, month_num, first_day_num;
String log[] = { "日", "一", "二", "三", "四", "五", "六" };
for (int i = 0; i < 7; i++) {
label[i].setText(log[i]);
}
for (int i = 0; i < 49; i = i + 7) {
label[i].setForeground(Color.red); // 将星期日的日期设置为红色
}
for (int i = 6; i < 49; i = i + 7) {
label[i].setForeground(Color.green);// 将星期六的日期设置为绿色
}
for (int i = 1; i < 10000; i++) {
com1.addItem("" + i);
}
for (int i = 1; i < 13; i++) {
com2.addItem("" + i);
}
month_num = (int) (now.get(Calendar.MONTH)); // 得到当前时间的月份
year = (int) (now.get(Calendar.YEAR)); // 得到当前时间的年份
com1.setSelectedIndex(year - 1); // 设置下拉列表显示为当前年
com2.setSelectedIndex(month_num); // 设置下拉列表显示为当前月
first_day_num = use(year, month_num);
Resetday(first_day_num, year, month_num);
}

public int use(int reyear, int remonth) {
int week_num;
now.set(reyear, remonth, 1); // 设置时间为所要查询的年月的第一天
week_num = (int) (now.get(Calendar.DAY_OF_WEEK));// 得到第一天的星期
return week_num;
}

@SuppressWarnings("deprecation")
public void Resetday(int week_log, int year_log, int month_log) {
int month_day_score; // 存储月份的天数
int count;
month_day_score = 0;
count = 1;

Date date = new Date(year_log, month_log + 1, 1); // now
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.MONTH, -1); // 前个月
month_day_score = cal.getActualMaximum(Calendar.DAY_OF_MONTH);// 最后一天

for (int i = 7; i < 49; i++) { // 初始化标签
label[i].setText("");
}
week_log = week_log + 6; // 将星期数加6,使显示正确
month_day_score = month_day_score + week_log;
for (int i = week_log; i < month_day_score; i++, count++) {
label[i].setText(count + "");
}
}

public static void main(String[] args) {
JFrame.(true);
new MainFrame();
}
}
2.Clock.java
-----
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Date;
import java.util.Calendar;

import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

//显示时间的类:Clock

/** *//**
* Clock.java
* Summary 数字时间显示
* Created on
* @author
* remark
*/

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.text.SimpleDateFormat;
import java.util.Calendar;
class Clock extends Canvas implements Runnable{
/** *//**
*
*/
private static final long serialVersionUID = 3660124045489727166L;
MainFrame mf;
Thread t;
String time;
public Clock(MainFrame mf){
this.mf=mf;
setSize(280,40);
setBackground(Color.white);
t=new Thread(this); //实例化线程
t.start(); //调用线程
}
public void run(){
while(true){
try{
Thread.sleep(1000); //休眠1秒钟
}catch(InterruptedException e){
System.out.println("异常");
}
this.repaint(100);
}
}
public void paint(Graphics g){
Font f=new Font("宋体",Font.BOLD,16);
SimpleDateFormat SDF=new SimpleDateFormat("yyyy'年'MM'月'dd'日'HH:mm:ss");//格式化时间显示类型
Calendar now=Calendar.getInstance();
time=SDF.format(now.getTime()); //得到当前日期和时间
g.setFont(f);
g.setColor(Color.orange);
g.drawString(time,45,25);
}
}

⑼ 求一个java swing带界面的万年历代码

按照你的要求编写的Java swing 带界面的万年历代码如下

//日历
importjava.awt.BorderLayout;
importjava.awt.Color;
importjava.awt.Font;
importjava.awt.GridLayout;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.util.Calendar;
importjavax.swing.BorderFactory;
importjavax.swing.JButton;
importjavax.swing.JFrame;
importjavax.swing.JLabel;
importjavax.swing.JPanel;
{
JButtonjb1=newJButton("<<");
JButtonjb2=newJButton("<");
JButtonjb3=newJButton(">");
JButtonjb4=newJButton(">>");
JPaneljp1=newJPanel();
JPaneljp2=newJPanel();
JPaneljp3=newJPanel();
JPaneljp4=newJPanel();
JLabeljl1=newJLabel();
JLabeljl2=newJLabel();
JLabel[]jl=newJLabel[49];
String[]week={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
Calendarc=Calendar.getInstance();
intyear,month,day;
intnowyear,nowmonth,nowday;
CCI(){
super("简单日历");
nowyear=c.get(Calendar.YEAR);
nowmonth=c.get(Calendar.MONTH)+1;
nowday=c.get(Calendar.DAY_OF_MONTH);
year=nowyear;
month=nowmonth;
day=nowday;
Strings=year+"年"+month+"月";
jl1.setForeground(Color.RED);
jl1.setFont(newFont(null,Font.BOLD,20));
jl1.setText(s);
jb1.addActionListener(this);
jb2.addActionListener(this);
jb3.addActionListener(this);
jb4.addActionListener(this);
jp1.add(jb1);jp1.add(jb2);jp1.add(jl1);jp1.add(jb3);jp1.add(jb4);
jp2.setLayout(null);
createMonthPanel();
jp2.add(jp3);
jl2.setFont(newFont(null,Font.BOLD,20));
jl2.setText("今天是"+nowyear+"年"+nowmonth+"月"+nowday+"日");
jp4.add(jl2);
add(jp1,BorderLayout.NORTH);
add(jp2,BorderLayout.CENTER);
add(jp4,BorderLayout.SOUTH);
setSize(500,500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
}
@Override
publicvoidactionPerformed(ActionEventae){
if(ae.getSource()==jb1){
year=year-1;
Strings=year+"年"+month+"月";
jl1.setText(s);
jp3.removeAll();
createMonthPanel();
jp3.validate();
}
if(ae.getSource()==jb2){
if(month==1){
year=year-1;
month=12;
}else{
month=month-1;
}
Strings=year+"年"+month+"月";
jl1.setText(s);
jp3.removeAll();
createMonthPanel();
jp3.validate();
}
if(ae.getSource()==jb3){
if(month==12){
year=year+1;
month=1;
}else{
month=month+1;
}
Strings=year+"年"+month+"月";
jl1.setText(s);
jp3.removeAll();
createMonthPanel();
jp3.validate();
}
if(ae.getSource()==jb4){
year=year+1;
Strings=year+"年"+month+"月";
jl1.setText(s);
jp3.removeAll();
createMonthPanel();
jp3.validate();
}
}
publicstaticvoidmain(String[]args){
newCCI();
}
publicintgetMonthDays(intyear,intmonth){
switch(month){
case1:
case3:
case5:
case7:
case8:
case10:
case12:
return31;
case2:
if((year%4==0&&year%100!=0)||year%400==0){
return29;
}else{
return28;
}
default:
return30;
}
}
publicvoidcreateMonthPanel(){
c.set(year,month-1,getMonthDays(year,month));
intweekOfMonth=c.get(Calendar.WEEK_OF_MONTH);
if(weekOfMonth==6){
jp3.setLayout(newGridLayout(7,7));
jp3.setBounds(50,20,420,350);
}else{
jp3.setLayout(newGridLayout(6,7));
jp3.setBounds(50,20,420,300);
}
jp3.setBorder(BorderFactory.createEtchedBorder());
for(inti=0;i<7;i++){
jl[i]=newJLabel(week[i],JLabel.CENTER);
jl[i].setFont(newFont(null,Font.BOLD,20));
jl[i].setBorder(BorderFactory.createEtchedBorder());
jp3.add(jl[i]);
}
c.set(year,month-1,1);
intemptyFirst=c.get(Calendar.DAY_OF_WEEK)-1;
intdaysOfMonth=getMonthDays(year,month);
for(inti=6+emptyFirst;i>=7;i--){
intintyear=year;
intintmonth=month;
if(intmonth==1){
intyear=intyear-1;
intmonth=12;
}else{
intmonth=intmonth-1;
}
intintdays=getMonthDays(intyear,intmonth);
jl[i]=newJLabel((intdays+7-i)+"",JLabel.CENTER);
jl[i].setFont(newFont(null,Font.BOLD,20));
jl[i].setForeground(Color.GRAY);
jl[i].setBorder(BorderFactory.createEtchedBorder());
jp3.add(jl[i]);
}
for(inti=7+emptyFirst;i<daysOfMonth+7+emptyFirst;i++){
jl[i]=newJLabel((i-7-emptyFirst+1)+"",JLabel.CENTER);
jl[i].setFont(newFont(null,Font.BOLD,20));
if((i+1)%7==0||(i+1)%7==1){
jl[i].setForeground(Color.RED);
}elseif((i-7-emptyFirst+1)==nowday&&month==nowmonth&&year==nowyear)
jl[i].setForeground(Color.BLUE);
else
jl[i].setForeground(Color.BLACK);

jl[i].setBorder(BorderFactory.createEtchedBorder());
jp3.add(jl[i]);
}
if(weekOfMonth==6)
for(inti=48;i>=daysOfMonth+emptyFirst+7;i--){
jl[i]=newJLabel((49-i)+"",JLabel.CENTER);
jl[i].setFont(newFont(null,Font.BOLD,20));
jl[i].setForeground(Color.GRAY);
jl[i].setBorder(BorderFactory.createEtchedBorder());
jp3.add(jl[i]);
}
else
for(inti=41;i>=daysOfMonth+emptyFirst+7;i--){
jl[i]=newJLabel((42-i)+"",JLabel.CENTER);
jl[i].setFont(newFont(null,Font.BOLD,20));
jl[i].setForeground(Color.GRAY);
jl[i].setBorder(BorderFactory.createEtchedBorder());
jp3.add(jl[i]);
}
}
}
阅读全文

与javaswing日历相关的资料

热点内容
win11什么时候可以装安卓 浏览:560
java不写this 浏览:999
云点播电影网php源码 浏览:95
pythonclass使用方法 浏览:224
移动加密软件去哪下载 浏览:292
php弹出alert 浏览:208
吉林文档课件加密费用 浏览:136
传感器pdf下载 浏览:287
随车拍app绑定什么设备 浏览:898
方维团购系统源码 浏览:993
linux反弹shell 浏览:159
打印机接口加密狗还能用吗 浏览:301
二板股票源码 浏览:448
度人经pdf 浏览:902
怎么配置android远程服务器地址 浏览:960
java程序员看哪些书 浏览:943
什么app可以免费和外国人聊天 浏览:797
pdf手写笔 浏览:182
别永远伤在童年pdf 浏览:990
爱上北斗星男友在哪个app上看 浏览:421