一,項目介紹

1.可以查看年,月,日等功能。能获取今天的日期,并且能够通过下拉年,月的列表。

2.当程序运行时,显示的时间是系统当前时间。

3.可以手动输入时间,确定后系统跳转到制定的时间。

4.提供一种点击功能,通过点击实现年份,月份的自增和自减功能。

二,运行界面

三,代码详情

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*; public class Demo6 extends JApplet {
private static final long serialVersionUID = 1L;
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 = "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.white;
public static final Color foreground = Color.black;
public static final Color headerBackground = Color.blue;
public 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 JComboBox<Integer> monthsComboBox;
private JTable daysTable;
private AbstractTableModel daysModel;
private Calendar calendar; public Demo6() {
cPane = (JPanel) getContentPane();
} public void init() {
cPane.setLayout(new BorderLayout()); calendar = Calendar.getInstance(); 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() { @Override
public void stateChanged(ChangeEvent e) {
// TODO Auto-generated method stub
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 > 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, BorderLayout.WEST);
yearPanel.setLayout(new BorderLayout());
yearPanel.add(yearsLabel, BorderLayout.WEST);
yearPanel.add(yearsSpinner, BorderLayout.CENTER); monthsLabel = new JLabel("Month: ");
monthsComboBox = new JComboBox<Integer>();
for (int i = 1; i <= 12; i++) {
monthsComboBox.addItem(new Integer(i));
}
monthsComboBox.setSelectedIndex(calendar.get(Calendar.MONTH));
monthsComboBox.addActionListener(new ActionListener() { @Override
public void actionPerformed(ActionEvent actionEvent) {
int day = calendar.get(Calendar.DAY_OF_MONTH);
calendar.set(Calendar.DAY_OF_MONTH, 1);
calendar.set(Calendar.MONTH, monthsComboBox.getSelectedIndex());
int maxDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
calendar.set(Calendar.DAY_OF_MONTH, day > maxDay ? maxDay : day);
updateView();
}
});
JPanel monthPanel = new JPanel();
yearMonthPanel.add(monthPanel, BorderLayout.EAST);
monthPanel.setLayout(new BorderLayout());
monthPanel.add(monthsLabel, BorderLayout.WEST);
monthPanel.add(monthsComboBox, BorderLayout.CENTER); daysModel = new AbstractTableModel() {
/**
*
*/
private static final long serialVersionUID = -3517455337953024330L;
public int getRowCount() {
return 7;
} public int getColumnCount() {
return 7;
} public Object getValueAt(int row, int column) {
if (row == 0) {
return getHeader(column);
}
row--;
Calendar calendar = (Calendar) Demo6.this.calendar.clone();
calendar.set(Calendar.DAY_OF_MONTH, 1);
int dayCount = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
int moreDayCount = calendar.get(Calendar.DAY_OF_WEEK) - 1;
int index = row * 7 + column;
int dayIndex = index - moreDayCount + 1;
if (index < moreDayCount || dayIndex > dayCount) {
return null;
} else {
return new Integer(dayIndex);
}
}
}; daysTable = new CalendarTable(daysModel, calendar);
daysTable.setCellSelectionEnabled(true);
daysTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); daysTable.setDefaultRenderer(daysTable.getColumnClass(0), new TableCellRenderer() {
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
boolean hasFocus, int row, int column) {
String text = (value == null) ? "" : value.toString();
JLabel cell = new JLabel(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 cell;
}
});
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;
default:
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) - 1);
}
public static class CalendarTable extends JTable {
private static final long serialVersionUID = 1L;
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, toggle, 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("Calendar Application");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Demo6 myCalendar = new Demo6();
myCalendar.init();
frame.getContentPane().add(myCalendar);
frame.setSize(240, 172); }
}

Java 由浅入深GUI编程实战练习(三)的更多相关文章

  1. Java 由浅入深GUI编程实战练习(二)

    一,项目简介 1.利用Java GUI 绘制图像界面,设置整体布局 2.编写一个随机数生成1~100的随机数 3.编写一个验证类,用于验证用户输入值与生成随机数是否相等并记录用户猜测次数,当用户猜测成 ...

  2. Java 由浅入深GUI编程实战练习(一)

    项目简介: 1.实现利用下拉菜单的方式选择发送快捷语句: 2.实现对留言信息内容的置顶处理以及至尾处理: 3.实现清屏处理或现实保留部分留言内容: 运行界面: 代码展示: import java.aw ...

  3. 【Java】GUI编程

    GUI编程 前言 某koukou老师的任务罢了,好在狂神老师居然有GUI的课,只能说是有救星了. [狂神说Java]GUI编程入门到游戏实战 最好笑的是,老师要求掌握的居然是14年的知识,就连狂神在上 ...

  4. 5.JAVA之GUI编程窗体事件

    我们回顾下第三篇时的内容: 在3.JAVA之GUI编程Frame窗口中窗体是无法直接关闭的,想要关闭须进程管理器结束进程方式关掉. 现在我们就来解决下这个问题. ******************* ...

  5. Java之GUI编程(一)

    GUI全称Graphical User Interfaces,意为图形用户户界面,又称为图形用户接口.GUI指的就是採用图形方式显示的计算机操作用户界面,打个例如吧.我们点击QQ图标,就会弹出一个QQ ...

  6. java基础—GUI编程(一)

    一.AWT介绍

  7. Java之GUI编程

    GUI编程 组建 窗口 弹窗 面板 文本框 列表框 按钮 图片 监听事件 鼠标 键盘事件 破解工具 1.简介 GUI的核心技术:Swing AWT 为什么不流行? 界面不美观. 需要jre环境.(没必 ...

  8. 1.JAVA之GUI编程概述

          下列内容为本人看毕向东老师java视频教程学习笔记! JAVA GUI图形用户界面编程: Windows 操作系统提供两种操作方式:                             ...

  9. 2.JAVA之GUI编程布局

    布局管理器 容器中的组件排放方式,就是布局 常见的布局管理器: **************************************************** 1.FlowLayout(流式 ...

随机推荐

  1. atx-agent minicap、minitouch源码分析

    项目描述: 因为公司需要,特别研究了一下openatx系列手机群控源码 源码地址: https://github.com/openatx 该项目主要以go语言来编写服务端.集成 OpenSTF中核心组 ...

  2. git命令别名(Alias)

    每次切换分支: git ckeckout branch_name 等命令费时又费力,git 别名配置起来: 别名配置: git config --global alias.ck ckeckout 其他 ...

  3. Hadoop 集群安装(主节点安装)

    1.下载安装包及测试文档 切换目录到/tmp view plain copy cd /tmp 下载Hadoop安装包 view plain copy wget http://192.168.1.100 ...

  4. [蓝牙前沿应用] 照明即平台 —— 通过蓝牙增强服务提高照明投资回报率(蓝牙MESH、定位AoA、AoD)

      1.照明即平台(LAAP) 随着照明设施向LED转型,他们也在利用类似于蓝牙MESH的技术,创建一个可连接的照明平台,作为他们建筑的中枢神经系统.这些系统除了提供先进的照明控制,还建立了一个增强版 ...

  5. sha256 js 前端加密

    <html> <head></head> <body> <script type="text/javascript" src= ...

  6. [Swift]LeetCode483. 最小好进制 | Smallest Good Base

    For an integer n, we call k>=2 a good base of n, if all digits of n base k are 1. Now given a str ...

  7. Redis 设计与实现 (六)--发布订阅

    发布订阅 相关命令:publish(发布)  subscribe(订阅)  psubscribe(支持通配符匹配) 发布者--频道--订阅者(发布和订阅都是客户端频道是服务端) 1.publish-  ...

  8. Java数据结构与算法 - 外部存储

    Q: 什么是外部存储? A: 外部存储特指某类磁盘系统,例如在大多数台式电脑或服务器中的硬盘. Q: 如何访问外部存储? A: 我们所学的数据结构都是假设数据存储在内存中,但是,在很多情况下要处理的数 ...

  9. python学习笔记(十)、文件操作

    在前面我们了解到了没得模块,其中有一个模块为fileinput,为文件操作模块,不知道小伙伴们是否还记得? 1 打开文件 要打开文件,可以使用fileinput中的fileinput.input函数进 ...

  10. performance数据

    // 获取 performance 数据 var performance = { // memory 是非标准属性,只在 Chrome 有 // 财富问题:我有多少内存 memory: { usedJ ...