1、XXXJava语言课程期末作业 2015年12月18日一、课程题目 设计一个万年历系统,实现以下功能(1)可以查看当前的年份、月份和日期;(2)有时间栏,便于用户查看当前时间;(3)能够查找1至9999年的日期与星期的关系。 其它功能可自行开发。二、题目分析与设计 1.使用的开发环境为 eclipseJdk环境: jdk1.82. (此为万年历界面图)使用了 jTabbedPane 类 对功能页面分页,布局策略基本用BorderLayout 和GridLayout 。3. 本项共有5个类,基本一个功能有一个类实现,主类 CalendarMain 只包含一个main函数:对创建框架和其属性设置。
2、AlarmDemo类 则实现闹钟功能;NoteBook 类实现记事本功能;CalendarsView 类 则实现基本日历功能,ChooseDay 类 是一个弹窗,被CalendarsView 调用,CalendarsView 、AlarmDemo 也在该类被调用;CalendarMain -运行 .new CalendarsView();CalendarsView总的框架界面设计布局,日历功能的实现,对其他功能的调用 new AlarmDemo(); new NoteBook();AlarmDemo继承Jpanel ,运用线程 和applet 实现闹钟功能,到规定时间播放闹铃NoteBook继
3、承Jpanel ,运用io ,可以保存文本newChooseDay(null, year, month).setVisible(true);ChooseDay继承 JDialog ,让用户选择看那一年月的日历4. 日历界面使用了 jTable ,功能的实现逻辑则是1.首先定义两个全局变量-年和月,查看不同时间的日历,该变量也随之改变。2. 写了几个函数获取今年第一天是星期几,然后根据月份天数即可推算具体某月第一天是星期几。3. 知道一个月第一天是星期几,则该月日历既可以顺序列出。闹钟的功能实现思路:1. 获取界面设定的时间。2. 使用线程,每过一秒急救获取当前时间,并判断是否到达设定的时间3.
4、 到设定的时间,启动弹窗,播放闹铃4. 闹铃科根据文件选择器自行选择记事本功能实现思路1. 在项目文件夹先新建一个txt文件作为储存的数据库2. 利用File 类 和io 数据流将界面文本储存进txt文件,实现保存功能3. 每当程序启动,读取txt文件内容并显示到界面上三、测试分析1.使用如下拓展包2. 程序界面截图如下日历主界面闹钟界面记事本界面3. 测试流程及功能介绍(1) 点击上一月、下一月可查看不同月份日历,左下角显示当前时间。点击蓝色的日期栏可以回到当前页(2)点击圈中按钮会出现弹框,可以调节年、月,查看指定年月日历(3)闹铃可以使用默认闹铃,也能自定义选择闹铃,不过闹铃格式只能为w
5、av或au时间到了以后出现弹框 ,并响起闹铃(4)记事本输入文本,按保存即可。(5)点击最小化按钮,可以选择普通缩小或者直接缩小到托盘.附录:源代码(注:在工程目录下新建文件夹 source ,并添加了闹铃文件,txt,托盘图片,如图)AlarmDemo.javaimport java.applet.Applet;import java.applet.AudioClip;import java.awt.BorderLayout;import java.awt.Dimension;import java.awt.Font;import java.awt.GridLayout;import jav
6、a.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.io.File;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;import javax.swing.*;import javax.swing.text.DefaultFormatterFactory;import javax.swing.text.NumberFormatter;public class AlarmDemo exten
7、ds JPanel implements Runnable private static final long serialVersionUID = -7066237267822356675L;Thread alarm;public AudioClip soumd1;Calendar c = Calendar.getInstance();JButton sure = new JButton(确定);JButton open = new JButton(可选择闹铃);JLabel jLabel = new JLabel(设置 时间 日-时-分, JLabel.CENTER);JLabel day
8、Label = new JLabel(日, JLabel.CENTER);JLabel hourLabel = new JLabel(时, JLabel.CENTER);JLabel minuteLabel = new JLabel(分, JLabel.CENTER);JPanel jPanel1 = new JPanel();JPanel jPanel2 = new JPanel();JPanel jPanel3 = new JPanel();JPanel jPanel4 = new JPanel();JTextField music = new JTextField(System.getP
9、roperty(user.dir)+sourcealarm.wav);int y = 0, m = 0, h = 0, mi = 0, d = 0;boolean fo = false;public AlarmDemo() music.setEditable(false);/ 设置标签格式dayLabel.setFont(new Font(Dialog, 1, 30);hourLabel.setFont(new Font(Dialog, 1, 30);minuteLabel.setFont(new Font(Dialog, 1, 30);jLabel.setFont(new Font(Dial
10、og, 1, 30);/按钮设置sure.setContentAreaFilled(false);sure.setFocusPainted(false);open.setContentAreaFilled(false);open.setFocusPainted(false);JSpinner spinner1 = new JSpinner();JSpinner spinner2 = new JSpinner();JSpinner spinner3 = new JSpinner();spinner3.setModel(new SpinnerNumberModel(c.get(Calendar.D
11、ATE), 1, 31, 1);spinner1.setModel(new SpinnerNumberModel(c.get(Calendar.HOUR_OF_DAY),0, 23, 1);spinner2.setModel(new SpinnerNumberModel(c.get(Calendar.MINUTE), 0, 59,1);/ 设置属性setSpinners(spinner1);setSpinners(spinner2);setSpinners(spinner3);jPanel4.add(jLabel);jPanel4.setPreferredSize(new Dimension(
12、500, 100);jPanel1.setLayout(new GridLayout(1, 6);jPanel1.add(spinner3);jPanel1.add(dayLabel);jPanel1.add(spinner1);jPanel1.add(hourLabel);jPanel1.add(spinner2);jPanel1.add(minuteLabel);jPanel1.setPreferredSize(new Dimension(500, 100);jPanel2.setLayout(new BorderLayout();jPanel2.add(jPanel4, BorderLa
13、yout.NORTH);jPanel2.add(music, BorderLayout.CENTER);jPanel2.add(open, BorderLayout.EAST);jPanel3.setLayout(new GridLayout(1, 1);jPanel3.add(sure);setLayout(new BorderLayout();add(jPanel1, BorderLayout.NORTH);add(jPanel2, BorderLayout.CENTER);add(jPanel3, BorderLayout.SOUTH);open.addActionListener(ne
14、w ActionListener() public void actionPerformed(ActionEvent event) JFileChooser fileChooser = new JFileChooser(); / 实例化文件选择器fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); / 设置文件选择模式,此处为文件和目录均可fileChooser.setCurrentDirectory(new File(.); / 设置文件选择器当前目录fileChooser.setFileFilter(ne
15、w javax.swing.filechooser.FileFilter() public boolean accept(File file) / 可接受的文件类型String name = file.getName().toLowerCase();return name.endsWith(.wav)| name.endsWith(.au)| file.isDirectory();public String getDescription() / 文件描述return 音乐文件(*.wav,*.au););if (fileChooser.showOpenDialog(AlarmDemo.this
16、) = JFileChooser.APPROVE_OPTION) / 弹出文件选择器,并判断是否点击了打开按钮String fileName = fileChooser.getSelectedFile().getAbsolutePath(); / 得到选择文件或目录的绝对路径music.setText(fileName););sure.addActionListener(new ActionListener() public void actionPerformed(ActionEvent event) if (sure.getText().equals(确定) try d = (int) s
17、pinner3.getValue();h = (int) spinner1.getValue();mi = (int) spinner2.getValue();if (1 = d & d = 31 & 0 = h & h = 23 & 0 = mi& mi = 59) fo = true;jLabel.setText(设置闹钟成功);spinner1.setEnabled(false);spinner2.setEnabled(false);spinner3.setEnabled(false);start(); elseJOptionPane.showMessageDialog(null, 输入
18、时间错误); catch (Exception e) JOptionPane.showMessageDialog(null, 请输入正确的时间); else spinner1.setEnabled(true);spinner2.setEnabled(true);spinner3.setEnabled(true);fo = false;stop();soumd1.stop();sure.setText(确定);jLabel.setText(设置 时间 日-时-分););public void start() if (alarm = null) alarm = new Thread(this);a
19、larm.start();public void stop()/ 停止线程alarm = null;public void run() while (true) Date now = new Date();if (fo) sure.setText(关闭);SimpleDateFormat ri = new SimpleDateFormat(dd); / 封装 为了获取日期SimpleDateFormat shi = new SimpleDateFormat(kk); / 封装 为了获取小时SimpleDateFormat fen = new SimpleDateFormat(mm); / 封装
20、 为了获取分int riqi = Integer.parseInt(ri.format(now); / 获取日期int shizhong = Integer.parseInt(shi.format(now); / 获取小时int fenzhong = Integer.parseInt(fen.format(now); / 获取分钟if (riqi = d & shizhong = h & fenzhong = mi) / 判断条件try soumd1 = Applet.newAudioClip(new File(music.getText().toURL(); / 播放音乐soumd1.loo
21、p();fo = false;JOptionPane.showMessageDialog(null, 时间到了! (v); catch (Exception e) e.printStackTrace();try Thread.sleep(1000); catch (InterruptedException ie) public void setSpinners(JSpinner spinner) / 设置属性JSpinner.NumberEditor editor = new JSpinner.NumberEditor(spinner, 0);spinner.setEditor(editor)
22、;JFormattedTextField textField = (JSpinner.NumberEditor) spinner.getEditor().getTextField();textField.setFont(new Font(Dialog, 1, 45);textField.setEditable(true);DefaultFormatterFactory factory = (DefaultFormatterFactory) textField.getFormatterFactory();NumberFormatter formatter = (NumberFormatter)
23、factory.getDefaultFormatter();formatter.setAllowsInvalid(false);CalendarsView.javaimport java.awt.AWTException;import java.awt.BorderLayout;import java.awt.Color;import java.awt.Dimension;import java.awt.Font;import java.awt.Frame;import java.awt.GridLayout;import java.awt.SystemTray;import java.awt
24、.TrayIcon;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import java.awt.event.MouseListener;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import java.io.File;import java.io.IOExceptio
25、n;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;import javax.imageio.ImageIO;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JScrollPane;import javax.swing.J
26、TabbedPane;import javax.swing.JTable;import javax.swing.Timer;import javax.swing.table.DefaultTableModel;public class CalendarsView extends JFrame private static final long serialVersionUID = 1L;static Calendar c = Calendar.getInstance();static int year = c.get(Calendar.YEAR);static int month = c.ge
27、t(Calendar.MONTH) + 1;int monthday = 0;String weekdaystr = 星期日, 星期一, 星期二, 星期三, 星期四, 星期五, 星期六 ;Object monthview = new String67;JPanel dayPanel = new JPanel();JPanel panel1 = new JPanel();JPanel calPanel = new JPanel();JPanel alarmDemo = new AlarmDemo();JPanel noteBook = new NoteBook();JTabbedPane jTa
28、bbedPane = new JTabbedPane(JTabbedPane.TOP);JButton Prebutton = new JButton(上一月);JButton Nextbutton = new JButton(下一月);JButton PreYear = new JButton(上一年);JButton NextYear = new JButton(下一年);JButton Change = new JButton(c.get(Calendar.YEAR) + 年+ (c.get(Calendar.MONTH) + 1) + 月);JLabel nowLabel = new
29、JLabel(c.get(Calendar.YEAR) + 年+ (c.get(Calendar.MONTH) + 1) + 月 + (c.get(Calendar.DATE) + 日,JLabel.CENTER);JLabel timeLabel = new JLabel(-, JLabel.LEFT);DefaultTableModel model = new DefaultTableModel();JTable JTable;JScrollPane JScrollPane;TrayIcon trayIcon;/托盘图标 SystemTray systemTray;/系统托盘public
30、CalendarsView() /托盘化systemTray = SystemTray.getSystemTray();/获得系统托盘的实例 try trayIcon = new TrayIcon(ImageIO.read(new File(System.getProperty(user.dir)+source360.png); catch (IOException e1) e1.printStackTrace(); this.addWindowListener( new WindowAdapter() public void windowIconified(WindowEvent e) in
31、t n = JOptionPane.showConfirmDialog(null, 是否缩小到托盘?, 缩小, JOptionPane.YES_NO_OPTION); if (n = JOptionPane.YES_OPTION) dispose();/窗口最小化时dispose该窗口 try systemTray.add(trayIcon); catch (AWTException e1) e1.printStackTrace();/设置托盘的图标 else if (n = JOptionPane.NO_OPTION) systemTray.remove(trayIcon); ); tray
32、Icon.addMouseListener( new MouseAdapter() public void mouseClicked(MouseEvent e) if(e.getClickCount() = 2)/双击托盘窗口再现 setExtendedState(Frame.NORMAL); setVisible(true); systemTray.remove(trayIcon); ); /时间栏Timer timer = new Timer(1000, new ActionListener() public void actionPerformed(ActionEvent evt) ti
33、meLabel.setText(new SimpleDateFormat(yyyy-MM-dd HH:mm:ss).format(new Date(););timer.start();nowLabel.setFont(new java.awt.Font(Dialog, 1, 15);nowLabel.setForeground(Color.blue);dataOfDay();dayPanel.setLayout(new BorderLayout();dayPanel.add(nowLabel, BorderLayout.NORTH);dayPanel.add(JScrollPane, Bord
34、erLayout.CENTER);/ 上方按钮panel1.setLayout(new GridLayout(1, 3);panel1.setBackground(Color.white);Prebutton.setContentAreaFilled(false);Prebutton.setFocusPainted(false);Change.setContentAreaFilled(false);Change.setFocusPainted(false);Nextbutton.setContentAreaFilled(false);Nextbutton.setFocusPainted(fal
35、se);panel1.add(Prebutton);panel1.add(Change);panel1.add(Nextbutton);calPanel.setLayout(new BorderLayout();calPanel.add(panel1, BorderLayout.NORTH);calPanel.add(dayPanel, BorderLayout.CENTER);calPanel.add(timeLabel, BorderLayout.SOUTH);jTabbedPane.add(calPanel, 日历);jTabbedPane.add(alarmDemo,闹钟); jTab
36、bedPane.add(noteBook,记事本); add(jTabbedPane, BorderLayout.NORTH);/ 按钮监听器Prebutton.addActionListener(new PressButton();Nextbutton.addActionListener(new PressButton();Change.addMouseListener(new MouseListener() public void mouseClicked(MouseEvent e) if (e.getButton() = MouseEvent.BUTTON1)new ChooseDay(
37、null, year, month).setVisible(true);dataOfDay();Change.setText(year + 年 + month + 月);public void mousePressed(MouseEvent e) public void mouseReleased(MouseEvent e) public void mouseEntered(MouseEvent e) public void mouseExited(MouseEvent e) );nowLabel.addMouseListener(new MouseListener() public void
38、 mouseClicked(MouseEvent e) if (e.getButton() = MouseEvent.BUTTON1)year = c.get(Calendar.YEAR);month = c.get(Calendar.MONTH) + 1;dataOfDay();Change.setText(year + 年 + month + 月);public void mousePressed(MouseEvent e) public void mouseReleased(MouseEvent e) public void mouseEntered(MouseEvent e) publ
39、ic void mouseExited(MouseEvent e) );public void dataOfDay() int n = 1;int k = getDays();for (int i = 0; i 6; i+) for (int j = 0; j 7; j+) if (i = 0 & j getMonthDay(month) monthviewij = ; else monthviewij = String.valueOf(n);n+;model.setRowCount(0);model.setDataVector(monthview, weekdaystr);JTable =
40、new JTable(model);JTable.setGridColor(Color.gray);JTable.setBackground(Color.white);JTable.setColumnSelectionAllowed(true);/ 将table中的列设置为可选择的JTable.setSelectionBackground(Color.pink);/ 当选定某一天时背景颜色为粉JTable.setSelectionForeground(Color.BLUE);JTable.setFont(new Font(黑体, Font.BOLD, 24);/ 日期数字字体格式JTable.setRowHeight(26);/ 表格的高度