界面什么的都搞定了,只差时区相应事件那部分了,确实不会啊。
求助,明天上午要交了。
程序如下:
import javax.swing.*;import javax.swing.Timer;import java.awt.*;
import java.awt.event.*;
import java.text.DateFormat;
import java.util.*;// 静止时钟类
class StillClock extends JPanel {
private int hour;
private int minute;
private int second;
private boolean hourHandVisible = true;
private boolean minuteHandVisible = true;
private boolean secondHandVisible = true;
// 定义无参构造函数
public StillClock() {
setCurrentTime();
}
// 构建带参数的时钟
public StillClock(int hour, int minute, int second) {
this.hour = hour;
this.minute = minute;
this.second = second;
}
// 画出时钟
protected void paintComponent(Graphics g) {
super.paintComponent(g);
// 初始化时钟参数
int clockRadius =
(int)(Math.min(getWidth(), getHeight()) * 0.4);
final int xCenter = getWidth() / 2;
final int yCenter = getHeight() / 2;
// 画出一个圆
g.setColor(Color.black);
g.drawOval(xCenter - clockRadius, yCenter - clockRadius,
2 * clockRadius, 2 * clockRadius);
// 设置秒针
if (secondHandVisible) {
int sLength = (int)(clockRadius * 0.7);
int xSecond =
(int)(xCenter + sLength * Math.sin(second * (2 * Math.PI / 60)));
int ySecond =
(int)(yCenter - sLength * Math.cos(second * (2 * Math.PI / 60)));
g.setColor(Color.red);
g.drawLine(xCenter, yCenter, xSecond, ySecond);
}
// 设置分针
if (minuteHandVisible) {
int mLength = (int)(clockRadius * 0.6);
int xMinute =
(int)(xCenter + mLength * Math.sin(minute * (2 * Math.PI / 60)));
int yMinute =
(int)(yCenter - mLength * Math.cos(minute * (2 * Math.PI / 60)));
g.setColor(Color.blue);
g.drawLine(xCenter, yCenter, xMinute, yMinute);
}
// 设置时针
if (hourHandVisible) {
int hLength = (int)(clockRadius * 0.5);
int xHour = (int)(xCenter +
hLength *
Math.sin((hour + minute / 60.0) * (2 * Math.PI / 12)));
int yHour = (int)(yCenter -
hLength *
Math.cos((hour + minute / 60.0) * (2 * Math.PI / 12)));
g.setColor(Color.black);
g.drawLine(xCenter, yCenter, xHour, yHour);
}
// 绘制时钟的刻度
for (int i = 0; i < 60; i++) {
double percent;
if (i % 5 == 0) {
percent = 0.9;
}
else {
percent = 0.95;
}
int xOuter = (int)(xCenter +
clockRadius * Math.sin(i * (2 * Math.PI / 60)));
int yOuter = (int)(yCenter -
clockRadius * Math.cos(i * (2 * Math.PI / 60)));
int xInner = (int)(xCenter +
percent * clockRadius * Math.sin(i * (2 * Math.PI / 60)));
int yInner = (int)(yCenter -
percent * clockRadius * Math.cos(i * (2 * Math.PI / 60)));
g.drawLine(xOuter, yOuter, xInner, yInner);
}
// 显示时间
g.setColor(Color.black);
for (int i = 0; i < 12; i++) {
int x = (int)(xCenter +
0.8 * clockRadius * Math.sin(i * (2 * Math.PI / 12)));
int y = (int)(yCenter -
0.8 * clockRadius * Math.cos(i * (2 * Math.PI / 12)));
g.drawString("" + ((i == 0) ? 12 : i), x, y);
}
}
// 设置现在的时间
public void setCurrentTime() {
int x=8;
long totalSeconds = System.currentTimeMillis() / 1000;
int currentSecond = (int)(totalSeconds % 60);
long totalMinutes = totalSeconds / 60;
int currentMinute = (int)(totalMinutes % 60);
long totalHours = totalMinutes / 60;
int currentHour = (int)((totalHours+x) % 24);
this.hour = currentHour;
this.minute = currentMinute;
this.second = currentSecond;
}
public boolean isHourHandVisible() {
return hourHandVisible;
}
public boolean isMinuteHandVisible() {
return hourHandVisible;
}
public boolean isSecondHandVisible() {
return secondHandVisible;
}
public void setHourHandVisible(boolean hourHandVisible) {
this.hourHandVisible = hourHandVisible;
repaint();
}
public void setMinuteHandVisible(boolean minuteHandVisible) {
this.minuteHandVisible = minuteHandVisible;
repaint();
}
public void setSecondHandVisible(boolean secondHandVisible) {
this.secondHandVisible = secondHandVisible;
repaint();
}
}
public class Clock extends JFrame {
private GregorianCalendar now = new GregorianCalendar();
private JButton jbtStart = new JButton("Start");
private JButton jbtStop = new JButton("Stop");
private JLabel weekLabel = new JLabel();
private JLabel dateLabel = new JLabel();
private JLabel timeLabel = new JLabel();
String[] s = { " - 东一区 - ", " - 东二区 - ",
" - 东三区 - ", " - 东四区 - "," - 东五区 - ",
" - 东六区 - ", " - 东七区 - ", " - 东八区 - " };
private JComboBox jcb =new JComboBox(s);
private ClockAnimation clock = new ClockAnimation();
public Clock() {
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
JPanel panel3 = new JPanel();
JPanel panel4 = new JPanel();
dateLabel.setSize(20,20);
dateLabel.setText(now.get(Calendar.YEAR) + "年"+
(now.get(Calendar.MONTH)+1) + "月" + now.get(Calendar.DATE) + "日" );
timeLabel.setSize(20, 20);
weekLabel.setSize(20, 20);
int week = now.get(Calendar.DAY_OF_WEEK);
switch (week) {
case 1:
weekLabel.setText("SUNDAY");
break;
case 2:
weekLabel.setText("MONDAY");
break;
case 3:
weekLabel.setText("TUESDAY");
break;
case 4:
weekLabel.setText("WEDNESDAY");
break;
case 5:
weekLabel.setText("THURSDAY");
break;
case 6:
weekLabel.setText("FRIDAY");
break;
case 7:
weekLabel.setText("SATURDAY");
break;
}
panel1.add(jbtStart);// 添加开始键
panel1.add(jbtStop);// 添加停止键
panel2.add(jcb);
panel3.add(dateLabel);
panel3.add(weekLabel);
panel3.add(timeLabel);
panel4.add(panel1,BorderLayout.SOUTH);
panel4.add(panel2,BorderLayout.NORTH);
this.add(clock, BorderLayout.CENTER); // 设置圆在中心
this.add(panel4,BorderLayout.NORTH);
this.add(panel3,BorderLayout.SOUTH);
// 添加开始键监听器
jbtStart.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
clock.start();
}
});
// 添加停止键监听器
jbtStop.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
clock.stop();
}
});
}
// 主函数
public static void main(String[] args) {
JFrame frame = new Clock();
frame.setTitle("Clock");
frame.setSize(500, 500);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
class ClockAnimation extends StillClock {
Timer timer = new Timer(1000, new TimerListener());
public ClockAnimation() {
timer.start();
}
public void start() {
timer.start();
}
public void stop() {
timer.stop();
} // 操作action事件
class TimerListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
// 设置时间并绘制时钟输出现在的时间
setCurrentTime();
repaint();
}
}
}
}
求助,明天上午要交了。
程序如下:
import javax.swing.*;import javax.swing.Timer;import java.awt.*;
import java.awt.event.*;
import java.text.DateFormat;
import java.util.*;// 静止时钟类
class StillClock extends JPanel {
private int hour;
private int minute;
private int second;
private boolean hourHandVisible = true;
private boolean minuteHandVisible = true;
private boolean secondHandVisible = true;
// 定义无参构造函数
public StillClock() {
setCurrentTime();
}
// 构建带参数的时钟
public StillClock(int hour, int minute, int second) {
this.hour = hour;
this.minute = minute;
this.second = second;
}
// 画出时钟
protected void paintComponent(Graphics g) {
super.paintComponent(g);
// 初始化时钟参数
int clockRadius =
(int)(Math.min(getWidth(), getHeight()) * 0.4);
final int xCenter = getWidth() / 2;
final int yCenter = getHeight() / 2;
// 画出一个圆
g.setColor(Color.black);
g.drawOval(xCenter - clockRadius, yCenter - clockRadius,
2 * clockRadius, 2 * clockRadius);
// 设置秒针
if (secondHandVisible) {
int sLength = (int)(clockRadius * 0.7);
int xSecond =
(int)(xCenter + sLength * Math.sin(second * (2 * Math.PI / 60)));
int ySecond =
(int)(yCenter - sLength * Math.cos(second * (2 * Math.PI / 60)));
g.setColor(Color.red);
g.drawLine(xCenter, yCenter, xSecond, ySecond);
}
// 设置分针
if (minuteHandVisible) {
int mLength = (int)(clockRadius * 0.6);
int xMinute =
(int)(xCenter + mLength * Math.sin(minute * (2 * Math.PI / 60)));
int yMinute =
(int)(yCenter - mLength * Math.cos(minute * (2 * Math.PI / 60)));
g.setColor(Color.blue);
g.drawLine(xCenter, yCenter, xMinute, yMinute);
}
// 设置时针
if (hourHandVisible) {
int hLength = (int)(clockRadius * 0.5);
int xHour = (int)(xCenter +
hLength *
Math.sin((hour + minute / 60.0) * (2 * Math.PI / 12)));
int yHour = (int)(yCenter -
hLength *
Math.cos((hour + minute / 60.0) * (2 * Math.PI / 12)));
g.setColor(Color.black);
g.drawLine(xCenter, yCenter, xHour, yHour);
}
// 绘制时钟的刻度
for (int i = 0; i < 60; i++) {
double percent;
if (i % 5 == 0) {
percent = 0.9;
}
else {
percent = 0.95;
}
int xOuter = (int)(xCenter +
clockRadius * Math.sin(i * (2 * Math.PI / 60)));
int yOuter = (int)(yCenter -
clockRadius * Math.cos(i * (2 * Math.PI / 60)));
int xInner = (int)(xCenter +
percent * clockRadius * Math.sin(i * (2 * Math.PI / 60)));
int yInner = (int)(yCenter -
percent * clockRadius * Math.cos(i * (2 * Math.PI / 60)));
g.drawLine(xOuter, yOuter, xInner, yInner);
}
// 显示时间
g.setColor(Color.black);
for (int i = 0; i < 12; i++) {
int x = (int)(xCenter +
0.8 * clockRadius * Math.sin(i * (2 * Math.PI / 12)));
int y = (int)(yCenter -
0.8 * clockRadius * Math.cos(i * (2 * Math.PI / 12)));
g.drawString("" + ((i == 0) ? 12 : i), x, y);
}
}
// 设置现在的时间
public void setCurrentTime() {
int x=8;
long totalSeconds = System.currentTimeMillis() / 1000;
int currentSecond = (int)(totalSeconds % 60);
long totalMinutes = totalSeconds / 60;
int currentMinute = (int)(totalMinutes % 60);
long totalHours = totalMinutes / 60;
int currentHour = (int)((totalHours+x) % 24);
this.hour = currentHour;
this.minute = currentMinute;
this.second = currentSecond;
}
public boolean isHourHandVisible() {
return hourHandVisible;
}
public boolean isMinuteHandVisible() {
return hourHandVisible;
}
public boolean isSecondHandVisible() {
return secondHandVisible;
}
public void setHourHandVisible(boolean hourHandVisible) {
this.hourHandVisible = hourHandVisible;
repaint();
}
public void setMinuteHandVisible(boolean minuteHandVisible) {
this.minuteHandVisible = minuteHandVisible;
repaint();
}
public void setSecondHandVisible(boolean secondHandVisible) {
this.secondHandVisible = secondHandVisible;
repaint();
}
}
public class Clock extends JFrame {
private GregorianCalendar now = new GregorianCalendar();
private JButton jbtStart = new JButton("Start");
private JButton jbtStop = new JButton("Stop");
private JLabel weekLabel = new JLabel();
private JLabel dateLabel = new JLabel();
private JLabel timeLabel = new JLabel();
String[] s = { " - 东一区 - ", " - 东二区 - ",
" - 东三区 - ", " - 东四区 - "," - 东五区 - ",
" - 东六区 - ", " - 东七区 - ", " - 东八区 - " };
private JComboBox jcb =new JComboBox(s);
private ClockAnimation clock = new ClockAnimation();
public Clock() {
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
JPanel panel3 = new JPanel();
JPanel panel4 = new JPanel();
dateLabel.setSize(20,20);
dateLabel.setText(now.get(Calendar.YEAR) + "年"+
(now.get(Calendar.MONTH)+1) + "月" + now.get(Calendar.DATE) + "日" );
timeLabel.setSize(20, 20);
weekLabel.setSize(20, 20);
int week = now.get(Calendar.DAY_OF_WEEK);
switch (week) {
case 1:
weekLabel.setText("SUNDAY");
break;
case 2:
weekLabel.setText("MONDAY");
break;
case 3:
weekLabel.setText("TUESDAY");
break;
case 4:
weekLabel.setText("WEDNESDAY");
break;
case 5:
weekLabel.setText("THURSDAY");
break;
case 6:
weekLabel.setText("FRIDAY");
break;
case 7:
weekLabel.setText("SATURDAY");
break;
}
panel1.add(jbtStart);// 添加开始键
panel1.add(jbtStop);// 添加停止键
panel2.add(jcb);
panel3.add(dateLabel);
panel3.add(weekLabel);
panel3.add(timeLabel);
panel4.add(panel1,BorderLayout.SOUTH);
panel4.add(panel2,BorderLayout.NORTH);
this.add(clock, BorderLayout.CENTER); // 设置圆在中心
this.add(panel4,BorderLayout.NORTH);
this.add(panel3,BorderLayout.SOUTH);
// 添加开始键监听器
jbtStart.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
clock.start();
}
});
// 添加停止键监听器
jbtStop.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
clock.stop();
}
});
}
// 主函数
public static void main(String[] args) {
JFrame frame = new Clock();
frame.setTitle("Clock");
frame.setSize(500, 500);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
class ClockAnimation extends StillClock {
Timer timer = new Timer(1000, new TimerListener());
public ClockAnimation() {
timer.start();
}
public void start() {
timer.start();
}
public void stop() {
timer.stop();
} // 操作action事件
class TimerListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
// 设置时间并绘制时钟输出现在的时间
setCurrentTime();
repaint();
}
}
}
}
package net.csdn;import javax.swing.*;import javax.swing.Timer;import java.awt.*;
import java.awt.event.*;
import java.text.DateFormat;
import java.util.*;// 静止时钟类
class StillClock extends JPanel {
private int hour; private int minute; private int second; private boolean hourHandVisible = true; private boolean minuteHandVisible = true; private boolean secondHandVisible = true; // 定义无参构造函数
public StillClock() {
setCurrentTime(7);
} // 构建带参数的时钟
public StillClock(int hour, int minute, int second) {
this.hour = hour;
this.minute = minute;
this.second = second;
} // 画出时钟
protected void paintComponent(Graphics g) {
super.paintComponent(g);
// 初始化时钟参数
int clockRadius = (int) (Math.min(getWidth(), getHeight()) * 0.4);
final int xCenter = getWidth() / 2;
final int yCenter = getHeight() / 2;
// 画出一个圆
g.setColor(Color.black);
g.drawOval(xCenter - clockRadius, yCenter - clockRadius, 2 * clockRadius, 2 * clockRadius);
// 设置秒针
if (secondHandVisible) {
int sLength = (int) (clockRadius * 0.7);
int xSecond = (int) (xCenter + sLength * Math.sin(second * (2 * Math.PI / 60)));
int ySecond = (int) (yCenter - sLength * Math.cos(second * (2 * Math.PI / 60)));
g.setColor(Color.red);
g.drawLine(xCenter, yCenter, xSecond, ySecond);
}
// 设置分针
if (minuteHandVisible) {
int mLength = (int) (clockRadius * 0.6);
int xMinute = (int) (xCenter + mLength * Math.sin(minute * (2 * Math.PI / 60)));
int yMinute = (int) (yCenter - mLength * Math.cos(minute * (2 * Math.PI / 60)));
g.setColor(Color.blue);
g.drawLine(xCenter, yCenter, xMinute, yMinute);
}
// 设置时针
if (hourHandVisible) {
int hLength = (int) (clockRadius * 0.5);
int xHour = (int) (xCenter + hLength * Math.sin((hour + minute / 60.0) * (2 * Math.PI / 12)));
int yHour = (int) (yCenter - hLength * Math.cos((hour + minute / 60.0) * (2 * Math.PI / 12)));
g.setColor(Color.black);
g.drawLine(xCenter, yCenter, xHour, yHour);
}
// 绘制时钟的刻度
for (int i = 0; i < 60; i++) {
double percent;
if (i % 5 == 0) {
percent = 0.9;
} else {
percent = 0.95;
}
int xOuter = (int) (xCenter + clockRadius * Math.sin(i * (2 * Math.PI / 60)));
int yOuter = (int) (yCenter - clockRadius * Math.cos(i * (2 * Math.PI / 60)));
int xInner = (int) (xCenter + percent * clockRadius * Math.sin(i * (2 * Math.PI / 60)));
int yInner = (int) (yCenter - percent * clockRadius * Math.cos(i * (2 * Math.PI / 60)));
g.drawLine(xOuter, yOuter, xInner, yInner);
}
// 显示时间
g.setColor(Color.black);
for (int i = 0; i < 12; i++) {
int x = (int) (xCenter + 0.8 * clockRadius * Math.sin(i * (2 * Math.PI / 12)));
int y = (int) (yCenter - 0.8 * clockRadius * Math.cos(i * (2 * Math.PI / 12)));
g.drawString("" + ((i == 0) ? 12 : i), x, y);
}
} // 设置现在的时间
public void setCurrentTime(int index) {
int x = 8;
long totalSeconds = System.currentTimeMillis() / 1000;
int currentSecond = (int) (totalSeconds % 60);
long totalMinutes = totalSeconds / 60;
int currentMinute = (int) (totalMinutes % 60);
long totalHours = totalMinutes / 60;
int currentHour = (int) ((totalHours + x) % 24);
this.hour = currentHour + index - 7;
this.minute = currentMinute;
this.second = currentSecond;
} public boolean isHourHandVisible() {
return hourHandVisible;
} public boolean isMinuteHandVisible() {
return hourHandVisible;
} public boolean isSecondHandVisible() {
return secondHandVisible;
} public void setHourHandVisible(boolean hourHandVisible) {
this.hourHandVisible = hourHandVisible;
repaint();
} public void setMinuteHandVisible(boolean minuteHandVisible) {
this.minuteHandVisible = minuteHandVisible;
repaint();
} public void setSecondHandVisible(boolean secondHandVisible) {
this.secondHandVisible = secondHandVisible;
repaint();
}
}public class Clock extends JFrame {
private GregorianCalendar now = new GregorianCalendar(); private JButton jbtStart = new JButton("Start"); private JButton jbtStop = new JButton("Stop"); private JLabel weekLabel = new JLabel(); private JLabel dateLabel = new JLabel(); private JLabel timeLabel = new JLabel(); String[] s = { " - 东一区 - ", " - 东二区 - ", " - 东三区 - ", " - 东四区 - ", " - 东五区 - ", " - 东六区 - ", " - 东七区 - ", " - 东八区 - " }; private JComboBox jcb = new JComboBox(s); private ClockAnimation clock = new ClockAnimation(); public Clock() {
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
JPanel panel3 = new JPanel();
JPanel panel4 = new JPanel();
dateLabel.setSize(20, 20);
dateLabel.setText(now.get(Calendar.YEAR) + "年" + (now.get(Calendar.MONTH) + 1) + "月" + now.get(Calendar.DATE) + "日");
timeLabel.setSize(20, 20);
weekLabel.setSize(20, 20);
jcb.setSelectedIndex(7);
int week = now.get(Calendar.DAY_OF_WEEK);
switch (week) {
case 1:
weekLabel.setText("SUNDAY");
break;
case 2:
weekLabel.setText("MONDAY");
break;
case 3:
weekLabel.setText("TUESDAY");
break;
case 4:
weekLabel.setText("WEDNESDAY");
break;
case 5:
weekLabel.setText("THURSDAY");
break;
case 6:
weekLabel.setText("FRIDAY");
break;
case 7:
weekLabel.setText("SATURDAY");
break;
}
panel1.add(jbtStart);// 添加开始键
panel1.add(jbtStop);// 添加停止键
panel2.add(jcb);
panel3.add(dateLabel);
panel3.add(weekLabel);
panel3.add(timeLabel);
panel4.add(panel1, BorderLayout.SOUTH);
panel4.add(panel2, BorderLayout.NORTH);
this.add(clock, BorderLayout.CENTER); // 设置圆在中心
this.add(panel4, BorderLayout.NORTH);
this.add(panel3, BorderLayout.SOUTH);
// 添加开始键监听器
jbtStart.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
clock.start();
}
});
// 添加停止键监听器
jbtStop.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
clock.stop();
}
});
jcb.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
clock.index = jcb.getSelectedIndex();
}
});
} // 主函数
public static void main(String[] args) { JFrame frame = new Clock();
frame.setTitle("Clock");
frame.setSize(500, 500);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
} class ClockAnimation extends StillClock {
int index = 7;
Timer timer = new Timer(1000, new TimerListener()); public ClockAnimation() {
timer.start();
} public void start() {
timer.start();
} public void stop() {
timer.stop();
} // 操作action事件
class TimerListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
// 设置时间并绘制时钟输出现在的时间
setCurrentTime(index);
repaint();
}
}
}
}
非常感谢~
原来是用ItemListener啊。还有一个问题
now.get(Calendar.MONTH)那里为什么会少了一个月显示是11月的?我只能直接(now.get(Calendar.MONTH)+1)了。