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

📄 mainframe.java

📁 用Java Swing写的一个日历程序
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/**
 * MainFrame.java
 * Summary 万年历主类
 * Created on 2008年5月27日, 下午3:40
 * @author supergg
 * remark 如有改动请发一份代码给我,邮箱ecgql@qq.com
 */
package ecgql;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.Date;
import java.util.Calendar;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.*;


public class MainFrame 
{ 
	private static final long serialVersionUID = 1L; 
	JFrame date_mf=new JFrame("Calendar");
	JPanel panel = new JPanel(new BorderLayout()); 
	JPanel panel1 = new JPanel(); 
	JPanel panel2 = new JPanel(new GridLayout(7, 7)); 
	JPanel panel3 = new JPanel(); 
	JPanel leftpanel = new JPanel(new GridLayout(7, 1)); 
	JPanel rightpanel = new JPanel(new GridLayout(7, 1)); 
	JPanel textbutton = new JPanel(new GridLayout(2, 1));
	JButton  []datebutton = new JButton[49]; 
	JLabel y_label = new JLabel("Year"); 
	JLabel m_label = new JLabel("  Month"); 
	JComboBox com1 = new JComboBox(); 
	JComboBox com2 = new JComboBox(); 
	JButton getdate =new JButton("   OK   ");
	JButton pre_month =new JButton("<<");
	JButton next_month =new JButton(">>");
	JButton pre=new JButton("∧");
	JButton next=new JButton("∨");
	JButton cancel =new JButton("Cancel");	
	
	JTextField text = new JTextField();
	int re_year, re_month; 
	int x_size, y_size; 
	String year_num; 
	
	String sel_year=null;
	String sel_month=null;
	String sel_day="1";     //所用的全局数据
	
	Calendar now = Calendar.getInstance(); // 实例化Calendar 
	
	MainFrame()      //绘制gui界面
	{ 
		date_mf.setSize(350,300); 		
		x_size = (int) (Toolkit.getDefaultToolkit().getScreenSize().getWidth()); 
		y_size = (int) (Toolkit.getDefaultToolkit().getScreenSize().getHeight()); 
		date_mf.setLocation((x_size - 500) / 2, (y_size - 350) / 2); 
		date_mf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
		
		textbutton.add(pre);
		textbutton.add(next);
		
		//pre.setActionCommand("pre");
		//ImageIcon image=new ImageIcon("directory");
		//pre.setIcon(image);
		
		pre.setBorder(BorderFactory.createEmptyBorder());
		pre.setBackground(Color.WHITE);
		next.setBorder(BorderFactory.createEmptyBorder());
		next.setBackground(Color.WHITE);
		pre.addActionListener(new ButtonAction());
		next.addActionListener(new ButtonAction());
		
		com1.setFont(new Font("宋体",12,15));
		com2.setFont(new Font("宋体",12,15));
		panel1.add(y_label);
		panel1.add(com1);
		panel1.add(m_label);
		panel1.add(com2);
		panel1.add(new JLabel("    "));
		panel1.add(text);
		text.setEditable(false);
		text.setFont(new Font("宋体",12,15));
		text.setBackground(Color.WHITE);
		panel1.add(new JLabel(" "));
		panel1.add(textbutton);
		

		leftpanel.add(pre_month);
		leftpanel.setBackground(Color.WHITE);
		rightpanel.add(next_month);
		rightpanel.setBackground(Color.WHITE);
		
		for (int i = 0; i < 49; i++) 
		{ 
			datebutton[i] = new JButton("");// 将显示的字符设置为居中 
			datebutton[i].addActionListener(new selectAction());
			datebutton[i].addMouseListener(new mouseclick());
			datebutton[i].setBackground(Color.WHITE); 
			datebutton[i].setBorder(BorderFactory.createEmptyBorder());
			panel2.add(datebutton[i]); 
		}
		pre_month.setBackground(Color.WHITE); 
		next_month.setBackground(Color.WHITE); 
		pre_month.setBorder(BorderFactory.createEmptyBorder());
		next_month.setBorder(BorderFactory.createEmptyBorder());
		pre_month.addActionListener(new ButtonAction()); 
		next_month.addActionListener(new ButtonAction()); 
		
		JPanel panel22=new JPanel(new FlowLayout());
		
		panel3.add(new TimeClock(this));
		panel3.add(new JLabel("     "));
		panel3.add(getdate);
		panel3.add(new JLabel("   "));
		panel3.add(cancel); 
		panel.add(panel1, BorderLayout.NORTH); 
		panel.add(panel22, BorderLayout.CENTER); 
		panel.add(panel3, BorderLayout.SOUTH); 
		panel.setBackground(Color.WHITE); 
		panel1.setBackground(Color.WHITE); 
		panel1.setBorder(BorderFactory.createTitledBorder(""));
		panel2.setBackground(Color.WHITE); 
		panel3.setBackground(Color.WHITE); 
				
		panel22.add(leftpanel);
		panel22.add(panel2);
		panel22.add(rightpanel);
		panel22.setBackground(Color.WHITE); 
		panel22.setBorder(BorderFactory.createTitledBorder(""));
		
		init(); 
		
		com1.addActionListener(new ClockAction()); 
		com2.addActionListener(new ClockAction()); 
		getdate.addActionListener(new ButtonAction()); 
		cancel.addActionListener(new ButtonAction()); 

		date_mf.setContentPane(panel); 
		date_mf.setVisible(true); 
		date_mf.setResizable(false); 
	} 
	
	
	
	public void init()     //初始化系统控件数据
	{ 
		int year, month_num, day_num,first_day_num; 
		String log[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }; 
		
		for (int i = 0; i < 7; i++) //初始化星期的Button并修改颜色
		{ 
			datebutton[i].setText(log[i]); 
			datebutton[i].setBackground(Color.GREEN);
			datebutton[i].setEnabled(false);
		} 
		
		for (int i = 1; i < 10000; 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)); // 得到当前时间的年份 
		day_num = (int) (now.get(Calendar.DAY_OF_MONTH));
		com1.setSelectedIndex(year - 1); // 设置下拉列表显示为当前年 
		com2.setSelectedIndex(month_num); // 设置下拉列表显示为当前月 
		
		String c_day=String.valueOf(day_num);
		String c_year=String.valueOf(year);
		String c_month=String.valueOf(month_num+1);
		String c_date=c_month+"-"+c_day+"-"+c_year;
		text.setText(c_date);//设定Text日期
		
		first_day_num = use(year, month_num); 
		reSetDay(first_day_num, year, month_num,c_day); 
	} 

	public int use(int reyear, int remonth)   //获得周次 remonth范围为0-11
	{ 
		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,String day_log) //绘制日历 month_log范围为0-11
	{ 
		int month_day_score; // 存储月份的天数 
		int count; 
		month_day_score = 0; 
		count = 1; 
		sel_day=day_log;

		Date date = new Date(year_log, month_log + 1, 1); // 现在的时间
		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++) 
		{  // 初始化标签 
			datebutton[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++) 
		{ 					
			datebutton[i].setText(count + "");//初始化Button的数值
			
			if(datebutton[i].getText().equals(sel_day))//初始化并修改选中的数值的颜色
			  {
				  datebutton[i].setForeground(Color.red);				 
			  }else{
				datebutton[i].setForeground(Color.black);
			  }
		} 
	} 
	public void showDate(String date)   //显示选择的日期
	{
		JFrame dmf=new JFrame("选择的日期");
		dmf.setSize(200,100);
		
		int x_size = (int) (Toolkit.getDefaultToolkit().getScreenSize().getWidth()); 
		int y_size = (int) (Toolkit.getDefaultToolkit().getScreenSize().getHeight()); 
		dmf.setLocation((x_size - 500) / 2, (y_size - 350) / 2); 
		
		dmf.add(new JLabel("当前选择的日期是:"+date));
		dmf.setVisible(true);
		dmf.setResizable(false);
	}
	
	public String getSelectData()      //获得并设定选择的日期
	{
		String date=null;
		
		sel_year=com1.getSelectedItem().toString();
		sel_month=com2.getSelectedItem().toString();
		
		date=sel_month+"-"+sel_day+"-"+sel_year;
		text.setText(date);
		if(!date.equals(""))
		{		
			System.out.println("选择的日期是:"+date);
			return date;
		}
		else
		{
		    return null;
		}
	}
	public void setTextdata()      //设定选择的日期
	{
		String date=null;
		
		sel_year=com1.getSelectedItem().toString();
		sel_month=com2.getSelectedItem().toString();
		
		date=sel_month+"-"+sel_day+"-"+sel_year;
		text.setText(date);		
	}
	
	class ClockAction implements ActionListener     //下拉列表事件监听器
	{ 
		public void actionPerformed(ActionEvent arg0) 
		{ 
			System.out.println(arg0.getActionCommand());
			
			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 
			
			sel_year=com1.getSelectedItem().toString();
			sel_month=com2.getSelectedItem().toString();   //保存选择的日期
			
			
			c_week = use(c_year, c_month); // 调用函数use,得到星期几 
			reSetDay(c_week, c_year, c_month,sel_day ); // 调用函数Resetday 
			setTextdata();//修改Text显示的时间
		} 
	} 

	class ButtonAction implements ActionListener //按钮事件监听器
	{  
		int str=0;
		int end=1;    //保存日期文本框选择的起始点和终结点
		
		public void actionPerformed(ActionEvent e) 
		{ 

⌨️ 快捷键说明

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