📄 dateandtime.java
字号:
/**
*
*/
package edu.swjtu.sist.java.calendar;
import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.text.SimpleDateFormat;
import java.awt.event.*;
/**
* @author 518lee@gmail.com
* @version 1.0
*/
public class DateAndTime extends JFrame implements ActionListener,
MouseListener, ItemListener, AdjustmentListener
{
private JTabbedPane tabPanel = new JTabbedPane();
private LayoutPanel layoutPanel = new LayoutPanel();
private TimeZonePanel timezonePanel = new TimeZonePanel();
private InternetTimePanel internetTimePanel = new InternetTimePanel();
private JPanel pn3 = new JPanel();
private JButton confirmBtn = new JButton(" 确定 ");
private JButton cancelBtn = new JButton(" 取消 ");
private JButton applyBtn = new JButton(" 应用 ");
private DateCalendarPanel dateCalendarPanel = new DateCalendarPanel();;
private ClockPanel clockPanel = new ClockPanel();
// 时区选择菜单
private JComboBox timezoneBox = new JComboBox();
// 时间属性变量
private Calendar today;
private int year, month, day, week, hour, minute, second, m_second;
// protected boolean hasModified = false;
public DateAndTime()
{
today = Calendar.getInstance();//
year = today.get(Calendar.YEAR);// 当前年
month = today.get(Calendar.MONTH);// 月
day = today.get(Calendar.DATE);// 日
hour = today.get(Calendar.HOUR);// 时
minute = today.get(Calendar.MINUTE);// 分
second = today.get(Calendar.SECOND);// 秒
tabPanel.addTab("时间和日期", layoutPanel);
tabPanel.addTab("时区", timezonePanel);
tabPanel.addTab("Internet 时间", internetTimePanel);
tabPanel.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
tabPanel.setBackground(SystemColor.controlHighlight);
timezonePanel.add(timezoneBox);
layoutPanel.setLayout(null);
layoutPanel.setSize(300, 200);
// add by lee
// this.dateCalendarPanel.addMouseListener(this);
layoutPanel.add(dateCalendarPanel);
dateCalendarPanel.setBounds(new Rectangle(20, 38, 175, 145));
timezonePanel.setLocation(40, 40);
this.setLocation(300, 100);
this.getContentPane().add(tabPanel, BorderLayout.CENTER);
this.getContentPane().add(pn3, BorderLayout.SOUTH);
pn3.setLayout(new FlowLayout(FlowLayout.RIGHT, 10, 3));
pn3.add(confirmBtn);
pn3.add(cancelBtn);
pn3.add(applyBtn);
// 所有组件都加上判断时间是更新时间的标志
pn3.addMouseListener(this);
timezonePanel.addMouseListener(this);
dateCalendarPanel.addMouseListener(this);
clockPanel.addMouseListener(this);
applyBtn.setEnabled(false);
confirmBtn.addActionListener(this);
cancelBtn.addActionListener(this);
applyBtn.addActionListener(this);
this.setSize(410, 330);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
clockPanel.setBounds(new Rectangle(225, 30, 160, 180));
clockPanel.setBackground(layoutPanel.getBackground());
layoutPanel.add(clockPanel);
clockPanel.start();
timezoneBox.setBounds(new Rectangle(15, 15, 370, 20));
timezoneBox.addItem("GMT+8:00");
timezoneBox.addItem("GMT+9:00");
timezoneBox.addItem("GMT+10:00");
timezoneBox.addItem("GMT+11:00");
timezoneBox.addItem("GMT+12:00");
timezoneBox.addItem("GMT+13:00");
timezoneBox.addItem("GMT-12:00");
timezoneBox.addItem("GMT-11:00");
timezoneBox.addItem("GMT-10:00");
timezoneBox.addItem("GMT-9:00");
timezoneBox.addItem("GMT-8:00");
timezoneBox.addItem("GMT-7:00");
timezoneBox.addItem("GMT-6:00");
timezoneBox.addItem("GMT-5:00");
timezoneBox.addItem("GMT-4:00");
timezoneBox.addItem("GMT-3:00");
timezoneBox.addItem("GMT-2:00");
timezoneBox.addItem("GMT-1:00");
timezoneBox.addItem("GMT+1:00");
timezoneBox.addItem("GMT+2:00");
timezoneBox.addItem("GMT+3:00");
timezoneBox.addItem("GMT+4:00");
timezoneBox.addItem("GMT+5:00");
timezoneBox.addItem("GMT+6:00");
timezoneBox.addItem("GMT+7:00");
timezoneBox.addItemListener(this);
timezonePanel.setLayout(null);
timezonePanel.setBackground(SystemColor.controlLtHighlight);
this.setVisible(true);
}
public void paintComponent(Graphics g)
{
;
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == cancelBtn)
System.exit(0);
if (e.getSource() == this.confirmBtn)
{
WindowsTime.hasModified = true;
today = dateCalendarPanel.calendar;
this.setTodayTimeZone();
internetTimePanel.setSystemTime(today);
System.exit(0);
// repaint();
}
if (e.getSource() == this.applyBtn)
{
WindowsTime.hasModified = false;
today = dateCalendarPanel.calendar;
this.setTodayTimeZone();
internetTimePanel.setSystemTime(today);
this.applyBtn.setEnabled(false);
}
}
private void setTodayTimeZone()
{
int i = timezoneBox.getSelectedIndex();
TimeZone tz = TimeZone.getDefault();
// int row=tz.getRawOffset();
// JOptionPane.showConfirmDialog(this, ""+row/(3600*1000));
if (i < 6)
i = i + 8;
else if (i > 5 && i < 18)
i = i - 18;
else
i = i - 17;
tz.setRawOffset(i * 3600 * 1000);
// JOptionPane.showConfirmDialog(this, ""+i);
today.setTimeZone(tz);
}
public void itemStateChanged(ItemEvent e)
{
if (e.getSource() == timezoneBox)
{
int i = timezoneBox.getSelectedIndex() * 15;
timezonePanel.reset(i);
WindowsTime.hasModified = true;
}
}
private void setApply()
{
if (WindowsTime.hasModified == true)
this.applyBtn.setEnabled(true);
else
this.applyBtn.setEnabled(false);
}
public void mouseClicked(MouseEvent e)
{
this.setApply();
}
public void mouseEntered(MouseEvent e)
{
this.setApply();
}
public void mouseExited(MouseEvent e)
{
this.setApply();
}
public void mousePressed(MouseEvent e)
{
this.setApply();
}
public void mouseReleased(MouseEvent e)
{
this.setApply();
}
public void adjustmentValueChanged(AdjustmentEvent e)
{
;
}
public static void main(String argv[])
{
try
{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch (ClassNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (InstantiationException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IllegalAccessException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (UnsupportedLookAndFeelException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
new DateAndTime();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -