Java基础之GUI--日历

在学习了张老师的(GUI)视频后,自己跟个书上的例子做了个日历


分析:

1、通过继承JFrame类,把图形日历实现一个窗口;

2、通过实现ActionListener接口是的窗口本身能处理用户单击窗口中按钮的事件;

3、使用下拉列表框JComboBox存放年份和月份;

4、通过JButton实现"查看"和“今天”按钮,addListener方法为按钮注册时间处理器是,当按钮被单击时,由事件处理器响应;

5、用一个面板JPanel显示日期信息,面板中放多个按钮,每个按钮代表一个日期;

6、日期面板中的界面布局使用GridLayout(网格布局管理器),把面板分成多个规则的网格,将日期按钮顺序放在网格上;

7、窗口的界面布局使用BorderLayout(边界布局管理器),将窗口分成南、北、东、西和中5个方向,将组件放在窗口上;

8、使用Java.util.GregorianCalendar类计算日期、星期和闰月。


代码:

(代码有点多,不好意思漏了好多注释,大家包涵啦)

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
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;


public class CalendarTrain extends JFrame implements ActionListener{
	//月份和年份下拉 列表框
	private JComboBox MonthBox = new JComboBox();
	private JComboBox YearBox = new JComboBox();
	
	//年份月份标签
	private JLabel YearLabel = new JLabel("年份:");
	private JLabel MonthLabel = new JLabel("月份:");
	
	//确定和今天按钮
	private JButton button_ok = new JButton("查看");
	private JButton button_today = new JButton("今天");
	
	
	//获取今天的日期、年份和月份
	private Date now_date = new Date();
	
	private int now_year = now_date.getYear() + 1900;
	private int now_month = now_date.getMonth();
	private boolean todayFlag = false;
	
	//用一组按钮显示日期,一共7行7列。第一行是星期
	private JButton[] button_day = new JButton[42];
	private final String[] week = {"SUN","MON","TUE","WEN","THR","FRI","SAT"};
	private JButton[] button_week = new JButton[7];
	private String year_int = null;
	private int month_int;
	
	/*构造函数*/
	public CalendarTrain(){
		super();
		this.setTitle("日历");
		this.init();
		this.setLocation(500, 300);

		this.setResizable(false);
		pack();
		
	}
	
	//初始化日历
	private void init() {
		Font font = new Font("Dialog",Font.BOLD,16);
		YearLabel.setFont(font);
		MonthLabel.setFont(font);
		button_ok.setFont(font);
		button_today.setFont(font);
		//过去20年--未来20年
		for(int i = now_year - 20;i <= now_year + 100;i++){
			YearBox.addItem(i+"");
		}
		YearBox.setSelectedIndex(20);
		
		for(int i = 1;i <= 13;i++){
			MonthBox.addItem(i+"");
		}
		MonthBox.setSelectedIndex(now_month);
		
		//放置下拉列表框和控制按钮的面板
		JPanel panel_ym = new JPanel();
		panel_ym.add(YearLabel);
		panel_ym.add(YearBox);
		panel_ym.add(MonthLabel);
		panel_ym.add(MonthBox);
		panel_ym.add(button_ok);
		panel_ym.add(button_today);
		
		//为两个按钮添加时间监听器
		button_ok.addActionListener(this);
		button_today.addActionListener(this);
		
		
		JPanel panel_day = new JPanel();
		//7*7
		panel_day.setLayout(new GridLayout(7, 7, 3, 3));
		for(int i = 0; i < 7; i++) {
			button_week[i] = new JButton(" ");
			button_week[i].setText(week[i]);
			button_week[i].setForeground(Color.black);
			panel_day.add(button_week[i]);
		}
		button_week[0].setForeground(Color.red);
		button_week[6].setForeground(Color.red);
		
		for(int i = 0; i < 42;i++){
			button_day[i] = new JButton(" ");
			panel_day.add(button_day[i]);
		}
		
		this.paintDay();//显示当前日期
		
		JPanel panel_main = new JPanel();
		panel_main.setLayout(new BorderLayout());
		panel_main.add(panel_day,BorderLayout.SOUTH);
		panel_main.add(panel_ym,BorderLayout.NORTH);
		getContentPane().add(panel_main);
			
	}

	private void paintDay() {
		if(todayFlag){
			year_int = now_year +"";
			month_int = now_month;
		}else{
			year_int = YearBox.getSelectedItem().toString();
			month_int = MonthBox.getSelectedIndex();		
		}
		int year_sel = Integer.parseInt(year_int) - 1900;
		Date firstDay = new Date(year_sel, month_int, 1);
		GregorianCalendar cal = new GregorianCalendar();
		cal.setTime(firstDay);
		int days = 0;
		int day_week = 0;
		
		if(month_int == 0||month_int == 2||month_int == 4||month_int == 6
				||month_int == 7||month_int == 9||month_int == 11){
			days = 31;
		}else if(month_int == 3||month_int == 5||month_int == 8||month_int == 10){
			days = 30;
		}else{
			if(cal.isLeapYear(year_sel)){
				days = 29;
			}else{
				days = 28;
			}
		}
		
		day_week = firstDay.getDay();
		int count = 1;
		
		for(int i = day_week;i<day_week+days;count++,i++){
			if(i%7 == 0||(i+1)%7 == 0){
				if((i == day_week+now_date.getDate()-1)&& month_int==now_month && (year_sel == now_year-1900)){
					button_day[i].setForeground(Color.BLUE);
					button_day[i].setText(count+"");
				}else{
					button_day[i].setForeground(Color.RED);
					button_day[i].setText(count+"");
				}
			}else{
				if((i == day_week+now_date.getDate()-1)&& month_int==now_month && (year_sel == now_year-1900)){
					button_day[i].setForeground(Color.BLUE);
					button_day[i].setText(count+"");
				}else{
					button_day[i].setForeground(Color.BLACK);
					button_day[i].setText(count+"");
				}
			}
			
		}
		if(day_week == 0){
			for(int i = days;i<42;i++){
				button_day[i].setText("");
			}
		}else{
			for(int i = 0;i<day_week;i++){
				button_day[i].setText("");
			}
			for(int i=day_week+days;i<42;i++){
				button_day[i].setText("");
			}
		}
		
		
	}

	

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==button_ok){
			todayFlag=false;
			this.paintDay();
		}else if(e.getSource()==button_today){
			todayFlag=true;
			YearBox.setSelectedIndex(20);
			MonthBox.setSelectedIndex(now_month);
			this.paintDay();
		}
		
	}
	
	public static void main(String[] args) {
		CalendarTrain ct = new CalendarTrain();
		ct.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		ct.setVisible(true);

	}

}


欢饮一起交流学习......

  • 22
    点赞
  • 112
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
好的,以下是一个使用Java编写的GUI日历: ```java import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.text.SimpleDateFormat; import java.util.Calendar; public class CalendarGUI extends JFrame { private JLabel label; private JButton prevButton; private JButton nextButton; private JPanel monthPanel; private JPanel dayPanel; private Calendar calendar; public CalendarGUI() { // 设置窗口标题 setTitle("Calendar"); // 创建日历对象 calendar = Calendar.getInstance(); // 初始化界面 initUI(); // 显示窗口 pack(); setLocationRelativeTo(null); setVisible(true); } private void initUI() { // 设置布局管理器 setLayout(new BorderLayout()); // 创建月份选择面板 monthPanel = new JPanel(); monthPanel.setLayout(new FlowLayout()); // 添加上个月按钮 prevButton = new JButton("<"); prevButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { calendar.add(Calendar.MONTH, -1); updateUI(); } }); monthPanel.add(prevButton); // 添加月份标签 label = new JLabel(); label.setHorizontalAlignment(JLabel.CENTER); monthPanel.add(label); // 添加下个月按钮 nextButton = new JButton(">"); nextButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { calendar.add(Calendar.MONTH, 1); updateUI(); } }); monthPanel.add(nextButton); // 添加月份选择面板 add(monthPanel, BorderLayout.NORTH); // 创建日期面板 dayPanel = new JPanel(); dayPanel.setLayout(new GridLayout(0, 7)); // 添加星期标签 String[] weekdays = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}; for (String weekday : weekdays) { JLabel label = new JLabel(weekday, JLabel.CENTER); dayPanel.add(label); } // 添加日期标签 updateUI(); add(dayPanel, BorderLayout.CENTER); } private void updateUI() { // 清空日期面板 dayPanel.removeAll(); // 更新月份标签 SimpleDateFormat format = new SimpleDateFormat("MMMM yyyy"); label.setText(format.format(calendar.getTime())); // 添加日期标签 calendar.set(Calendar.DAY_OF_MONTH, 1); int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1; int daysInMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH); for (int i = 0; i < dayOfWeek; i++) { dayPanel.add(new JLabel("")); } for (int i = 1; i <= daysInMonth; i++) { JLabel label = new JLabel(Integer.toString(i), JLabel.CENTER); dayPanel.add(label); } // 重新绘制界面 revalidate(); repaint(); } public static void main(String[] args) { // 创建日历GUI对象 new CalendarGUI(); } } ``` 这个日历GUI界面由两个面板组成,一个是月份选择面板,另一个是日期显示面板。月份选择面板包括上个月按钮、月份标签和下个月按钮。日期显示面板则根据当前月份显示日期标签。 当用户点击上个月或下个月按钮时,程序会根据当前日期计算出上个月或下个月的日期,并重新绘制日期面板。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值