欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Java 实训1:编写一个窗体程序显示日历表。

程序员文章站 2024-03-16 18:54:34
...
 实训!要求:
1.简单实现日历功能,能查看前月后月的日历功能。
2.使用JTable 组件显示日历。
参考结果:
Java 实训1:编写一个窗体程序显示日历表。
代码:
CalendaBean.java
import java.util.Calendar;

public class CalendaBean {
	Test test;
	String[] day;

	int year = 2017, month = 7;

	public void setYear(int year) {
		this.year = year;
	}

	public void setMonth(int month) {
		this.month = month;
	}

	public int Last() {
		month--;
		if (month == 0) {
			month = 12;
			year--;
		}
		return month;
	}

	public int Next() {
		month++;
		if (month == 13) {
			month = 1;
			year++;
		}
		return month;
	}

	public String[] getCalendar() {
		String[] a = new String[42];
		Calendar rili = Calendar.getInstance();
		rili.set(year, month - 1, 1);
		int weekDay = rili.get(Calendar.DAY_OF_WEEK) - 1;
		int day = 0;
		if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8
				|| month == 10 || month == 12) {
			day = 31;
		}
		if (month == 4 || month == 6 || month == 9 || month == 11) {
			day = 30;
		}
		if (month == 2) {
			if ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0))
				day = 29;
			else
				day = 28;
		}
		for (int i = 0; i < weekDay; i++)
			a[i] = " ";
		for (int i = weekDay, n = 1; i < weekDay + day; i++) {
			a[i] = String.valueOf(n);
			n++;
		}
		for (int i = weekDay + day; i < a.length; i++)
			a[i] = " ";
		return a;
	}
}
Change.java
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JLabel;

public class Change implements ActionListener {
	CalendaBean c;
	JLabel now;
	Test test;

	public void actionPerformed(ActionEvent e) {
		String str = e.getActionCommand();
		if (str.equals("lastmonth")) {
			c.Last();
		} else if (str.equals("nextmonth")) {
			c.Next();
		}
		test.Rili();
		now.setText("日历 :" + c.year + "年" + c.month + "月");
	}

}
Test.java

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;

public class Test extends JFrame {
	JButton bx, by;
	CalendaBean cb = new CalendaBean();
	Change change = new Change();
	DefaultTableModel model;
	String[] label;
	JLabel now;
	JTable table = new JTable();
	JScrollPane pane = new JScrollPane();
	Object[] columnNames = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
	Object[][] rowData = { { null, null, null, null, null, null, null },
			{ null, null, null, null, null, null, null },
			{ null, null, null, null, null, null, null },
			{ null, null, null, null, null, null, null },
			{ null, null, null, null, null, null, null },
			{ null, null, null, null, null, null, null }, };

	public static void main(String[] args) {
		Test frame = new Test();
		frame.setSize(500, 400);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setTitle("日历");
		frame.setVisible(true);

	}

	public Test() {
		change.test = this;
		int year, month;
		setLayout(new BorderLayout());
		JPanel pNorth = new JPanel();
		cb = new CalendaBean();
		change.c = cb;
		bx = new JButton("上月");
		by = new JButton("下月");
		bx.setActionCommand("lastmonth");
		by.setActionCommand("nextmonth");
		bx.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				change.actionPerformed(e);

			}
		});
		by.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				change.actionPerformed(e);
			}
		});
		pNorth.add(bx);
		pNorth.add(by);

		Rili();
		model = new DefaultTableModel(rowData, columnNames);

		table = new JTable(model);
		table.setRowHeight(38);
		table.getTableHeader().setResizingAllowed(false);

		pane = new JScrollPane(table);
		JPanel pSouth = new JPanel();
		now = new JLabel();
		now.setText("日历:" + cb.year + "年" + cb.month + "月");
		change.now = now;
		pSouth.add(now);
		add(pNorth, BorderLayout.NORTH);
		add(pane, BorderLayout.CENTER);
		add(pSouth, BorderLayout.SOUTH);
	}

	public void Rili() {

		String[] a = cb.getCalendar();
		int x = 0;
		if (model != null) {
			model.setRowCount(0);
		}
		for (int i = 0; i < 6; i++) {
			for (int j = 0; j < 7; j++) {
				rowData[i][j] = a[x];
				x++;
			}
			if (model != null)
				model.addRow(rowData[i]);
		}
	}
}

运行结果
Java 实训1:编写一个窗体程序显示日历表。Java 实训1:编写一个窗体程序显示日历表。Java 实训1:编写一个窗体程序显示日历表。

说明:这个程序与前几天上传的日历显示表差不多,只是改了中间的组件,原本以为是很简单的事,结果做的时候才发现其中的难点,初学Java还有很多不懂的地方,幸好有大神的解答,自己还有很多需要学习的地方。