用户界面分析与设计(SSD4) 实验四
Take Assessment: Exercise 4
1.需求分析
此任务是创建一个实现完整日期/时间控制面板(以标准Windows 98控制面板为模式)的接口。如下所示,这个接口应该包含一个选项卡带有两个窗格的控件:一个用于设置日期和时间,另一个用于设置时区。“日期和时间”选项卡将是此作业的新选项,应与此处显示的选项相同,你不必在右边画钟面。对于“时区”选项卡,应该使用在上一次编程分配中创建的接口组件和代码。
总的来说,这个接口维护了九条数据:
(1)秒钟设置(0到59)
(2)分钟设置(0到59)
(3)小时设置(在24小时时间内,0到23)
(4)一个月中的一天
(5)一年中的一个月
(6)年份
(7)当前选定时区的索引。
(8)当前选定时区从格林尼治时间到格林尼治时间的偏移量(以分钟为单位)。
(9)当前选定时区的标题。
除此之外,接口的最后一部分应该包含“OK”、“Cancel”和“Apply”按钮。当用户按下“确定”或“取消”按钮时,应生成当前设置的小报告(类似于下面所示),然后退出接口(通过“卸载”表单)。如果用户按下“Apply”按钮,您应该生成类似的报告,而不是退出界面。
2.概要设计
(1) 根据要求设置界面,第一个日期/时间界面主要用到标签、按钮、表格、下拉框、微调器等组件,第二个时区则是下拉框、图标等应用。
(2) 对相应的部件进行初始化赋值、数据范围设计。
(3) 实现时钟功能模块,此模块与实验三基本一致,相比而言多了一个选项即AM与PM的选择,选用if语句判断时钟的区间即可选定。
(4) 实现日历功能,用户通过月份、年份的选项设置自定义的日期,然后通过年月去计算该月份有多少天、第一天是星期几,从而将数据导入到表格中。
(5) 设置月份下拉框、年份微调器的状态改变事件,通过选择月份、年份程序将自动更新日历排版。
(6) 设置时区的更改事件,下拉框选择时区时,地图分布将选择不同区域,此处可以通过两个标签不断变更位置显示;
(7) 自动调整时间与时区的功能,时区改变,地图中选中的区域也将改变,同时,点击按钮将显示“√”,同时更改时区时间也会发生改变,未选中时更改时区时间不受影响。
(8) 实现点击“OK”、“Cancel”、“Apply”按钮时,弹出对应的程序目前时间年月时区等相关信息,关闭“OK”、“Cancel”弹出的对话框时,程序也随之关闭,关闭“Apply”弹出的对话框程序仍保持运行。
3.详细设计
(1) 第一个日期/时间界面设计通过选用jLabel、jButton、jSpinner、jComboBox、jTable、jTabbedPane、jRadioButton等组件进行设计,主要是位置的布局、表格的单元格大小设计、日历时钟的到标题边框等设计。第二个时区界面主要是通过jComboBox、jLabel等设计,添加图标、排版布局即可。
(2) 对相应的标签进行初始赋值、下拉框、微调器等进行数据设计;
(3)回顾实验三的设计,实现时钟的部分,增添代码判断时钟是在0-12之间还是12-23之间从而选择AM、PM。
(4)实现日历功能,通过数据类型转换获取用户当前选中的年月,再利用Calendar函数进行相关年月、星期信息的获取,再对表格进行数据清空重新写入。
int year = Integer.parseInt(jSpinner1.getValue().toString());
int month = jComboBox1.getSelectedIndex()+1;
int day = 1;
Calendar cal = Calendar.getInstance();
cal.set(year,month - 1,day);
int days = cal.getActualMaximum(Calendar.DATE);
int week = cal.get(Calendar.DAY_OF_WEEK);
//初始化日历
//清空表格数据
int count = 0;
while(count<6) {
jTable1.setValueAt(null, count, 0);
jTable1.setValueAt(null, count, 1);
jTable1.setValueAt(null, count, 2);
jTable1.setValueAt(null, count, 3);
jTable1.setValueAt(null, count, 4);
jTable1.setValueAt(null, count, 5);
jTable1.setValueAt(null, count, 6);
count++;
}
//输出指定年月日的日历
int day_count = 1;
//输出日历第一行数据
for(int i=week-1; i<7; i++){
jTable1.setValueAt(day_count++, 0, i);
}
//输出日历剩余数据
for(int i=1; i<6; i++){ //控制行数
for(int j=0; j<7; j++){ //控制列数
if(day_count <= days){
jTable1.setValueAt(day_count++, i, j);
}
else{
i = 6;
break;
}
}
}
(5) 设置月份下拉框、年份微调器的状态改变事件,即将日历的初始化过程修改添加。
(6) 通过选择时区下拉框更改指示图标的位置,显示更改后的时区,主要利用下拉框的状态改变事件以及jLabel的定位设计。
int n = jComboBox2.getSelectedIndex();
jButton2.setBounds(x1+n*16, y1, width, height);
jButton6.setBounds(x2+n*16, y2, width, height);
jLabel3.setText("Current Time Zone : "+jComboBox2.getSelectedItem());
(7) 对于自动更新时间功能则是将时区选择下拉框与时钟的部件相关联重而改变。
(8) 点击“OK”、“Cancel”、“Apply”按钮事件,主要是通过对程序现在保存的数据进行获取与类型转化,重而依照一定的格式输出到弹出的对话框,其中“OK”、“Cancel”按钮还应添加关闭程序窗口的代码,System.exit(state);。
4.调试分析
(1) 日历功能实现的过程中,星期判定一直出问题,对于函数的返回值并不是很清楚,后来发现是函数用的并非十分恰当,最终选用cal.getActualMaximum(Calendar.DATE)以及cal.get(Calendar.DAY_OF_WEEK)两个方法获得相关月份的天数与星期问题。
(2) 加载地图时一直显示空指针错误,加载方式换了好几种,图片也换了很多,也重复操作了很久,发现都解决不了,依照百度的意见也没行得通。后来在室友的帮助下发现是文件创建的不对,这个程序创建了一个javaFX文件,所以添加图片后就运行不了,之后将其换回到一般的java即可正常运行。
(3) 对于带标题边框内容,最开始还想着设计个标签加边框做到,结果仔细查阅后发现是直接有这个功能的,边框的选择也有很多。
5.用户使用说明
(1) 首先进入程序,用户可以看到现今的日历与时钟情况,同时用户可以选择继续在该界面操作还是直接进入到时区设置界面。
(2) 在日期/时间界面中用户可以选择不同的月份与年份进而查看不同日历信息,也可以对时间进行自定义设计,通过直接输入对应数字或者点击上下键都是可以的。
(3) 在日期/时间界面的下方则是显示着程序中的时间所在的时区信息。
(4) 时区界面主要是地图与时区的相对应,在地图的上下方各有一个图标显示当前所在的时区,选择时区的下拉框后可以更改地图中的图标指向标。
(5) 点击选择自动更新时钟对应时区,该框会出现“√”的符号,同时该文字也将显示出来,反之改段介绍文字不能操作的。
6.测试结果
(1)查看日历
(2)更改日历
(3)查看时钟
(4)更改时钟
(5)更改时区
(6)“OK”按钮
(7)“Cancel”按钮
(8)“Apply”按钮
7.附录
注:更加详细清晰的代码可参考文件 实验四源码
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */
package ssd4.program4;
import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JOptionPane;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
/** * * @author Summer */
public class Exercise4 extends javax.swing.JFrame {
/** * Creates new form SSD4_program4_Frame */
public Exercise4() {
initComponents();
this.setTitle("Date/Time");
this.setLocationRelativeTo(null);
this.setResizable(false);
jComboBox1.setSelectedIndex(10);
int hour = Integer.parseInt(jSpinner1.getValue().toString());
if(0<=hour && hour<12){
jRadioButton1.setSelected(true);
jRadioButton2.setSelected(false);
}
else{
jRadioButton1.setSelected(false);
jRadioButton2.setSelected(true);
}
jButton1.setText("");
jLabel5.setEnabled(false);
jComboBox2.setSelectedIndex(11);
Timer timer = new Timer();
//定时器执行任务
timer.schedule(new TimerTask() {
@Override
public void run() {
int second = Integer.parseInt(jSpinner3.getValue().toString());
second++;
jSpinner3.setValue(second);
repaint();
}
}, 0, 1000);
int year = Integer.parseInt(jSpinner1.getValue().toString());
int month = jComboBox1.getSelectedIndex()+1;
int day = 1;
Calendar cal = Calendar.getInstance();
cal.set(year,month - 1,day);
int days = cal.getActualMaximum(Calendar.DATE);
int week = cal.get(Calendar.DAY_OF_WEEK);
//初始化日历
//清空表格数据
int count = 0;
while(count<6) {
jTable1.setValueAt(null, count, 0);
jTable1.setValueAt(null, count, 1);
jTable1.setValueAt(null, count, 2);
jTable1.setValueAt(null, count, 3);
jTable1.setValueAt(null, count, 4);
jTable1.setValueAt(null, count, 5);
jTable1.setValueAt(null, count, 6);
count++;
}
//输出指定年月日的日历
int day_count = 1;
//输出日历第一行数据
for(int i=week-1; i<7; i++){
jTable1.setValueAt(day_count++, 0, i);
}
//输出日历剩余数据
for(int i=1; i<6; i++){ //控制行数
for(int j=0; j<7; j++){ //控制列数
if(day_count <= days){
jTable1.setValueAt(day_count++, i, j);
}
else{
i = 6;
break;
}
}
}
//设置默认定位东西12区
int x1 = jButton2.getX();
int y1 = jButton2.getY();
int x2 = jButton6.getX();
int y2 = jButton6.getY();
int width = 10;
int height = 23;
jButton2.setBounds(x1+8*22, y1, width, height);
jButton6.setBounds(x2+8*22, y2, width, height);
//使时区可以定位变动
jPanel2.setLayout(null);
}
/** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
buttonGroup2 = new javax.swing.ButtonGroup();
jTabbedPane1 = new javax.swing.JTabbedPane();
jPanel1 = new javax.swing.JPanel();
jPanel3 = new javax.swing.JPanel();
jScrollPane1 = new javax.swing.JScrollPane();
jTable1 = new javax.swing.JTable();
jSpinner1 = new javax.swing.JSpinner();
jComboBox1 = new javax.swing.JComboBox<>();
jPanel4 = new javax.swing.JPanel();
jSpinner2 = new javax.swing.JSpinner();
jRadioButton1 = new javax.swing.JRadioButton();
jSpinner4 = new javax.swing.JSpinner();
jRadioButton2 = new javax.swing.JRadioButton();
jSpinner3 = new javax.swing.JSpinner();
jLabel3 = new javax.swing.JLabel();
jPanel2 = new javax.swing.JPanel();
jComboBox2 = new javax.swing.JComboBox<>();
jPanel5 = new javax.swing.JPanel();
jButton1 = new javax.swing.JButton();
jLabel5 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jButton2 = new javax.swing.JButton();
jButton6 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
jButton4 = new javax.swing.JButton();
jButton5 = new javax.swing.JButton();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jTabbedPane1.setFont(new java.awt.Font("宋体", 0, 18)); // NOI18N
jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
jPanel1.setFont(new java.awt.Font("宋体", 0, 18)); // NOI18N
jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)), "Date", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("宋体", 0, 18))); // NOI18N
jTable1.setFont(new java.awt.Font("宋体", 0, 18)); // NOI18N
jTable1.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{ 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}
},
new String [] {
"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
}
) {
boolean[] canEdit = new boolean [] {
false, false, false, false, false, false, false
};
public boolean isCellEditable(int rowIndex, int columnIndex) {
return canEdit [columnIndex];
}
});
jTable1.setRowHeight(30);
jTable1.setRowSelectionAllowed(false);
jScrollPane1.setViewportView(jTable1);
jTable1.getColumnModel().getSelectionModel().setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
if (jTable1.getColumnModel().getColumnCount() > 0) {
jTable1.getColumnModel().getColumn(0).setMinWidth(35);
jTable1.getColumnModel().getColumn(0).setPreferredWidth(35);
jTable1.getColumnModel().getColumn(0).setMaxWidth(35);
jTable1.getColumnModel().getColumn(1).setMinWidth(35);
jTable1.getColumnModel().getColumn(1).setPreferredWidth(35);
jTable1.getColumnModel().getColumn(1).setMaxWidth(35);
jTable1.getColumnModel().getColumn(2).setMinWidth(35);
jTable1.getColumnModel().getColumn(2).setPreferredWidth(35);
jTable1.getColumnModel().getColumn(2).setMaxWidth(35);
jTable1.getColumnModel().getColumn(3).setMinWidth(35);
jTable1.getColumnModel().getColumn(3).setPreferredWidth(35);
jTable1.getColumnModel().getColumn(3).setMaxWidth(35);
jTable1.getColumnModel().getColumn(4).setMinWidth(35);
jTable1.getColumnModel().getColumn(4).setPreferredWidth(35);
jTable1.getColumnModel().getColumn(4).setMaxWidth(35);
jTable1.getColumnModel().getColumn(5).setMinWidth(35);
jTable1.getColumnModel().getColumn(5).setPreferredWidth(35);
jTable1.getColumnModel().getColumn(5).setMaxWidth(35);
jTable1.getColumnModel().getColumn(6).setMinWidth(35);
jTable1.getColumnModel().getColumn(6).setPreferredWidth(35);
jTable1.getColumnModel().getColumn(6).setMaxWidth(35);
}
jSpinner1.setFont(new java.awt.Font("宋体", 0, 18)); // NOI18N
jSpinner1.setModel(new javax.swing.SpinnerNumberModel(2018, null, null, 1));
jSpinner1.addChangeListener(new javax.swing.event.ChangeListener() {
public void stateChanged(javax.swing.event.ChangeEvent evt) {
jSpinner1StateChanged(evt);
}
});
jComboBox1.setFont(new java.awt.Font("宋体", 0, 18)); // NOI18N
jComboBox1.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }));
jComboBox1.addItemListener(new java.awt.event.ItemListener() {
public void itemStateChanged(java.awt.event.ItemEvent evt) {
jComboBox1ItemStateChanged(evt);
}
});
jComboBox1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jComboBox1ActionPerformed(evt);
}
});
javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
jPanel3.setLayout(jPanel3Layout);
jPanel3Layout.setHorizontalGroup(
jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel3Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(36, 36, 36)
.addComponent(jSpinner1, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 246, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);
jPanel3Layout.setVerticalGroup(
jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel3Layout.createSequentialGroup()
.addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel3Layout.createSequentialGroup()
.addGap(11, 11, 11)
.addComponent(jSpinner1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 220, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);
jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)), "Time", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("宋体", 0, 18))); // NOI18N
jSpinner2.setFont(new java.awt.Font("宋体", 0, 18)); // NOI18N
jSpinner2.setModel(new javax.swing.SpinnerNumberModel(11, -1, 24, 1));
jSpinner2.addChangeListener(new javax.swing.event.ChangeListener() {
public void stateChanged(javax.swing.event.ChangeEvent evt) {
jSpinner2StateChanged(evt);
}
});
buttonGroup2.add(jRadioButton1);
jRadioButton1.setFont(new java.awt.Font("宋体", 0, 18)); // NOI18N
jRadioButton1.setText("AM");
jSpinner4.setFont(new java.awt.Font("宋体", 0, 18)); // NOI18N
jSpinner4.setModel(new javax.swing.SpinnerNumberModel(58, -1, 60, 1));
jSpinner4.addChangeListener(new javax.swing.event.ChangeListener() {
public void stateChanged(javax.swing.event.ChangeEvent evt) {
jSpinner4StateChanged(evt);
}
});
buttonGroup2.add(jRadioButton2);
jRadioButton2.setFont(new java.awt.Font("宋体", 0, 18)); // NOI18N
jRadioButton2.setText("PM");
jSpinner3.setFont(new java.awt.Font("宋体", 0, 18)); // NOI18N
jSpinner3.setModel(new javax.swing.SpinnerNumberModel(52, -1, 60, 1));
jSpinner3.addChangeListener(new javax.swing.event.ChangeListener() {
public void stateChanged(javax.swing.event.ChangeEvent evt) {
jSpinner3StateChanged(evt);
}
});
javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
jPanel4.setLayout(jPanel4Layout);
jPanel4Layout.setHorizontalGroup(
jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel4Layout.createSequentialGroup()
.addComponent(jSpinner2, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(10, 10, 10)
.addComponent(jSpinner4, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jSpinner3, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(7, 7, 7)
.addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jRadioButton2)
.addComponent(jRadioButton1))
.addContainerGap(10, Short.MAX_VALUE))
);
jPanel4Layout.setVerticalGroup(
jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel4Layout.createSequentialGroup()
.addComponent(jRadioButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jRadioButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jSpinner2, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jSpinner4, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jSpinner3, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap())
);
jLabel3.setFont(new java.awt.Font("宋体", 0, 18)); // NOI18N
jLabel3.setText("Current Time Zone : (GMT -12:00) Eniwelok, Kwajalein");
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(jLabel3))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(24, 24, 24)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, 294, Short.MAX_VALUE))
.addGap(18, 18, 18)
.addComponent(jLabel3)
.addContainerGap(13, Short.MAX_VALUE))
);
jTabbedPane1.addTab("Date & Time", jPanel1);
jPanel2.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
jPanel2.setFont(new java.awt.Font("宋体", 0, 18)); // NOI18N
jComboBox2.setFont(new java.awt.Font("宋体", 0, 18)); // NOI18N
jComboBox2.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "(GMT -1:00) Eniwelok, Kwajalein", "(GMT -2:00) Eniwelok, Kwajalein", "(GMT -3:00) Eniwelok, Kwajalein", "(GMT -4:00) Eniwelok, Kwajalein", "(GMT -5:00) Eniwelok, Kwajalein", "(GMT -6:00) Eniwelok, Kwajalein", "(GMT -7:00) Eniwelok, Kwajalein", "(GMT -8:00) Eniwelok, Kwajalein", "(GMT -9:00) Eniwelok, Kwajalein", "(GMT -10:00) Eniwelok, Kwajalein", "(GMT -11:00) Eniwelok, Kwajalein", "(GMT -12:00) Eniwelok, Kwajalein", "(GMT +11:00) Eniwelok, Kwajalein", "(GMT +10:00) Eniwelok, Kwajalein", "(GMT +9:00) Eniwelok, Kwajalein", "(GMT +8:00) Eniwelok, Kwajalein", "(GMT +7:00) Eniwelok, Kwajalein", "(GMT +6:00) Eniwelok, Kwajalein", "(GMT +5:00) Eniwelok, Kwajalein", "(GMT +4:00) Eniwelok, Kwajalein", "(GMT +3:00) Eniwelok, Kwajalein", "(GMT +2:00) Eniwelok, Kwajalein", "(GMT +1:00) Eniwelok, Kwajalein" }));
jComboBox2.addItemListener(new java.awt.event.ItemListener() {
public void itemStateChanged(java.awt.event.ItemEvent evt) {
jComboBox2ItemStateChanged(evt);
}
});
jButton1.setFont(new java.awt.Font("宋体", 0, 18)); // NOI18N
jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jButton1MouseClicked(evt);
}
});
jLabel5.setFont(new java.awt.Font("宋体", 0, 14)); // NOI18N
jLabel5.setText("Automatically adjust clock for daylight saving changes");
javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
jPanel5.setLayout(jPanel5Layout);
jPanel5Layout.setHorizontalGroup(
jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel5Layout.createSequentialGroup()
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLabel5)
.addContainerGap())
);
jPanel5Layout.setVerticalGroup(
jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel5Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, 24, Short.MAX_VALUE)
.addContainerGap())
.addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
jLabel4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ssd4/program4/timezone_map_small.jpg"))); // NOI18N
jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ssd4/program4/downTriangle.jpg"))); // NOI18N
jButton6.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ssd4/program4/upTriangle.jpg"))); // NOI18N
javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addGap(82, 82, 82)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jComboBox2, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton6, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGroup(jPanel2Layout.createSequentialGroup()
.addGap(49, 49, 49)
.addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(51, Short.MAX_VALUE))
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(12, 12, 12)
.addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLabel4)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton6, javax.swing.GroupLayout.PREFERRED_SIZE, 23, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(30, 30, 30))
);
jTabbedPane1.addTab("Time Zone", jPanel2);
jButton3.setFont(new java.awt.Font("宋体", 0, 18)); // NOI18N
jButton3.setText("OK");
jButton3.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jButton3MouseClicked(evt);
}
});
jButton4.setFont(new java.awt.Font("宋体", 0, 18)); // NOI18N
jButton4.setText("Cancel");
jButton4.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jButton4MouseClicked(evt);
}
});
jButton5.setFont(new java.awt.Font("宋体", 0, 18)); // NOI18N
jButton5.setText("Apply");
jButton5.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jButton5MouseClicked(evt);
}
});
jLabel1.setFont(new java.awt.Font("宋体", 0, 18)); // NOI18N
jLabel1.setText("Summer");
jLabel2.setFont(new java.awt.Font("宋体", 0, 18)); // NOI18N
jLabel2.setText("Example");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel1)
.addGap(27, 27, 27)
.addComponent(jLabel2))
.addGroup(layout.createSequentialGroup()
.addGap(317, 317, 317)
.addComponent(jButton3)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButton4)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton5))
.addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))
.addGap(0, 13, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(jLabel1))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 407, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton3)
.addComponent(jButton4)
.addComponent(jButton5))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void jSpinner3StateChanged(javax.swing.event.ChangeEvent evt) {
// TODO add your handling code here:
int hour = Integer.parseInt(jSpinner2.getValue().toString());
int minute = Integer.parseInt(jSpinner4.getValue().toString());
int second = Integer.parseInt(jSpinner3.getValue().toString());
//秒钟变化
if(second == 60){
//分钟变化
if(minute == 59){
//时钟变化
if(hour == 24){
jSpinner2.setValue(0);
}
else{
jSpinner2.setValue(hour+1);
}
jSpinner4.setValue(0);
}
else{
jSpinner4.setValue(minute+1);
}
jSpinner3.setValue(0);
}
//秒钟变化
if(second == -1){
//分钟变化
if(minute == 0){
//时钟变化
if(hour == 0){
jSpinner2.setValue(24);
}
else{
jSpinner2.setValue(hour-1);
}
jSpinner4.setValue(59);
}
else{
jSpinner4.setValue(minute-1);
}
jSpinner3.setValue(59);
}
if(0<=hour && hour<12){
jRadioButton1.setSelected(true);
jRadioButton2.setSelected(false);
}
else{
jRadioButton1.setSelected(false);
jRadioButton2.setSelected(true);
}
}
private void jSpinner4StateChanged(javax.swing.event.ChangeEvent evt) {
// TODO add your handling code here:
int hour = Integer.parseInt(jSpinner2.getValue().toString());
int minute = Integer.parseInt(jSpinner4.getValue().toString());
//分钟变化
if(minute == 60){
//时钟变化
if(hour == 24){
jSpinner2.setValue(0);
}
else{
jSpinner2.setValue(hour+1);
}
jSpinner4.setValue(0);
}
//分钟变化
if(minute == -1){
//时钟变化
if(hour == 0){
jSpinner2.setValue(23);
}
else{
jSpinner2.setValue(hour-1);
}
jSpinner4.setValue(59);
}
if(0<=hour && hour<12){
jRadioButton1.setSelected(true);
jRadioButton2.setSelected(false);
}
else{
jRadioButton1.setSelected(false);
jRadioButton2.setSelected(true);
}
}
private void jSpinner2StateChanged(javax.swing.event.ChangeEvent evt) {
// TODO add your handling code here:
int hour = Integer.parseInt(jSpinner2.getValue().toString());
//时钟变化
if(hour == 24){
jSpinner2.setValue(0);
}
//时钟变化
if(hour == -1){
jSpinner2.setValue(11);
}
if(0<=hour && hour<12){
jRadioButton1.setSelected(true);
jRadioButton2.setSelected(false);
}
else{
jRadioButton1.setSelected(false);
jRadioButton2.setSelected(true);
}
}
private void jComboBox1ItemStateChanged(java.awt.event.ItemEvent evt) {
// TODO add your handling code here:
int year = Integer.parseInt(jSpinner1.getValue().toString());
int month = jComboBox1.getSelectedIndex()+1;
int day = 1;
Calendar cal = Calendar.getInstance();
cal.set(year,month - 1,day);
int days = cal.getActualMaximum(Calendar.DATE);
int week = cal.get(Calendar.DAY_OF_WEEK);
//初始化日历
//清空表格数据
int count = 0;
while(count<6) {
jTable1.setValueAt(null, count, 0);
jTable1.setValueAt(null, count, 1);
jTable1.setValueAt(null, count, 2);
jTable1.setValueAt(null, count, 3);
jTable1.setValueAt(null, count, 4);
jTable1.setValueAt(null, count, 5);
jTable1.setValueAt(null, count, 6);
count++;
}
//输出指定年月日的日历
int day_count = 1;
//输出日历第一行数据
for(int i=week-1; i<7; i++){
jTable1.setValueAt(day_count++, 0, i);
}
//输出日历剩余数据
for(int i=1; i<6; i++){ //控制行数
for(int j=0; j<7; j++){ //控制列数
if(day_count <= days){
jTable1.setValueAt(day_count++, i, j);
}
else{
i = 6;
break;
}
}
}
}
private void jSpinner1StateChanged(javax.swing.event.ChangeEvent evt) {
// TODO add your handling code here:
int year = Integer.parseInt(jSpinner1.getValue().toString());
int month = jComboBox1.getSelectedIndex()+1;
int day = 1;
Calendar cal = Calendar.getInstance();
cal.set(year,month - 1,day);
int days = cal.getActualMaximum(Calendar.DATE);
int week = cal.get(Calendar.DAY_OF_WEEK);
//初始化日历
//清空表格数据
int count = 0;
while(count<6) {
jTable1.setValueAt(null, count, 0);
jTable1.setValueAt(null, count, 1);
jTable1.setValueAt(null, count, 2);
jTable1.setValueAt(null, count, 3);
jTable1.setValueAt(null, count, 4);
jTable1.setValueAt(null, count, 5);
jTable1.setValueAt(null, count, 6);
count++;
}
//输出指定年月日的日历
int day_count = 1;
//输出日历第一行数据
for(int i=week-1; i<7; i++){
jTable1.setValueAt(day_count++, 0, i);
}
//输出日历剩余数据
for(int i=1; i<6; i++){ //控制行数
for(int j=0; j<7; j++){ //控制列数
if(day_count <= days){
jTable1.setValueAt(day_count++, i, j);
}
else{
i = 6;
break;
}
}
}
}
private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
if(jButton1.getText().length() == 0){
jButton1.setText("√");
jLabel5.setEnabled(true);
}
else{
jButton1.setText("");
jLabel5.setEnabled(false);
}
}
private void jComboBox2ItemStateChanged(java.awt.event.ItemEvent evt) {
// TODO add your handling code here:
int x1 = 78;
int y1 = 50;
int x2 = 78;
int y2 = 269;
int width = 10;
int height = 23;
int n = jComboBox2.getSelectedIndex();
jButton2.setBounds(x1+n*16, y1, width, height);
jButton6.setBounds(x2+n*16, y2, width, height);
jLabel3.setText("Current Time Zone : "+jComboBox2.getSelectedItem());
}
private void jButton3MouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
int year = Integer.parseInt(jSpinner1.getValue().toString());
int month = jComboBox1.getSelectedIndex()+1;
Calendar cal = Calendar.getInstance();
int a = 1;
cal.set(year,month - 1,a);
int day = cal.getActualMaximum(Calendar.DATE);
String hour = jSpinner2.getValue().toString();
String minute = jSpinner4.getValue().toString();
String second = jSpinner3.getValue().toString();
String timezone = jComboBox2.getSelectedItem().toString();
String autoDaylight;
if(jButton1.getText().length() == 0)
autoDaylight = "false";
else
autoDaylight = "true";
String s = " OK\n====================\n Year = "+year
+"\n Month = "+month+"\n Day = "+day+"\n Hour = "+hour+"\n Minute = "+minute
+"\n Second = "+second+"\n Timezone = "+timezone+"\n Auto Daylight = "+autoDaylight
+"\n====================\n (Time saved)";
JOptionPane.showMessageDialog(null, s,
"Date_and_time",JOptionPane.PLAIN_MESSAGE);
System.exit(1);
}
private void jButton4MouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
int year = Integer.parseInt(jSpinner1.getValue().toString());
int month = jComboBox1.getSelectedIndex()+1;
Calendar cal = Calendar.getInstance();
int a = 1;
cal.set(year,month - 1,a);
int day = cal.getActualMaximum(Calendar.DATE);
String hour = jSpinner2.getValue().toString();
String minute = jSpinner4.getValue().toString();
String second = jSpinner3.getValue().toString();
String timezone = jComboBox2.getSelectedItem().toString();
String autoDaylight;
if(jButton1.getText().length() == 0)
autoDaylight = "false";
else
autoDaylight = "true";
String s = " Cancel\n====================\n Year = "+year
+"\n Month = "+month+"\n Day = "+day+"\n Hour = "+hour+"\n Minute = "+minute
+"\n Second = "+second+"\n Timezone = "+timezone+"\n Auto Daylight = "+autoDaylight
+"\n====================\n (Time not saved)";
JOptionPane.showMessageDialog(null, s,
"Date_and_time",JOptionPane.PLAIN_MESSAGE);
System.exit(1);
}
private void jButton5MouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
int year = Integer.parseInt(jSpinner1.getValue().toString());
int month = jComboBox1.getSelectedIndex()+1;
Calendar cal = Calendar.getInstance();
int a = 1;
cal.set(year,month - 1,a);
int day = cal.getActualMaximum(Calendar.DATE);
String hour = jSpinner2.getValue().toString();
String minute = jSpinner4.getValue().toString();
String second = jSpinner3.getValue().toString();
String timezone = jComboBox2.getSelectedItem().toString();
String autoDaylight;
if(jButton1.getText().length() == 0)
autoDaylight = "false";
else
autoDaylight = "true";
String s = " Apply\n====================\n Year = "+year
+"\n Month = "+month+"\n Day = "+day+"\n Hour = "+hour+"\n Minute = "+minute
+"\n Second = "+second+"\n Timezone = "+timezone+"\n Auto Daylight = "+autoDaylight
+"\n====================\n (Time saved)";
JOptionPane.showMessageDialog(null, s,
"Date_and_time",JOptionPane.PLAIN_MESSAGE);
}
private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
/** * @param args the command line arguments */
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Exercise4.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Exercise4.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Exercise4.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Exercise4.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Exercise4().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.ButtonGroup buttonGroup2;
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton4;
private javax.swing.JButton jButton5;
private javax.swing.JButton jButton6;
private javax.swing.JComboBox<String> jComboBox1;
private javax.swing.JComboBox<String> jComboBox2;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JPanel jPanel3;
private javax.swing.JPanel jPanel4;
private javax.swing.JPanel jPanel5;
private javax.swing.JRadioButton jRadioButton1;
private javax.swing.JRadioButton jRadioButton2;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JSpinner jSpinner1;
private javax.swing.JSpinner jSpinner2;
private javax.swing.JSpinner jSpinner3;
private javax.swing.JSpinner jSpinner4;
private javax.swing.JTabbedPane jTabbedPane1;
private javax.swing.JTable jTable1;
// End of variables declaration
}
还没有评论,来说两句吧...