该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
代码如下,帮忙看一下这个时间怎么每秒刷新一次。。。。
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.awt.*;
import javax.swing.*;
public class xin extends JFrame implements ActionListener, Runnable{
private JFrame Main=new JFrame("万年历");
private int MyYear, MyMonth,MyWeek,days,MyHour,MyMinutes,MySecond; //用于记录起始年份、月份,计算第一天为星期几,一共有多少天
private String s; // 用于年份、月份的数据类型转换
ButtonGroup group;//选择国家的单选按钮
JRadioButton radio1,radio2,radio3,radio4,radio5,radio6;//选择国家的选项
private JTextField JTMonth=new JTextField(10); //月份显示文本框
private JTextField JTYear=new JTextField(10); //年份显示文本框
private JTextField JTHour=new JTextField(2); //小时显示文本框
private JTextField JTMinutes=new JTextField(2); //分钟显示文本框
private JTextField JTSecond=new JTextField(2); //秒显示文本框
private JButton JBAddYear=new JButton(">>"); //增加年份按钮
private JButton JBSubYear=new JButton("<
private JButton JBAddMonth=new JButton(">"); //增加月份按钮
private JButton JBSubMonth=new JButton("
private Calendar a; //日历功能
private GridLayout MyGridLayout = new GridLayout(2, 1, 0, 5); //设计容器
private JPanel row1 = new JPanel();
private JPanel row2 = new JPanel();
private JLabel[] numbers = new JLabel[42];
//private JLabel MyJLabel1=new JLabel("设计:王 时间:2014年12月");
private JLabel MyJLabel2=new JLabel("日 一 二 三 四 五 六");
private JLabel MyJLabel3=new JLabel("年");
private JLabel MyJLabel4=new JLabel("月");
private JLabel MyJLabel5=new JLabel("当前时间:");
private JLabel MyJLabel6=new JLabel("时");
private JLabel MyJLabel7=new JLabel("分");
private JLabel MyJLabel8=new JLabel("秒");
private JLabel MyJLabel9=new JLabel("请选择时区:");
public xin(){
//设置字体
MyJLabel2.setFont(new Font("宋体", Font.PLAIN, 18));
JTYear.setFont(new Font("宋体", Font.PLAIN, 18));
JTMonth.setFont(new Font("宋体", Font.PLAIN, 18));
JTHour.setFont(new Font("宋体", Font.PLAIN, 25));
JTMinutes.setFont(new Font("宋体", Font.PLAIN, 25));
JTSecond.setFont(new Font("宋体", Font.PLAIN, 25));
JTYear.setEnabled(false); //禁止编辑
JTMonth.setEnabled(false); //禁止编辑
JTHour.setEnabled(false); //禁止编辑
JTMinutes.setEnabled(false); //禁止编辑
JTSecond.setEnabled(false); //禁止编辑
JTYear.setBackground(new Color(110,100,114)); //设置颜色
JTMonth.setBackground(new Color(110,100,114));
JTHour.setBackground(new Color(110,100,114));
JTMinutes.setBackground(new Color(110,100,114));
JTSecond.setBackground(new Color(110,100,114));
JTYear.setHorizontalAlignment(JTextField.RIGHT); //设置对齐方式
JTMonth.setHorizontalAlignment(JTextField.RIGHT); //设置对齐方式
JTHour.setHorizontalAlignment(JTextField.RIGHT); //设置对齐方式
JTMinutes.setHorizontalAlignment(JTextField.RIGHT); //设置对齐方式
JTSecond.setHorizontalAlignment(JTextField.RIGHT); //设置对齐方式
//添加监视器
JBAddYear.addActionListener(this);
JBSubYear.addActionListener(this);
JBAddMonth.addActionListener(this);
JBSubMonth.addActionListener(this);
//以下为初始化界面
a=Calendar.getInstance();// 获取当前日历
MyMinutes=a.get(Calendar.MINUTE);//得到当前时间的分钟
MySecond=a.get(Calendar.SECOND);//得到当前时间的秒钟
MyHour=a.get(Calendar.HOUR_OF_DAY);//得到当前时间的小时
MyYear=a.get(Calendar.YEAR); // 得到当前时间的年份
MyMonth=a.get(Calendar.MONTH);// 得到当前时间的月份
s = Integer.toString(MyYear); // 将年份转换为字符串类型
JTYear.setText(s); // 输出
//计数从0开始,所加1后才是要正常显示的数字
s = Integer.toString(MyMonth+1); // 将月份转换为字符串类型
JTMonth.setText(s); // 输出月份
s = Integer.toString(MyHour); // 将小时转换为字符串类型
JTHour.setText(s); // 输出小时
s = Integer.toString(MyMinutes); // 将分钟转换为字符串类型
JTMinutes.setText(s); // 输出分钟
s = Integer.toString(MySecond); // 将秒钟转换为字符串类型
JTSecond.setText(s); // 输出秒钟
Main.setSize(280,500); //设置窗口大小
Main.setResizable(true); // 尺寸可调
Main.setLocationRelativeTo(null); //设置窗口居中显示
Main.setVisible(true); //显示窗口
Main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //设置用户关闭窗口时的默认处理
Main.setLayout(MyGridLayout);
FlowLayout flowLayout1 = new FlowLayout(FlowLayout.CENTER , 10 , 10);
row1.setLayout(flowLayout1);
//row1.add(MyJLabel1); //在窗口中添加标签组件
row1.add(JTYear);
row1.add(MyJLabel3);
row1.add(JTMonth);
row1.add(MyJLabel4);
row1.add(JBSubYear);
row1.add(JBAddYear);
row1.add(JBSubMonth);
row1.add(JBAddMonth);
row1.add(MyJLabel5);
row1.add(JTHour);
row1.add(MyJLabel6);
row1.add(JTMinutes);
row1.add(MyJLabel7);
row1.add(JTSecond);
row1.add(MyJLabel8);
row1.add(MyJLabel9);
group = new ButtonGroup();
radio1 = new JRadioButton("北京");
radio2 = new JRadioButton("纽约");
radio3 = new JRadioButton("伦敦");
radio4 = new JRadioButton("巴黎");
radio5 = new JRadioButton("东京");
radio6 = new JRadioButton("罗马");
group.add(radio1);
group.add(radio2);
group.add(radio3);
group.add(radio4);
group.add(radio5);
group.add(radio6);
row1.add(radio1);
row1.add(radio2);
row1.add(radio3);
row1.add(radio4);
row1.add(radio5);
row1.add(radio6);
Main.add(row1);
row1.add(MyJLabel2);
GridLayout gridLayout1 = new GridLayout(0, 7, 2, 2);
row2.setLayout(gridLayout1);
for (int i = 0; i < 42; i++) { // 使用循环语句,快速建立日期显示组件
numbers[i] = new JLabel();
numbers[i].setHorizontalAlignment(JLabel.CENTER);
row2.add(numbers[i]);
}
Main.add(row2);
change();
}
public static void main(String[] args) {
new xin();
new Thread().start();//启动程序线程
}
public void actionPerformed(ActionEvent e) {
Object obj;
obj = e.getSource(); // 系统传递来的事件代码
if (obj == JBAddYear) {
MyYear=MyYear+1;
s = Integer.toString(MyYear); // 将年份转换为字符串类型
JTYear.setText(s); // 输出年份
change();
}else if (obj == JBSubYear) {
MyYear=MyYear-1;
s = Integer.toString(MyYear); // 将年份转换为字符串类型
JTYear.setText(s); // 输出年份
change();
}else if (obj == JBAddMonth) {
MyMonth=MyMonth+1;
if (MyMonth>11){ // 月份起始数为零
MyMonth=0;
}
s = Integer.toString(MyMonth+1); // 将月份转换为字符串类型
JTMonth.setText(s); // 输出月份
change();
}else if (obj == JBSubMonth) {
MyMonth=MyMonth-1;
if (MyMonth<0){ // 月份起始数为零
MyMonth=11;
}
s = Integer.toString(MyMonth+1); // 将月份转换为字符串类型
JTMonth.setText(s); // 输出月份
change();
}
}
void change() {
a.set(MyYear, MyMonth, 1);
MyWeek = a.get(Calendar.DAY_OF_WEEK);// 得到本月第一天是本周星期几天,以零起步。
MyWeek=MyWeek-1;
int y=1; // 用来计算并显示日期
days = a.getActualMaximum(Calendar.DAY_OF_MONTH);// 获取本月天数
for (int i = 0; i < 42; i++) {
if (i
numbers[i].setText(""); // 该天显示为空
}
else if
((i-MyWeek+1)>days){ //本月后一天过后,当周其余天数显示为空
numbers[i].setText(""); // 这一周剩余天数的位置显示为空
}else{
s = Integer.toString(y); // 将日期转换为字符串类型
numbers[i].setText(s); //在日历上显示日期
y=y+1;
}
}
}
public void run() {
// TODO Auto-generated method stub
while (true) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
repaint();
}
}
}