⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mainframe.java

📁 实现的功能: 1. 设计出万年历的GUI界面。 2. 在GUI界面里显示日期
💻 JAVA
字号:
package clock;

/** *//**
 * MainFrame.java

 */

import java.awt.BorderLayout;
//borderlayout类是一个布置容器的边框布局,它可以对容器组件进行安排,并调整其大小,使其符合下列五个区域:北、南、东、西、中。
import java.awt.Color;
//GridLayout 类是一个布局处理器,它以矩形网格形式对容器的组件进行布置。容器被分成大小相等的矩形,一个矩形中放置一个组件。
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;

import com.sun.org.apache.xerces.internal.impl.xpath.regex.ParseException;


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

    //getinstance 使用指定时区和语言环境获得一个日历。
   //Calendar 类是一个抽象类,它为特定瞬间与一组诸如 YEAR、MONTH、DAY_OF_MONTH、HOUR 等

    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));    //调用Clock类实例,来实现对当前时间的显示
        panel.add(panel1, BorderLayout.NORTH);    //布局北面存放所要选择的年份和月份
        panel.add(panel2, BorderLayout.CENTER);   //存放日历
        panel.add(panel3, BorderLayout.SOUTH);   //存放当前时间
        panel.setBackground(Color.cyan);
        panel1.setBackground(Color.cyan);
        panel2.setBackground(Color.white);
        panel3.setBackground(Color.cyan);
        try {
			Init();
		} catch (java.text.ParseException e) {
			// TODO 自动生成 catch 块
			e.printStackTrace();
		}
        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,得到星期几
            try {
				Resetday(c_week, c_year, c_month);
			} catch (java.text.ParseException e) {
				// TODO 自动生成 catch 块
				e.printStackTrace();
			} // 调用函数Resetday
        }
    }
    
    public void Init() throws java.text.ParseException {
        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 = 1950; i < 2050; 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 - 1950); // 设置下拉列表显示为当前年,1950对应下拉框中是0
        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) throws java.text.ParseException {
    
    	int  month_num = (int) (now.get(Calendar.MONTH))+1; // 得到当前时间的月份
    	
    	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 + "");
            
            //  设置label的浮动提示
            try {
               now.setTime(Lunar.chineseDateFormat.parse(year_log+"年"+(month_log+1)+"月"+count+"日"));
            } catch (ParseException e) {
                e.printStackTrace();
            }
            String f=new Lunar(now).WorldFestival(month_num,count);
           if(f!=null)
          {
        	   label[i].setText(count+f);
           }
            
		  
			label[i].setToolTipText("农历:"+new Lunar(now).cyclical()+new Lunar(now)+" 生肖:"+new Lunar(now).animalsYear()+".节日:"+f);
          
        }
    }

    public static void main(String[] args) {
        JFrame.setDefaultLookAndFeelDecorated(true);
        new MainFrame();        //实例化,运行构造函数显示界面
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -