1、java课程设计报告简单日历程序课程设计题目2. 题目说明通过编写一个基于JAVA的应用系统综合实例,自定义一个日历组件显示日期和时间并进行适当的功能扩充,实践Java语言编程技术。3. 系统设计2.1 设计目标一个完整的程序应具有以下功能:1)显示当月日历、当前日期、当前时间;2)可查寻任意月以及任意年的日历;3)使用图形化界面能够弹出对话框;5)正常退出程序。2.2 设计思想设计一个类用来构成日历系统的主窗口,然后编写一个框架类显示时间和提示信息。在设计中应用了多种容器和控件。2.3 系统模块划分public static class CalendarTable(日历类)public st
2、atic void main(String args)public static class RollbyJFrame(滚动时间类)public class MyCalendar extends JApplet(主日历框架类)图1:简易日历的程序结构图2.3.1初始化:public void init()完成界面初始化,形成一个以挂历形式显示当前日期的窗口。2.3.2日历描述: (1)public void updateView()改变日期后完成更新界面; (2)抽象类java.util.Calendar获取系统日期并传递日期数据而且在人工改变日期后得出当天是周几; (3)public sta
3、tic void main(String args)主函数完成系统各算法的调用并对主窗口的一些属性进行设置;2.3.3滚动时间: 将时间以文本的形式在文本框中滚动播出,并能改变滚动的速度。4. 使用类及接口仅仅简单说明类的功能,详细资料请参看JavaTM2PlatformStandardEd. 6的电子文档,常规的接口与包则省略不屑。/以下是日历程序块中使用的类package fancy; import java.awt.*; import java.awt.event.*; import java.util.*; /主要用此包中的日期和时间类import javax.swing.*; imp
4、ort javax.swing.event.*; import javax.swing.table.*;/以下是对滚动时间程序块所使用的类和接口,用到定时器类Timerimport java.awt.Color;import java.awt.FlowLayout;import java.awt.event.ActionListener;import java.awt.event.ActionEvent;import java.awt.event.FocusListener;、/用于监听鼠标单击(焦点改变)事件import java.awt.event.FocusEvent;/用于响应鼠标单击
5、(焦点改变)事件、import javax.swing.JFrame;import javax.swing.JTextField;import javax.swing.JSpinner;/让用户从一个有序序列中选择一个数字或者一个对象值的单行输入字段。import javax.swing.JPanel;import javax.swing.Timer;/在指定时间间隔触发一个或多个 ActionEvent, 创建一个 Timer 对象,在该对象上注册一个或多个动作侦听器,以及使用 start 方法启动该计时器。并配合事件监听器支持时间的滚动播放。import javax.swing.event
6、.ChangeListener;import javax.swing.event.ChangeEvent;5. 运行结果与分析图2:初始界面显示日历。图3:点击查看时间按钮, 弹出时间消息对话框。图4:滚动显示当前时间。6. 程序源代码/* * (#) MyCalendar.java * author fancy */日历使用的类 import java.awt.BorderLayout;import java.awt.Color;import java.awt.ponent;import java.awt.Container;import java.awt.FlowLayout;import
7、 java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.FocusEvent;import java.awt.event.FocusListener;import java.util.Calendar;import javax.swing.JApplet;import javax.swing.utton;import javax.swing.oBox;import javax.swing.JFrame;import javax.swing.JLabel;import javax
8、.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JSpinner;import javax.swing.JTable;import javax.swing.JTextField;import javax.swing.ListSelectionModel;import javax.swing.Timer;import javax.swing.event.ChangeEvent;import javax.swing.event.ChangeListener;import javax.swing.table.Abstra
9、ctTableModel;import javax.swing.table.TableCellRenderer;import javax.swing.table.TableModel;/日历 public class MyCalendar extends JApplet public static final String WEEK_SUN = SUN; public static final String WEEK_MON = MON; public static final String WEEK_TUE = TUE; public static final String WEEK_WED
10、 = WED; public static final String WEEK_THU = THU; public static final String WEEK_FRI = FRI; public static final String WEEK_SAT = SAT; public static final Color background = Color.yellow; public static final Color foreground = Color.black; public static final Color headerBackground = Color.blue; p
11、ublic static final Color headerForeground = Color.white; public static final Color selectedBackground = Color.blue; public static final Color selectedForeground = Color.white; private JPanel cPane; private JLabel yearsLabel; private JSpinner yearsSpinner; private JLabel monthsLabel; private oBox mon
12、thsboBox; private JTable daysTable; private AbstractTableModel daysModel; private Calendar calendar; private utton button1; public MyCalendar() cPane = (JPanel) getContentPane(); public void init() cPane.setLayout(new BorderLayout(); calendar = Calendar.getInstance(); calendar = Calendar.getInstance
13、(); utton button1=new utton( 单击此处 查看时间 ); button1.setBounds(10,10,10,10); cPane.add(button1,BorderLayout.SOUTH); button1.addActionListener( new java.awt.event.ActionListener() public void actionPerformed(ActionEvent e) RollbyJFrame myrili=new RollbyJFrame(); JOptionPane.showMessageDialog(null, 您点击了+
14、e.getActionmand().toString()+按钮); ); yearsLabel = new JLabel(Year: ); yearsSpinner = new JSpinner(); yearsSpinner.setEditor(new JSpinner.NumberEditor(yearsSpinner, 0000); yearsSpinner.setValue(new Integer(calendar.get(Calendar.YEAR); yearsSpinner.addChangeListener(new ChangeListener() public void st
15、ateChanged(ChangeEvent changeEvent) int day = calendar.get(Calendar.DAY_OF_MONTH); calendar.set(Calendar.DAY_OF_MONTH, 1); calendar.set(Calendar.YEAR, (Integer) yearsSpinner.getValue().intValue(); int maxDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH); calendar.set(Calendar.DAY_OF_MONTH, day
16、maxDay ? maxDay : day); updateView(); ); JPanel yearMonthPanel = new JPanel(); cPane.add(yearMonthPanel, BorderLayout.NORTH); yearMonthPanel.setLayout(new BorderLayout(); yearMonthPanel.add(new JPanel(), BorderLayout.CENTER); JPanel yearPanel = new JPanel(); yearMonthPanel.add(yearPanel, BorderLayou
17、t.WEST); yearPanel.setLayout(new BorderLayout(); yearPanel.add(yearsLabel, BorderLayout.WEST); yearPanel.add(yearsSpinner, BorderLayout.CENTER); monthsLabel = new JLabel(Month: ); monthsboBox = new oBox(); for (int i = 1; i maxDay ? maxDay : day); updateView(); ); JPanel monthPanel = new JPanel(); y
18、earMonthPanel.add(monthPanel, BorderLayout.EAST); monthPanel.setLayout(new BorderLayout(); monthPanel.add(monthsLabel, BorderLayout.WEST); monthPanel.add(monthsboBox, BorderLayout.CENTER); daysModel = new AbstractTableModel() public int getRowCount() return 9; public int getColumnCount() return 7; p
19、ublic Object getValueAt(int row, int column) if (row = 0) return getHeader(column); row-; Calendar calendar = (Calendar) MyCalendar.this.calendar.clone(); calendar.set(Calendar.DAY_OF_MONTH, 1); int dayCount = calendar.getActualMaximum(Calendar.DAY_OF_MONTH); int moreDayCount = calendar.get(Calendar
20、.DAY_OF_WEEK) - 1; int index = row * 7 + column; int dayIndex = index - moreDayCount + 1; if (index dayCount) return null; else return new Integer(dayIndex); ; daysTable = new CalendarTable(daysModel, calendar); daysTable.setCellSelectionEnabled(true); daysTable.setSelectionMode(ListSelectionModel.S
21、INGLE_SELECTION); daysTable.setDefaultRenderer(daysTable.getColumnClass(0), new TableCellRenderer() public ponent getTableCellRendererponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) String text = (value = null) ? : value.toString(); JLabel cell = new JLa
22、bel(text); cell.setOpaque(true); if (row = 0) cell.setForeground(headerForeground); cell.setBackground(headerBackground); else if (isSelected) cell.setForeground(selectedForeground); cell.setBackground(selectedBackground); else cell.setForeground(foreground); cell.setBackground(background); return c
23、ell; ); updateView(); cPane.add(daysTable, BorderLayout.CENTER); public static String getHeader(int index) switch (index) case 0: return WEEK_SUN; case 1: return WEEK_MON; case 2: return WEEK_TUE; case 3: return WEEK_WED; case 4: return WEEK_THU; case 5: return WEEK_FRI; case 6: return WEEK_SAT; def
24、ault: return null; public void updateView() daysModel.fireTableDataChanged(); daysTable.setRowSelectionInterval(calendar.get(Calendar.WEEK_OF_MONTH), calendar.get(Calendar.WEEK_OF_MONTH); daysTable.setColumnSelectionInterval(calendar.get(Calendar.DAY_OF_WEEK) - 1, calendar.get(Calendar.DAY_OF_WEEK)
25、- 1); public static class CalendarTable extends JTable private Calendar calendar; public CalendarTable(TableModel model, Calendar calendar) super(model); this.calendar = calendar; public void changeSelection(int row, int column, boolean toggle, boolean extend) super.changeSelection(row, column, togg
26、le, extend); if (row = 0) return; Object obj = getValueAt(row, column); if (obj != null) calendar.set(Calendar.DAY_OF_MONTH, (Integer)obj).intValue(); public static void main(String args) JFrame frame = new JFrame(简易时间日历); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); MyCalendar myCalendar =
27、 new MyCalendar(); myCalendar.init(); frame.getContentPane().add(myCalendar); frame.setLocation(330,80); frame.setSize(360, 212); frame.setVisible(true); /滚动字public static class RollbyJFrame extends JFrame implements ActionListener, FocusListener, javax.swing.event.ChangeListener private JTextField
28、text; private JSpinner spinner; private Timer timer; private utton button; public RollbyJFrame() super(滚动时间); this.setSize(360,100); this.setBackground(java.awt.Color.lightGray); this.setLocation(700,120); Container c=getContentPane(); utton button=new utton(修改速度); this.add(button,East); button.addActionListener(this); Calendar now = Calendar.getInstance(); int hour=now.get(Calendar.HOUR); int minute=now.get(
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1