资源预览内容
第1页 / 共6页
第2页 / 共6页
第3页 / 共6页
第4页 / 共6页
第5页 / 共6页
第6页 / 共6页
亲,该文档总共6页全部预览完了,如果喜欢就下载吧!
资源描述
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.*;public class calendertest public static void main(String args) JFrame.setDefaultLookAndFeelDecorated(true);new MainFrame();class MainFrame extends JFrame JPanel panel = new JPanel(new BorderLayout();JPanel panel1 = new JPanel();JPanel panel2 = new JPanel(new GridLayout(7, 7);JLabel label = new JLabel49;JLabel y_label = new JLabel(年份);JLabel m_label = new JLabel(月份);JTextField T1=new JTextField(6);JTextField T2=new JTextField(6);JButton JB1=new JButton(转到);int re_year, re_month;int x_size, y_size;String year_num;Calendar now = Calendar.getInstance(); / 实例化 CalendarMainFrame() 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(T1);panel1.add(m_label);panel1.add(T2);panel1.add(JB1);for (int i = 0; i 49; i+) labeli = new JLabel(, JLabel.CENTER);/ 将显示的字符设置为居中panel2.add(labeli);panel.add(panel1, BorderLayout.NORTH);panel.add(panel2, BorderLayout.CENTER);panel.setBackground(Color.white);panel1.setBackground(Color.white);panel2.setBackground(Color.white);Init();JB1.addActionListener(new ClockAction();setContentPane(panel);setVisible(true);setResizable(false);public void Init() int year, month_num, first_day_num;String log = 日, 一, 二, 三, 四, 五, 六 ;for (int i = 0; i 7; i+) labeli.setText(logi);for (int i = 0; i 49; i = i + 7) labeli.setForeground(Color.red); / 将星期日的日期设置为红色for (int i = 6; i 49; i = i + 7) labeli.setForeground(Color.green);/ 将星期六的日期设置为绿色month_num = (int) (now.get(Calendar.MONTH); / 得到当前时间的月份year = (int) (now.get(Calendar.YEAR); / 得到当前时间的年份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;class ClockAction implements ActionListener public void actionPerformed(ActionEvent arg0) int c_year, c_month, c_week;c_year = Integer.parseInt(T1.getText().toString(); / 得到当前所选年份c_month = Integer.parseInt(T2.getText().toString() - 1; / 得到当前月份,并减 1,计算机中的月为 011c_week = use(c_year, c_month); / 调用函数 use,得到星期几Resetday(c_week, c_year, c_month); / 调用函数 Resetdaypublic 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); / nowCalendar 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+) / 初始化标签labeli.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+) labeli.setText(count + );/*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 JLabel49;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(); / 实例化 CalendarMainFrame() 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+) labeli = new JLabel(, JLabel.CENTER);/ 将显示的字符设置为居中panel2.add(labeli);/ 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,计
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号