java GUI简单日历查看

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;

public class swingCalendar extends JFrame implements ActionListener {
    public static swingCalendar u1;
    private JTextField jTextField;
    private JTextArea jTextArea;
    private JButton[] jButton;
    private JMenuItem[] jMenuItems;
    private JMenu[] jMenus;
    private JMenuBar mbar;
    private JComboBox comboBox;
    private JLabel jLabel;
    public swingCalendar(){
        super("日历");
        jButton = new JButton[1];
        jMenus = new JMenu[3];
        jButton = new JButton[1];
        comboBox = new JComboBox<String>();
        jLabel = new JLabel();
        jTextField = new JTextField();
        jTextArea = new JTextArea();
        jMenuItems = new JMenuItem[4];

        jMenuItems[0] = new JMenuItem("关于我");
        jMenuItems[1] = new JMenuItem("帮助文档");
        jMenuItems[2] = new JMenuItem("退出");
        jMenuItems[3] = new JMenuItem("清零");
        jMenuItems[0].addActionListener(this);
        jMenuItems[1].addActionListener(this);
        jMenuItems[2].addActionListener(this);
        jMenuItems[3].addActionListener(this);

        jMenus[0] = new JMenu("查看(V)");
        jMenus[1] = new JMenu("编辑(E)");
        jMenus[2] = new JMenu("帮助(H)");
        for (int i = 0 ; i < jMenus.length;i++){
            jMenus[i].addActionListener(this);
        }
        jMenus[2].add(jMenuItems[0]);
        jMenus[2].add(jMenuItems[1]);
        jMenus[2].add(jMenuItems[2]);
        jMenus[1].add(jMenuItems[3]);

        comboBox.addItem("一月");
        comboBox.addItem("二月");
        comboBox.addItem("三月");
        comboBox.addItem("四月");
        comboBox.addItem("五月");
        comboBox.addItem("六月");
        comboBox.addItem("七月");
        comboBox.addItem("八月");
        comboBox.addItem("九月");
        comboBox.addItem("十月");
        comboBox.addItem("十一月");
        comboBox.addItem("十二月");
        comboBox.addActionListener(this);
        comboBox.setBounds(100,20,70,30);

        jButton[0] = new JButton("确定");
        jButton[0].setBounds(350,20,70,30);
        jButton[0].setForeground(Color.blue);
        jButton[0].setFont(new Font("宋体",Font.PLAIN,14));
        jButton[0].addActionListener(this);

        jTextField= new JTextField("",40);
        jTextField.setFont(new Font("宋体",Font.PLAIN,11));
        jTextField.setForeground(Color.black);
        jTextField.setBounds(10,20,50,30);
        jTextField.setHorizontalAlignment(JTextField.RIGHT);
        jTextField.addActionListener(this);
        jTextField.setEditable(true);

        mbar = new JMenuBar();
        mbar.add(jMenus[0]);
        mbar.add(jMenus[1]);
        mbar.add(jMenus[2]);
        this.setJMenuBar(mbar);

        jTextArea = new JTextArea();
        jTextArea.setFont(new Font("宋体",Font.BOLD,14));
        jTextArea.setForeground(Color.black);
        jTextArea.setBounds(50,110,400,300);
        jTextArea.setEditable(false);

        jLabel.setBounds(230,20,50,30);
        jLabel.setText("");
        jLabel.setForeground(Color.red);

        Container winContainer = this.getContentPane();
        winContainer.setLayout(null);
        winContainer.add(jTextField);
        winContainer.add(jTextArea);
        winContainer.add(jButton[0]);
        winContainer.add(comboBox);
        winContainer.add(jTextArea);
        winContainer.add(jLabel);
        this.setTitle("日历");
        this.setLocation(300, 300);
        this.setSize(500,500);
        this.setVisible(true);

    }

    public static void main(String[] args) {
        u1 = new swingCalendar();
    }
    public void actionPerformed(ActionEvent e){
        if(e.getSource()==jButton[0]) {
            jLabel.setText("");
            jTextArea.setText("");
            String aa = jTextField.getText();
            if (aa.charAt(0)<‘0‘||aa.charAt(0)>‘9‘){
                JOptionPane.showMessageDialog(u1, "无法读取你的输入,请重新输入", "提示消息", 1);
                comboBox.setSelectedIndex(0);
                jTextField.setText("");
            }
            else{
                int year = Integer.valueOf(aa);
                if (year <= 250) {
                    JOptionPane.showMessageDialog(u1, "对不起,本日历无法查询到"+year+"年的日历\n\t请点击菜单栏-帮助-联系工程师", "提示消息", 1);
                    comboBox.setSelectedIndex(0);
                    jTextField.setText("");
                }
                else {
                    jLabel.setText(aa);

                    String[] English = {"一", "二", "三", "四", "五", "六", "日"};
                    String[] Month = {"一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月",};
                    Calendar calendar = Calendar.getInstance();
                    StringBuffer stringBuffer = new StringBuffer("");
                    for (int j = 0; j < English.length; j++) {
                        stringBuffer.append(English[j] + "\t");
                    }
                    stringBuffer.append("\n");
                    int index = comboBox.getSelectedIndex();
                    calendar.clear();
                    calendar.set(Calendar.YEAR, year);

                    switch (index) {
                        case 0: {
                            calendar.set(Calendar.MONTH, index);
                            int Month_Days = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
                            int First_Day = calendar.get(Calendar.DAY_OF_WEEK) - 1;
                            int n = 0;
                            if (First_Day > 0) {
                                for (int k = 2; k <= First_Day; k++) {
                                    n = First_Day - 1;
                                    stringBuffer.append("\t");
                                }
                            }
                            if (First_Day == 0) {
                                n = 6;
                                for (int k = 1; k < 7; k++) {
                                    stringBuffer.append("\t");
                                }
                            }
                            for (int k = 1; k <= Month_Days; k++) {
                                stringBuffer.append(k + "\t");
                                n++;
                                if (n % 7 == 0) {
                                    stringBuffer.append("\n");
                                }

                            }
                            break;
                        }
                        case 1: {
                            calendar.set(Calendar.MONTH, index);
                            int Month_Days = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
                            int First_Day = calendar.get(Calendar.DAY_OF_WEEK) - 1;
                            int n = 0;
                            if (First_Day > 0) {
                                for (int k = 2; k <= First_Day; k++) {
                                    n = First_Day - 1;
                                    stringBuffer.append("\t");
                                }
                            }
                            if (First_Day == 0) {
                                n = 6;
                                for (int k = 1; k < 7; k++) {
                                    stringBuffer.append("\t");
                                }
                            }
                            for (int k = 1; k <= Month_Days; k++) {
                                stringBuffer.append(k + "\t");
                                n++;
                                if (n % 7 == 0) {
                                    stringBuffer.append("\n");
                                }

                            }

                            break;
                        }
                        case 2: {
                            calendar.set(Calendar.MONTH, index);
                            int Month_Days = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
                            int First_Day = calendar.get(Calendar.DAY_OF_WEEK) - 1;
                            int n = 0;
                            if (First_Day > 0) {
                                for (int k = 2; k <= First_Day; k++) {
                                    n = First_Day - 1;
                                    stringBuffer.append("\t");
                                }
                            }
                            if (First_Day == 0) {
                                n = 6;
                                for (int k = 1; k < 7; k++) {
                                    stringBuffer.append("\t");
                                }
                            }
                            for (int k = 1; k <= Month_Days; k++) {
                                stringBuffer.append(k + "\t");
                                n++;
                                if (n % 7 == 0) {
                                    stringBuffer.append("\n");
                                }

                            }
                            break;
                        }
                        case 3: {
                            calendar.set(Calendar.MONTH, index);
                            int Month_Days = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
                            int First_Day = calendar.get(Calendar.DAY_OF_WEEK) - 1;
                            int n = 0;
                            if (First_Day > 0) {
                                for (int k = 2; k <= First_Day; k++) {
                                    n = First_Day - 1;
                                    stringBuffer.append("\t");
                                }
                            }
                            if (First_Day == 0) {
                                n = 6;
                                for (int k = 1; k < 7; k++) {
                                    stringBuffer.append("\t");
                                }
                            }
                            for (int k = 1; k <= Month_Days; k++) {
                                stringBuffer.append(k + "\t");
                                n++;
                                if (n % 7 == 0) {
                                    stringBuffer.append("\n");
                                }

                            }
                            break;
                        }
                        case 4: {
                            calendar.set(Calendar.MONTH, index);
                            int Month_Days = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
                            int First_Day = calendar.get(Calendar.DAY_OF_WEEK) - 1;
                            int n = 0;
                            if (First_Day > 0) {
                                for (int k = 2; k <= First_Day; k++) {
                                    n = First_Day - 1;
                                    stringBuffer.append("\t");
                                }
                            }
                            if (First_Day == 0) {
                                n = 6;
                                for (int k = 1; k < 7; k++) {
                                    stringBuffer.append("\t");
                                }
                            }
                            for (int k = 1; k <= Month_Days; k++) {
                                stringBuffer.append(k + "\t");
                                n++;
                                if (n % 7 == 0) {
                                    stringBuffer.append("\n");
                                }

                            }
                            break;
                        }
                        case 5: {
                            calendar.set(Calendar.MONTH, index);
                            int Month_Days = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
                            int First_Day = calendar.get(Calendar.DAY_OF_WEEK) - 1;
                            int n = 0;
                            if (First_Day > 0) {
                                for (int k = 2; k <= First_Day; k++) {
                                    n = First_Day - 1;
                                    stringBuffer.append("\t");
                                }
                            }
                            if (First_Day == 0) {
                                n = 6;
                                for (int k = 1; k < 7; k++) {
                                    stringBuffer.append("\t");
                                }
                            }
                            for (int k = 1; k <= Month_Days; k++) {
                                stringBuffer.append(k + "\t");
                                n++;
                                if (n % 7 == 0) {
                                    stringBuffer.append("\n");
                                }

                            }
                            break;
                        }
                        case 6: {
                            calendar.set(Calendar.MONTH, index);
                            int Month_Days = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
                            int First_Day = calendar.get(Calendar.DAY_OF_WEEK) - 1;
                            int n = 0;
                            if (First_Day > 0) {
                                for (int k = 2; k <= First_Day; k++) {
                                    n = First_Day - 1;
                                    stringBuffer.append("\t");
                                }
                            }
                            if (First_Day == 0) {
                                n = 6;
                                for (int k = 1; k < 7; k++) {
                                    stringBuffer.append("\t");
                                }
                            }
                            for (int k = 1; k <= Month_Days; k++) {
                                stringBuffer.append(k + "\t");
                                n++;
                                if (n % 7 == 0) {
                                    stringBuffer.append("\n");
                                }

                            }
                            break;
                        }
                        case 7: {
                            calendar.set(Calendar.MONTH, index);
                            int Month_Days = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
                            int First_Day = calendar.get(Calendar.DAY_OF_WEEK) - 1;
                            int n = 0;
                            if (First_Day > 0) {
                                for (int k = 2; k <= First_Day; k++) {
                                    n = First_Day - 1;
                                    stringBuffer.append("\t");
                                }
                            }
                            if (First_Day == 0) {
                                n = 6;
                                for (int k = 1; k < 7; k++) {
                                    stringBuffer.append("\t");
                                }
                            }
                            for (int k = 1; k <= Month_Days; k++) {
                                stringBuffer.append(k + "\t");
                                n++;
                                if (n % 7 == 0) {
                                    stringBuffer.append("\n");
                                }

                            }
                            break;
                        }
                        case 8: {
                            calendar.set(Calendar.MONTH, index);
                            int Month_Days = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
                            int First_Day = calendar.get(Calendar.DAY_OF_WEEK) - 1;
                            int n = 0;
                            if (First_Day > 0) {
                                for (int k = 2; k <= First_Day; k++) {
                                    n = First_Day - 1;
                                    stringBuffer.append("\t");
                                }
                            }
                            if (First_Day == 0) {
                                n = 6;
                                for (int k = 1; k < 7; k++) {
                                    stringBuffer.append("\t");
                                }
                            }
                            for (int k = 1; k <= Month_Days; k++) {
                                stringBuffer.append(k + "\t");
                                n++;
                                if (n % 7 == 0) {
                                    stringBuffer.append("\n");
                                }

                            }
                            break;
                        }
                        case 9: {
                            calendar.set(Calendar.MONTH, index);
                            int Month_Days = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
                            int First_Day = calendar.get(Calendar.DAY_OF_WEEK) - 1;
                            int n = 0;
                            if (First_Day > 0) {
                                for (int k = 2; k <= First_Day; k++) {
                                    n = First_Day - 1;
                                    stringBuffer.append("\t");
                                }
                            }
                            if (First_Day == 0) {
                                n = 6;
                                for (int k = 1; k < 7; k++) {
                                    stringBuffer.append("\t");
                                }
                            }
                            for (int k = 1; k <= Month_Days; k++) {
                                stringBuffer.append(k + "\t");
                                n++;
                                if (n % 7 == 0) {
                                    stringBuffer.append("\n");
                                }

                            }
                            break;
                        }
                        case 10: {
                            calendar.set(Calendar.MONTH, index);
                            int Month_Days = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
                            int First_Day = calendar.get(Calendar.DAY_OF_WEEK) - 1;
                            int n = 0;
                            if (First_Day > 0) {
                                for (int k = 2; k <= First_Day; k++) {
                                    n = First_Day - 1;
                                    stringBuffer.append("\t");
                                }
                            }
                            if (First_Day == 0) {
                                n = 6;
                                for (int k = 1; k < 7; k++) {
                                    stringBuffer.append("\t");
                                }
                            }
                            for (int k = 1; k <= Month_Days; k++) {
                                stringBuffer.append(k + "\t");
                                n++;
                                if (n % 7 == 0) {
                                    stringBuffer.append("\n");
                                }

                            }
                            break;
                        }
                        case 11: {
                            calendar.set(Calendar.MONTH, index);
                            int Month_Days = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
                            int First_Day = calendar.get(Calendar.DAY_OF_WEEK) - 1;
                            int n = 0;
                            if (First_Day > 0) {
                                for (int k = 2; k <= First_Day; k++) {
                                    n = First_Day - 1;
                                    stringBuffer.append("\t");
                                }
                            }
                            if (First_Day == 0) {
                                n = 6;
                                for (int k = 1; k < 7; k++) {
                                    stringBuffer.append("\t");
                                }
                            }
                            for (int k = 1; k <= Month_Days; k++) {
                                stringBuffer.append(k + "\t");
                                n++;
                                if (n % 7 == 0) {
                                    stringBuffer.append("\n");
                                }

                            }
                            break;
                        }

                    }

                    jTextArea.setText(String.valueOf(stringBuffer));

                }
            }

        }
        else if (e.getSource()==jMenuItems[0]){
            JOptionPane.showMessageDialog(u1,"开发者洪家骏","开发者信息",1);

        }
        else if (e.getSource()==jMenuItems[1]){
            JOptionPane.showMessageDialog(u1,"暂时没有开发文档","帮助文档",1);
        }
        else if (e.getSource()==jMenuItems[2]){
            int aa = JOptionPane.showConfirmDialog(u1,"您确定要退出吗!","退出",1);
            if(aa==0) {
                System.exit(0);
            }

        }
        else if(e.getSource()==jMenuItems[3]){
            jLabel.setText("");
            jTextArea.setText("");
            jTextField.setText("");
            comboBox.setSelectedIndex(0);
            JOptionPane.showMessageDialog(u1,"清空成功","清空",1);
        }

        }

    }

利用Calendar(国际日期类)与 java的swing 做的一个GUI 简单日历查看,并进行了一些事件捕获。

原文地址:https://www.cnblogs.com/algorithmvcerhj/p/9090958.html

时间: 2024-10-29 02:47:15

java GUI简单日历查看的相关文章

【J2SE】java实现简单照片查看器

程序运行结果: 工程结构图: 程序代码: import java.awt.BorderLayout; import java.awt.FileDialog; import java.awt.Graphics; import java.awt.Image; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.Ke

java GUI简单记事本

代码: 1 import java.awt.*; 2 import java.awt.event.*; 3 import java.io.*; 4 class MyMenu 5 { 6 //定义该窗体所需的组件,方便其他函数引用 7 private Frame f; 8 private Button btn; //按钮 9 private Button btn1; 10 private Label label; //标签 11 private TextArea ta ; //文本区域 12 pr

Java GUI 简单台球游戏模型

完成效果: 1 package com.neuedu.test; 2 3 import java.awt.Frame; 4 import java.awt.Graphics; 5 import java.awt.event.WindowAdapter; 6 import java.awt.event.WindowEvent; 7 8 import com.neuedu.utill.GameUtill; 9 10 public class TaiQiu extends Frame { 11 pub

第14篇-JAVA GUI编程

第14篇-JAVA GUI编程 每篇一句 :道路一开始开辟的时候总是存在障碍的 初学心得: 原本下定决心才能开始的事情也变得理所当然 (笔者:JEEP/711)[JAVA笔记 | 时间:2017-04-25| JAVA GUI编程 ] 1.什么是界面 图形用户界面(Graphical User Interface,简称 GUI,又称图形用户接口)是指采用图形方式显示的计算机操作用户界面 与早期计算机使用的命令行界面相比,图形界面对于用户来说在视觉上更易于接受 2.Java 基础类 JFC 的基本

Java Log Viewer日志查看器

工欲善其事必先利其器 在投奔怒海--一个Domino老程序员眼里的Java开发我提到目前所做的Java开发中遇到的大量日志之问题.服务器控制台刷屏似地滚动,日志文件飞快地增长,debug的时候相关信息常常被例常日志淹没,在众多Logger滔滔不绝的输出下,要找出错误所在有时都挺费眼.日志的作用无可置疑,大型项目中日志海量产生也不少见.在以前的Domino开发中,Domino服务器在产生日志方面相当节制,程序员对日志的依赖程度也不高.转换到Java开发,我用一直最喜爱的文本编辑器EditPlus肉

java 模拟简单搜索

Java 模拟简单搜索 实体类 package org.dennisit.entity; /** * * * @version : 1.0 * * @author : 苏若年 <a href="mailto:[email protected]">发送邮件</a> * * @since : 1.0 创建时间: 2013-4-8 下午04:51:03 * * @function: TODO * */ public class Medicine { private I

Java GUI 基础 Eight Puzzle (拼图游戏)

很简单使用java GUI 制作一个简单的拼图游戏 // main package HW1; import java.io.IOException; import javax.swing.Icon; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JLabel; public class HW1 extends JFrame{ /** * */ public HW1_0586(String s

如何快速配好java环境变量和查看电脑上安装JDK的版本位数

[java] view plaincopy 今天一个新手在群里问自己的Eclipse打不开,然后我是属于那种热心肠的人,一般自己知道的就会告诉他们,看了下,是环境变量没有配好,反正我觉得配环境比较简单,现在就教大家简单的环境变量配法 path ----- 输入jdk里面bin的目录,例如D:\mtools\win32-x86\java\jdk1.7.0\bin classpath   ------直接输入一点 注意是"." OK 然后在cmd下面输入java -version 我告诉他

JAVA 实现 简单的 HTTP服务器

关于JAVA的HTTP服务器的简单实现,还望大家给出宝贵建议,谢谢. 1.首先来说下HTTP所有的状态碼,相信对于网页开发的博友们,肯定不陌生. 状态码 状态码英文名称 中文描述 100 Continue 继续.客户端应继续其请求 101 Switching Protocols 切换协议.服务器根据客户端的请求切换协议.只能切换到更高级的协议,例如,切换到HTTP的新版本协议 200 OK 请求成功.一般用于GET与POST请求 201 Created 已创建.成功请求并创建了新的资源 202