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

📄 calendarjdialog.java

📁 学生管理系统。使用java编程
💻 JAVA
字号:
package com.hb.stumanagesys;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import java.util.Date;
import java.util.GregorianCalendar;

import javax.swing.JButton;
import javax.swing.JComboBox;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.SpinnerListModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class CalendarJDialog extends javax.swing.JDialog /*implements ActionListener */{
	private JPanel mainPanel;
	private JButton todayButton;
	private JLabel satLabel;
	private JLabel fridayLabel;
	private JLabel thursLabel;
	private JLabel wedLabel;
	private JLabel tuesLabel;
	private JLabel monLabel;
	private JLabel sunLabel;
	private JLabel monthLabel;
	private JComboBox monthComboBox;
	private JLabel yearLabel;
	private JSpinner yearSpinner;
	private JPanel buttonPanel;
	private JButton nextButton;
	private JButton oderButton;
	private JButton[] button = new JButton[42]; //定义一个数组用来存放日期 
	Date nowDate = new Date();
	int nowYear= nowDate.getYear()+1900;//获取并保存当前年份
	int nowMonth = nowDate.getMonth();//获取并保存当前月份
	int toDay = nowDate.getDate();//获取并保存当前日
    private String date="";
	
	
	
	
	
	/**
	* Auto-generated main method to display this JDialog
	*/
	public static void main(String[] args) {
		JFrame frame = new JFrame();
		CalendarJDialog inst = new CalendarJDialog(frame);
		inst.setVisible(true);
		inst.setDay();
	}
	
	public CalendarJDialog(JFrame frame) {
		super(frame);
		initGUI();
		setLocationRelativeTo(null);//设置居中显示
		setResizable(false); //面板的大小不能变化 
	}
	
	private void initGUI() {
		try {
			{
				mainPanel = new JPanel();
				getContentPane().add(mainPanel, BorderLayout.CENTER);
				mainPanel.setLayout(null);
				{
					oderButton = new JButton();
					mainPanel.add(oderButton);
					oderButton.setText("\u4e0a\u4e00\u5e74");
					oderButton.setBounds(21, 7, 77, 21);
					oderButton.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent evt) {
							oderButtonActionPerformed(evt);
						}
					});
				}
				{
					nextButton = new JButton();
					mainPanel.add(nextButton);
					nextButton.setText("\u4e0b\u4e00\u5e74");
					nextButton.setBounds(294, 7, 77, 21);
					nextButton.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent evt) {
							nextButtonActionPerformed(evt);
						}
					});
				}
				{
					String [] year=new String [80];//能查询的年份为当前年份的前50年,后30年
					for (int i=nowYear-50,j=0 ;i<nowYear+30 && j<80;i++,j++)
					{
						year[j]=i+"";
						
					}
					SpinnerListModel yearSpinnerModel = new SpinnerListModel(
						year);
					yearSpinnerModel.setValue(""+nowYear);
					yearSpinner = new JSpinner();
					mainPanel.add(yearSpinner);
					yearSpinner.setModel(yearSpinnerModel);
					yearSpinner.setBounds(126, 7, 70, 21);
					yearSpinner.addChangeListener(new ChangeListener() {
						public void stateChanged(ChangeEvent evt) {
							yearSpinnerStateChanged(evt);
						}
					});
					yearSpinner.addMouseWheelListener(new MouseWheelListener() {
						public void mouseWheelMoved(MouseWheelEvent evt) {
							yearSpinnerMouseWheelMoved(evt);
						}
					});
				}
				{
					yearLabel = new JLabel();
					mainPanel.add(yearLabel);
					yearLabel.setText("\u5e74");
					yearLabel.setBounds(203, 7, 14, 21);
				}
				{
					monthComboBox = new JComboBox();
					for (int i = 1; i <=12; i++) { 
						monthComboBox.addItem(i + ""); 
						} 
					monthComboBox.setSelectedItem((1+nowMonth)+"");
					mainPanel.add(monthComboBox);
					monthComboBox.setBounds(217, 7, 42, 21);
					monthComboBox.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent evt) {
							monthComboBoxActionPerformed(evt);
						}
					});
				}
				{
					monthLabel = new JLabel();
					mainPanel.add(monthLabel);
					monthLabel.setText("\u6708");
					monthLabel.setBounds(259, 7, 14, 21);
				}
				{
					sunLabel = new JLabel();
					mainPanel.add(sunLabel);
					sunLabel.setText("\u661f\u671f\u5929");
					sunLabel.setBounds(0, 35, 56, 21);
				}
				{
					monLabel = new JLabel();
					mainPanel.add(monLabel);
					monLabel.setText("\u661f\u671f\u4e00");
					monLabel.setBounds(56, 35, 56, 21);
				}
				{
					tuesLabel = new JLabel();
					mainPanel.add(tuesLabel);
					tuesLabel.setText("\u661f\u671f\u4e8c");
					tuesLabel.setBounds(112, 35, 56, 21);
				}
				{
					wedLabel = new JLabel();
					mainPanel.add(wedLabel);
					wedLabel.setText("\u661f\u671f\u4e09");
					wedLabel.setBounds(168, 35, 56, 21);
				}
				{
					thursLabel = new JLabel();
					mainPanel.add(thursLabel);
					thursLabel.setText("\u661f\u671f\u56db");
					thursLabel.setBounds(224, 35, 56, 21);
				}
				{
					fridayLabel = new JLabel();
					mainPanel.add(fridayLabel);
					fridayLabel.setText("\u661f\u671f\u4e94");
					fridayLabel.setBounds(280, 35, 56, 21);
				}
				{
					satLabel = new JLabel();
					mainPanel.add(satLabel);
					satLabel.setText("\u661f\u671f\u516d");
					satLabel.setBounds(336, 35, 56, 21);
				}
				{
					todayButton = new JButton();
					mainPanel.add(todayButton);
					todayButton.setText("\u4eca\u5929\u662f:"+this.nowYear+"-"+(this.nowMonth+1)+"-"+this.toDay);
					todayButton.setBounds(77, 238, 245, 28);
					todayButton.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent evt) {
							jButtonActionPerformed(evt);
						}
					});
				}
				{
					buttonPanel = new JPanel();
					GridLayout buttonPanelLayout = new GridLayout(6, 7);
					buttonPanelLayout.setColumns(7);
					buttonPanelLayout.setHgap(5);
					buttonPanelLayout.setVgap(5);
					buttonPanelLayout.setRows(6);
					buttonPanel.setLayout(buttonPanelLayout);
					for (int i=0;i<42;i++)
					{
						 button[i] = new JButton("");
						 buttonPanel.add(button[i]);
						 button[i].addActionListener(new ActionListener() {
								public void actionPerformed(ActionEvent evt) {
									jButtonActionPerformed(evt);
								}
							});
						 //button[i].addActionListener(l);
					}
					mainPanel.add(buttonPanel);
					buttonPanel.setBounds(0, 56, 392, 182);
				}
				
			}
			this.setDay();//实例化时设置日历显示
			this.setSize(400, 300);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**"下一年"按钮响应方法
	 * 
	 * @param evt
	 */
	private void nextButtonActionPerformed(ActionEvent evt) {
		if (Integer.parseInt((String) yearSpinner.getValue())<(this.nowYear+29))
		{
			yearSpinner.setValue(yearSpinner.getNextValue());
		}
	}
	
	/**"上一年"按钮响应方法
	 * 
	 * @param evt
	 */
	private void oderButtonActionPerformed(ActionEvent evt) {
		if (Integer.parseInt((String) yearSpinner.getValue())>this.nowYear-50)
		{
			yearSpinner.setValue(yearSpinner.getPreviousValue());
		}
	}
	
	/**年选择框鼠标滚轮响应方法
	 * 
	 * @param evt
	 */
	private void yearSpinnerMouseWheelMoved(MouseWheelEvent evt) {
		if (evt.getWheelRotation()<0 && Integer.parseInt((String) yearSpinner.getValue())<this.nowYear+29)
		{
			yearSpinner.setValue(yearSpinner.getNextValue());
		}
		else if (evt.getWheelRotation()>0 && Integer.parseInt((String) yearSpinner.getValue())>this.nowYear-50)
		{
			yearSpinner.setValue(yearSpinner.getPreviousValue());
		}
	}
	
	/**设置日历显示
	 * 
	 *
	 */
	public void setDay()
	{
		int getYear=0,getMonth=0;//存放要显示的年份、月份
		int getOderMonth=0;//存放要显示月份的前一个月
		getYear=Integer.parseInt((String)this.yearSpinner.getValue().toString());
		getMonth=Integer.parseInt((String) this.monthComboBox.getSelectedItem());
		Date dt = new Date(getYear-1900,getMonth-1,1);
		GregorianCalendar cal = new GregorianCalendar(); //创建一个Calendar实例 
		cal.setTime(dt); 
		int day = 0; //存放某个月份的天数 
		int oderMonthDay = 0;//存放getMonth前一个月的天数
		if (getMonth==1)
		{
			getOderMonth=12;
		}
		else
		{
			getOderMonth=getMonth-1;
		}
		int day_week = 0; //用来存放某个月的第一天是星期几的数值 
		if (getMonth == 1 || getMonth == 3 || getMonth == 5 ||getMonth == 7 
				|| getMonth == 8 || getMonth == 10 || getMonth == 12)
		{ 
			day = 31; 
		}
		else if ( getMonth == 4	|| getMonth == 6 || getMonth == 9 || getMonth == 11) 
		{ 
			day = 30; 
		} 
		else
		{ 
			if (cal.isLeapYear(getYear))
			{ 
				day = 29; 
			} 
			else 
			{ 
				day = 28; 
			} 
		} 
		
		if (getOderMonth == 1 || getOderMonth == 3 || getOderMonth == 5 ||getOderMonth == 7 
				|| getOderMonth == 8 || getOderMonth == 10 || getOderMonth == 12)
		{ 
			oderMonthDay = 31; 
		}
		else if ( getOderMonth == 4	|| getOderMonth == 6 || getOderMonth == 9 || getOderMonth == 11) 
		{ 
			oderMonthDay = 30; 
		} 
		else
		{ 
			if (cal.isLeapYear(getYear))
			{ 
				oderMonthDay = 29; 
			} 
			else 
			{ 
				oderMonthDay = 28; 
			} 
		} 
		
		day_week =dt.getDay(); 
		if (day_week==0)day_week=day_week+7;
		/*绘制按钮 
		* 在这里我们首先要根据选定的月份的第一天是星期几来确定我们绘制按钮的起始位置 
		* 其中day_week就是我们要绘制的起始位置 
		* 对于那些没有数值可以显示的按钮要置空 
		*/ 
		if (day_week > 0) 
		{ 
			for (int i = 0,j=oderMonthDay-day_week+1; i < day_week; i++,j++) //第一天前面的按钮置空 
			{ 
				button[i].setText(j+""); 
				button[i].setEnabled(false);
			} 
		} 
		for (int i = day_week,count=1; i < day_week + day; count++, i++) //打印日历
		{ 
			if (this.nowYear==getYear && this.nowMonth==(getMonth-1) && this.toDay==count)
			{
				button[i].setForeground(Color.blue); 
			}
			else
			{
				button[i].setForeground(Color.black); 
			}
			button[i].setText(count + ""); 
			button[i].setEnabled(true);
		} 	
		for (int i = day_week + day ,j=1; i < 42; i++ ,j++) //最后一天后面的按钮置空 
			{ 
				button[i].setText(j+""); 
				button[i].setEnabled(false);
			} 
		
	}
	
	private void yearSpinnerStateChanged(ChangeEvent evt) {
		this.setDay();
	}
	
	private void monthComboBoxActionPerformed(ActionEvent evt) {
		this.setDay();
	}

	private void jButtonActionPerformed(ActionEvent evt) 
	{
		if (((JButton)evt.getSource()).getText().equals("\u4eca\u5929\u662f:"+this.nowYear+"-"+(this.nowMonth+1)+"-"+this.toDay))
		{
			String temp_Day = ""+this.toDay;
			String temp_Mon = ""+(this.nowMonth+1);
			if (this.nowMonth<9)//月份小于10, 则输出前面加0
			{
				temp_Mon="0"+(this.nowMonth+1);
			}
			if (this.toDay<10)//日数小于10,则输出前面加0
			{
				temp_Day="0"+this.toDay;
			}
			this.date=this.nowYear+"-"+temp_Mon+"-"+temp_Day;
		}
		else
		{
			String temp_Day = ""+((JButton)evt.getSource()).getText();
			String temp_Mon = ""+this.monthComboBox.getSelectedItem();
			if (Integer.parseInt(this.monthComboBox.getSelectedItem().toString())<10)//月份小于10, 则输出前面加0
			{
				temp_Mon="0"+this.monthComboBox.getSelectedItem();
			}
			if (Integer.parseInt(((JButton)evt.getSource()).getText())<10)//日数小于10,则输出前面加0
			{
				temp_Day="0"+((JButton)evt.getSource()).getText();
			}
			this.date=this.yearSpinner.getValue()+"-"+temp_Mon+"-"+temp_Day;
		}
		dispose();
	}
	
	public String toString ()
	{
		return this.date;
	}
}

⌨️ 快捷键说明

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