这个程序通过从文本框中输入小时跟分钟 点击“确定”以后,钟表上的指针会自动指向所给时间。。。非常简单@@
程序从Clock类开始运行,开始时时间的初始化为3:30,在一个使用边界布局管理器的框架里面,中间(CENTER)区域加入显示钟表的面板,当输入时间以后,通知面板自动更新。
首先是一个主函数类Clock
package 钟表;
import javax.swing.JFrame;
public class Clock
{
public Clock()
{
}
public static void main(String[] args)
{
//create a new frame
ClockFrame clockframe = new ClockFrame();
clockframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//set size and location
clockframe.setSize(500,400);
clockframe.setLocation(100,100);
//make the frame is visible
clockframe.setVisible(true);
}
}
接下来是一个钟表框架类
package 钟表;
import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.event.*;
public class ClockFrame extends JFrame
{
public ClockFrame()
{
setTitle("Clock");
//set the layout manager
this.setLayout(new BorderLayout());
//add a panel with text fields
JPanel panel=new JPanel();
//create a new time for the clock
final ClockTime time=new ClockTime(3.00,30.00);
//create a label and a jtextfield for hours
panel.add(new JLabel("Hours:"));
final JTextField hourField=new JTextField(Double.toString(time.getHours()),3);
panel.add(hourField);
//create a label and a jtextfield for minutes
panel.add(new JLabel("minutes:"));
final JTextField minuteField=new JTextField(Double.toString(time.getMinutes()),3);
panel.add(minuteField);
//create a new jbutton
JButton btn=new JButton("确定");
panel.add(btn);
this.add(panel,BorderLayout.SOUTH);
final ClockPanel clockpanel=new ClockPanel(time);
this.add(clockpanel,BorderLayout.CENTER);
//deal with the order from the btn(JButton)
btn.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
//set the time:hours and minutes
time.setHours(Double.parseDouble(hourField.getText()));
time.setMinutes(Double.parseDouble(minuteField.getText()));
clockpanel.updateUI(); //update the UI of the clockpanel
}
});
}
}
一个加入到框架中的面板类ClockPanel,这个类用来显示整个钟表,并更新指针的指向
package 钟表;
import javax.swing.*;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Point2D;
import java.awt.Graphics2D;
import java.awt.geom.Line2D;
import java.awt.Dimension;
import java.awt.Color;
import java.awt.Graphics;
public class ClockPanel extends JPanel
{
public ClockPanel(ClockTime a_time)
{
this.time=a_time; //get the time from the clockframe
}
public void paintComponent(Graphics g)
{
super.paintComponent(g); //完成超类的一部分工作
Graphics2D g2=(Graphics2D)g;
g2.setColor(Color.red);
//取得组件的大小
Dimension dimension=this.getSize();
double radius; //钟表圆的半径
double height=dimension.getHeight();
double width=dimension.getWidth();
if(dimension.getWidth()>dimension.getHeight())
{
radius =height/2;
}
else
{
radius=width/2;
}
Point2D.Double point0 =new Point2D.Double(width/2,height/2); //圆心
Ellipse2D ellipse=new Ellipse2D.Double((width-2*radius)/2,(height-2*radius)/2,radius*2,radius*2);
g2.draw(ellipse);
g2.setColor(Color.CYAN);
g2.fill(ellipse);
//画出时针和分针
double angle1=(time.getHours()*2*Math.PI)/12.0; //时针与12点钟夹角(以弧度表示)
double angle2=(time.getMinutes()*2*Math.PI)/60.0; //分针与12点钟夹角(以弧度表示)
Point2D.Double hourpoint=new Point2D.Double(width/2+radius*Math.sin(angle1),height/2-radius*Math.cos(angle1)); //时针指向与圆相交的点
Point2D.Double minutepoint=new Point2D.Double(width/2+radius*Math.sin(angle2),height/2-radius*Math.cos(angle2)); //分针指向与圆相交的点
Line2D.Double hourline=new Line2D.Double(point0,hourpoint);
Line2D.Double minuteline=new Line2D.Double(point0,minutepoint);
g2.setColor(Color.black);
g2.draw(hourline); //draw the hour hand use the black color
g2.setColor(Color.YELLOW);
g2.draw(minuteline); //draw the minute hand use the yellow color
}
public ClockTime time;
}
最后还有个时间类,用来存储时间,并提供各种对时间的存,取方式
package 钟表;
public class ClockTime
{
public ClockTime(double a_hours,double a_minutes ) //initializtion
{
hours=a_hours%12;
minutes=a_minutes%60;
}
public double getHours() //get hours
{
return hours;
}
public double getMinutes() //get minutes
{
return minutes;
}
public void setTime(double a_hours,double a_minutes) // set time
{
hours=a_hours%12;
minutes=a_minutes%60;
}
public void setHours(double a_hours) //set hours
{
hours=a_hours%12;
}
public void setMinutes(double a_minutes) //set time
{
minutes=a_minutes%60;
}
private double hours;
private double minutes;
}