Java实现图形化界面的日历
作者:SSSuperbeyond 时间:2023-03-04 04:07:47
本文实例为大家分享了Java实现图形化界面日历的具体代码,供大家参考,具体内容如下
此程序主要功能实现了可以根据用户选择的年月日来定位日期,日期的旁边用#加以标注
主界面如下:
代码如下:
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import javax.swing.*;
class awt{
JFrame jf = new JFrame();
Container con = jf.getContentPane();
JPanel p_mid = new JPanel();
JPanel pTime = new JPanel();
JPanel pTime2 = new JPanel();
JComboBox<String> box1 = new JComboBox<String>();
JComboBox<String> box2 = new JComboBox<String>();
JComboBox<String> box3 = new JComboBox<String>();
String year, month, day; //记录年、月、日
Calendar ca = Calendar.getInstance();
public awt() {
//基本设置
jf.setVisible(true);
jf.setLocation(300, 300);
jf.setDefaultCloseOperation(jf.EXIT_ON_CLOSE);
jf.setLayout(new BorderLayout());
jf.setResizable(false);
p_mid.setLayout(new GridLayout(7, 7, 1, 1));
//调用方法
setTime();
week();
Day();
//自适应窗口大小
jf.pack();
}
//下拉列表框 *
public class action1 implements ActionListener {
JComboBox<String> bool;
@Override
public void actionPerformed(ActionEvent e) {
bool = (JComboBox<String>)e.getSource();
year = (String)bool.getSelectedItem();
combox();
}
public void combox() {
if(!year.equals("--年份--")) {
String str[] = new String[13];
str[0] = "--月份--";
for(int i=1; i<=12; i++) {
str[i] = Integer.toString(i);
}
box2.setModel(new DefaultComboBoxModel<String>(str));
}
}
}
class action2 implements ActionListener{
JComboBox<String> bool;
String str[];
@Override
public void actionPerformed(ActionEvent e) {
bool = (JComboBox<String>)e.getSource();
month = (String)bool.getSelectedItem();
combox();
}
public void combox() {
if(!month.equals("--月份--") && (month.matches("[13578]") || month.equals("10") || month.equals("12"))) {
str = new String[32];
str[0] = "--日期--";
for(int i=1; i<=31; i++) {
str[i] = Integer.toString(i);
}
box3.setModel(new DefaultComboBoxModel<String>(str));
}else if(!month.equals("--月份--") && (month.matches("[469]") || month.equals("11"))){
str = new String[31];
str[0] = "--日期--";
for(int i=1; i<=30; i++) {
str[i] = Integer.toString(i);
}
box3.setModel(new DefaultComboBoxModel<String>(str));
}else if(!month.equals("--月份--") && month.equals("2")) {
//判断为闰年还是平年
if(Integer.parseInt(year)%4==0 && Integer.parseInt(year)%100!=0) {
//进入此循环,则为闰年,2月份有29天
str = new String[30];
str[0] = "--日期--";
for(int i=1; i<=29; i++) {
str[i] = Integer.toString(i);
}
box3.setModel(new DefaultComboBoxModel<String>(str));
}else {
//进入此循环,则为平年,2月份有28天
str = new String[29];
str[0] = "--日期--";
for(int i=1; i<=28; i++) {
str[i] = Integer.toString(i);
}
box3.setModel(new DefaultComboBoxModel<String>(str));
}
}
}
}
class action3 implements ActionListener{
JComboBox<String> bool;
@Override
public void actionPerformed(ActionEvent e) {
bool = (JComboBox<String>)e.getSource();
day = (String)bool.getSelectedItem();
}
}
class action4 extends JComponent implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
//实现方法
if(!year.equals("--年份--") && !month.equals("--月份--") && !day.equals("--日期--")) {
//移除组件
con.remove(p_mid);
p_mid.removeAll();
//实现方法
Week_new();
Day_new();
}
}
//新的星期组件
public void Week_new() {
JButton lab1 = new JButton("一");
JButton lab2 = new JButton("二");
JButton lab3 = new JButton("三");
JButton lab4 = new JButton("四");
JButton lab5 = new JButton("五");
JButton lab6 = new JButton("六");
JButton lab7 = new JButton("七");
lab1.setFont(new Font("微软雅黑", Font.BOLD, 15));
lab2.setFont(new Font("微软雅黑", Font.BOLD, 15));
lab3.setFont(new Font("微软雅黑", Font.BOLD, 15));
lab4.setFont(new Font("微软雅黑", Font.BOLD, 15));
lab5.setFont(new Font("微软雅黑", Font.BOLD, 15));
lab6.setFont(new Font("微软雅黑", Font.BOLD, 15));
lab7.setFont(new Font("微软雅黑", Font.BOLD, 15));
p_mid.add(lab1);
p_mid.add(lab2);
p_mid.add(lab3);
p_mid.add(lab4);
p_mid.add(lab5);
p_mid.add(lab6);
p_mid.add(lab7);
}
//新的天数组件
public void Day_new() {
int temp=1;
ca.set(Integer.parseInt(year), Integer.parseInt(month)-1, Integer.parseInt(day));
int Month_max = ca.getActualMaximum(Calendar.DATE);
int day = ca.get(Calendar.DATE);
ca.set(Calendar.DATE, 1);
int Week = ca.get(Calendar.DAY_OF_WEEK)-1;
if(Week == 0) {
Week = 7;
}
for(int i=1; i<=42; i++) {
if(i>=Week && i<Week+Month_max) {
if(temp == day) {
p_mid.add(new Button("#"+temp));
temp++;
continue;
}
p_mid.add(new Button(Integer.toString(temp)));
temp++;
continue;
}
p_mid.add(new Button());
}
con.add(p_mid, BorderLayout.CENTER);
con.validate();
}
}
//------//
public void setTime() {
//声明变量
JLabel labTime = new JLabel();
JButton button = new JButton("查询");
box1.addActionListener(new action1());
box2.addActionListener(new action2());
box3.addActionListener(new action3());
button.addActionListener(new action4());
Date d = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-dd");
String time = sdf.format(d);
labTime.setText("北京时间:"+time);
labTime.setFont(new Font("微软雅黑", Font.BOLD, 15));
box1.addItem("--年份--");
box1.addItem("2016");
box1.addItem("2017");
box1.addItem("2018");
box1.addItem("2019");
box2.addItem("--月份--");
box3.addItem("--日期--");
pTime.setLayout(new FlowLayout(FlowLayout.LEFT,20,5));
pTime2.setLayout(new FlowLayout(FlowLayout.CENTER,1,0));
pTime.add(labTime);
pTime2.add(box1);
pTime2.add(box2);
pTime2.add(box3);
pTime2.add(button);
pTime.add(pTime2);
con.add(pTime, BorderLayout.NORTH);
}
public void week() {
JButton lab1 = new JButton("一");
JButton lab2 = new JButton("二");
JButton lab3 = new JButton("三");
JButton lab4 = new JButton("四");
JButton lab5 = new JButton("五");
JButton lab6 = new JButton("六");
JButton lab7 = new JButton("七");
lab1.setFont(new Font("微软雅黑", Font.BOLD, 15));
lab2.setFont(new Font("微软雅黑", Font.BOLD, 15));
lab3.setFont(new Font("微软雅黑", Font.BOLD, 15));
lab4.setFont(new Font("微软雅黑", Font.BOLD, 15));
lab5.setFont(new Font("微软雅黑", Font.BOLD, 15));
lab6.setFont(new Font("微软雅黑", Font.BOLD, 15));
lab7.setFont(new Font("微软雅黑", Font.BOLD, 15));
p_mid.add(lab1);
p_mid.add(lab2);
p_mid.add(lab3);
p_mid.add(lab4);
p_mid.add(lab5);
p_mid.add(lab6);
p_mid.add(lab7);
}
public void Day() {
int temp=1;
int Month_max = ca.getActualMaximum(Calendar.DATE); //当前月有几天
int day = ca.get(Calendar.DATE); //当前日期
ca.set(Calendar.DATE, 1);
int Week = ca.get(Calendar.DAY_OF_WEEK)-1; //星期几
if(Week == 0) {
Week = 7;
}
for(int i=1; i<=42; i++) {
Button b = new Button();
if(i>=Week && i<Week+Month_max) {
if(temp == day) {
p_mid.add(new Button("#"+temp));
temp++;
continue;
}
p_mid.add(new Button(Integer.toString(temp)));
temp++;
continue;
}
p_mid.add(new Button());
}
con.add(p_mid, BorderLayout.CENTER);
}
}
public class application {
public static void main(String[] args) {
awt at = new awt();
}
}
实现过程中碰到的主要问题有:
1、关于JComBobox组件的更新问题,开始是直接修改值和将组件移除后重新添加,发现都行不通,在查看了JComBobox的源码之后发现底层是调用setModel方法来实现添加元素的,于是我将使用此方法来更新下拉列表框的值,成功解决此问题
box.setModel(new DefaultComboBoxModel<String>(str));
2、将按钮移除重新添加后必须将界面框最小化之后打开才能出现界面,查阅资料后发现Container有一个validate()方法可以用来重绘,于是便添加此方法,成功解决问题
con.validate();
来源:https://blog.csdn.net/SSSuperbeyond/article/details/103148610
标签:Java,日历
0
投稿
猜你喜欢
C#使用远程服务调用框架Apache Thrift
2023-05-07 01:05:01
Java中的break和continue关键字的使用方法总结
2022-07-13 11:50:46
java String 转成Double二维数组的方法
2023-04-28 12:11:02
深入探究Spring底层核心原理
2023-03-05 08:32:16
使用Java实现三种等级的扫雷游戏(完整版)
2023-05-10 07:34:17
Android 实现沉浸式状态栏的方法
2023-05-02 21:42:11
Java多态性抽象类与接口细致详解
2022-08-22 00:06:11
详解C#中HashTable的用法
2023-07-17 04:42:07
Java File类 mkdir 不能创建多层目录的解决
2022-12-01 09:20:18
Android平台预置GMS包后关机闹钟失效问题及解决方法
2022-12-31 05:52:33
关于Java中修饰符的总结(fina除外)
2023-11-22 23:15:57
Java多线程编程之ThreadLocal线程范围内的共享变量
2022-03-10 00:41:05
在Android设备上搭建Web服务器的方法
2023-06-23 23:38:36
Android自定义View实现渐变色进度条
2022-11-25 08:27:17
C#队列Queue多线程用法实例
2023-03-09 20:12:09
android自定义圆形倒计时显示控件
2022-12-13 00:27:30
Android实现图像切换器
2023-11-27 00:45:41
spring mvc中的@PathVariable获得请求url中的动态参数
2023-08-22 22:08:40
C# Path类---文件路径解读
2022-05-20 05:58:47
Java中List常用操作比for循环更优雅的写法示例
2023-08-08 23:25:14