该怎样写一个钟表的GUI?就是像系统时间里面的那个,每一秒秒针转动,每1min分针转动一下,1hour时针转一下
这样的GUI会不会很复杂?用Java自带的gui的类可不可以?
这样的GUI会不会很复杂?用Java自带的gui的类可不可以?
解决方案 »
- 实习面试题
- java出发时间一定比到达时间早(不要错误提示,只是符合出发时间一定比到达时间早)
- 阿里巴巴武汉笔试题
- 如何用 UltraEdit 集成编译运行JAVA代码 (在哪里配置脚本),在线给分100
- JavaFX 各种内建built-in布局管理器的使用
- 安装, 出错, 问题, 着急 ,解决, 100分, 容易, 你的, 谢谢!
- 可控直线类
- 怎么才能让java实现代理服务器及用户验证,是http协议.
- 如何把Date变成byte[](String也行),然后又能把byte[]变回一个Date??? 求救!!!
- JSONArray.fromObject(list)的问题
- oracle sql 这个算不算依赖关系
- 关于这个问题,我怎么关闭原Password窗口呢
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.*;
import java.util.Calendar;
import java.util.GregorianCalendar;@SuppressWarnings("serial")
class Clock extends JFrame implements ActionListener { int x, y, x0, y0, r, h, olds_x, olds_y, oldm_x, oldm_y, oldh_x, oldh_y, ss,
mm, hh, old_m, old_h, ang;
final double RAD = Math.PI / 180; //构造函数创建一个窗体
public Clock() {
super("Java时钟");
setDefaultCloseOperation(3);
setSize(200, 200);
setBackground(Color.white);
setLocation(300, 150);
setResizable(false);
setVisible(true);
int delay = 1000;
//创建一个监听事件
ActionListener drawClock = new ActionListener() {
/* (非 Javadoc)
* @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
*/
public void actionPerformed(ActionEvent evt) {
repaint();
}
};
//创建一个时间计数器
new Timer(delay, drawClock).start();
} //实现ActionListener接口必须实现的方法
public void actionPerformed(ActionEvent e) {
} //绘制图形
public void paint(Graphics g) {
Graphics2D g2D = (Graphics2D) g;
Insets insets = getInsets();
int L = insets.left / 2, T = insets.top / 2;
h = getSize().height;
g.setColor(Color.BLACK);
//画圆
g2D.setStroke(new BasicStroke(4.0f));
g.drawOval(L + 40, T + 40, h - 80, h - 80);
r = h / 2 - 40;
x0 = 40 + r - 5 + L;
y0 = 40 + r - 5 - T;
ang = 60;
//绘制时钟上的12个汉字
for (int i = 1; i <= 12; i++) {
x = (int) ((r + 10) * Math.cos(RAD * ang) + x0);
y = (int) ((r + 10) * Math.sin(RAD * ang) + y0);
g.drawString("" + i, x, h - y);
ang -= 30;
}
//获得现在时间
Calendar now = new GregorianCalendar();
int nowh = now.get(Calendar.HOUR_OF_DAY);
int nowm = now.get(Calendar.MINUTE);
int nows = now.get(Calendar.SECOND);
String st;
if (nowh < 10)
st = "0" + nowh;
else
st = "" + nowh;
if (nowm < 10)
st += ":0" + nowm;
else
st += ":" + nowm;
if (nows < 10)
st += ":0" + nows;
else
st += ":" + nows;
//在窗体上显示时间
g.setColor(Color.pink);
g.fillRect(L, T, 50, 28);
g.setColor(Color.blue);
g.drawString(st, L + 2, T + 26);
//计算时间与度数的关系
ss = 90 - nows * 6;
mm = 90 - nowm * 6;
hh = 90 - nowh * 30 - nowm / 2;
x0 = r + 40 + L;
y0 = r + 40 + T;
g2D.setStroke(new BasicStroke(1.2f));
//擦除秒针
if (olds_x > 0) {
g.setColor(getBackground());
g.drawLine(x0, y0, olds_x, h - olds_y);
} else {
old_m = mm;
old_h = hh;
}
//绘制秒针
x = (int) (r * 0.9 * Math.cos(RAD * ss)) + x0;
y = (int) (r * 0.9 * Math.sin(RAD * ss)) + y0 - 2 * T;
g.setColor(Color.yellow);
g.drawLine(x0, y0, x, h - y);
olds_x = x;
olds_y = y;
g2D.setStroke(new BasicStroke(2.2f));
//擦除分针
if (old_m != mm) {
g.setColor(getBackground());
g.drawLine(x0, y0, oldm_x, h - oldm_y);
}
//绘制分针
x = (int) (r * 0.7 * Math.cos(RAD * mm)) + x0;
y = (int) (r * 0.7 * Math.sin(RAD * mm)) + y0 - 2 * T;
g.setColor(Color.green);
g.drawLine(x0, y0, x, h - y);
oldm_x = x;
oldm_y = y;
old_m = mm;
g2D.setStroke(new BasicStroke(3.4f));
//擦除时针
if (old_h != hh) {
g.setColor(getBackground());
g.drawLine(x0, y0, oldh_x, h - oldh_y);
}
//绘制时针
x = (int) (r * 0.5 * Math.cos(RAD * hh)) + x0;
y = (int) (r * 0.5 * Math.sin(RAD * hh)) + y0 - 2 * T;
g.setColor(Color.red);
g.drawLine(x0, y0, x, h - y);
oldh_x = x;
oldh_y = y;
old_h = hh;
} public static void main(String[] args) {
@SuppressWarnings("unused")
Clock c = new Clock();
} public void clear(Graphics g, int i, int j, int k, Color background) {
// TODO 自动生成方法存根 }
}
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.text.DecimalFormat;
import java.util.Calendar;/** A simple Clock */
public class Clock extends javax.swing.JComponent {
protected DecimalFormat tflz, tf; protected boolean done = false; public Clock() {
new Thread(new Runnable() {
public void run() {
while (!done) {
Clock.this.repaint(); // request a redraw
try {
Thread.sleep(1000);
} catch (InterruptedException e) { /* do nothing */
}
}
}
}).start();
tf = new DecimalFormat("#0");
tflz = new DecimalFormat("00");
} public void stop() {
done = true;
} /* paint() - get current time and draw (centered) in Component. */
public void paint(Graphics g) {
Calendar myCal = Calendar.getInstance();
StringBuffer sb = new StringBuffer();
sb.append(tf.format(myCal.get(Calendar.HOUR)));
sb.append(':');
sb.append(tflz.format(myCal.get(Calendar.MINUTE)));
sb.append(':');
sb.append(tflz.format(myCal.get(Calendar.SECOND)));
String s = sb.toString();
FontMetrics fm = getFontMetrics(getFont());
int x = (getSize().width - fm.stringWidth(s)) / 2;
// System.out.println("Size is " + getSize());
g.drawString(s, x, 10);
} public Dimension getPreferredSize() {
return new Dimension(100, 30);
} public Dimension getMinimumSize() {
return new Dimension(50, 10);
}
}
netbeans编译的结果:
init:
deps-jar:
Warning: clock\ClockApp.java modified in the future.
Compiling 3 source files to E:\code\clock\clock\build\classes
E:\code\clock\clock\src\clock\ClockApp.java:8: 类 Clock 是公共的,应在名为 Clock.java 的文件中声明
public class Clock extends javax.swing.JComponent {
E:\code\clock\clock\src\clock\ClockAboutBox.java:41: 找不到符号
符号: 类 ClockApp
位置: 软件包 clock
org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(clock.ClockApp.class).getContext().getResourceMap(ClockAboutBox.class);
E:\code\clock\clock\src\clock\ClockAboutBox.java:47: 找不到符号
符号: 类 ClockApp
位置: 软件包 clock
javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(clock.ClockApp.class).getContext().getActionMap(ClockAboutBox.class, this);
E:\code\clock\clock\src\clock\ClockView.java:87: 找不到符号
符号: 变量 ClockApp
位置: 类 clock.ClockView
JFrame mainFrame = ClockApp.getApplication().getMainFrame();
E:\code\clock\clock\src\clock\ClockView.java:91: 找不到符号
符号: 变量 ClockApp
位置: 类 clock.ClockView
ClockApp.getApplication().show(aboutBox);
E:\code\clock\clock\src\clock\ClockView.java:130: 找不到符号
符号: 类 ClockApp
位置: 软件包 clock
org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(clock.ClockApp.class).getContext().getResourceMap(ClockView.class);
E:\code\clock\clock\src\clock\ClockView.java:134: 找不到符号
符号: 类 ClockApp
位置: 软件包 clock
javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(clock.ClockApp.class).getContext().getActionMap(ClockView.class, this);
7 错误
生成失败(总时间:0 秒)