新网创想网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
按照你的要求编写的java输入时间显示钟表的程序如下:
定日ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为成都创新互联的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:028-86922220(备注:SSL证书合作)期待与您的合作!
import java.awt.BorderLayout;import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Clock extends JFrame implements ActionListener{ JLabel jl_hour=new JLabel("时");
JLabel jl_minute=new JLabel("分");
JLabel jl_second=new JLabel("秒");
JTextField jtf_hour=new JTextField(10);
JTextField jtf_minute=new JTextField(10);
JTextField jtf_second=new JTextField(10);
JButton jb_input=new JButton("显示");
JPanel jp_input=new JPanel();
ClockPanel jp_clock=new ClockPanel();
Clock(){
super("Clock");
jb_input.addActionListener(this);
jp_input.add(jtf_hour);jp_input.add(jl_hour);
jp_input.add(jtf_minute);jp_input.add(jl_minute);
jp_input.add(jtf_second);jp_input.add(jl_second);
jp_input.add(jb_input);
add(jp_input,BorderLayout.NORTH);
add(jp_clock,BorderLayout.CENTER);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(600,400);
this.setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String[] args) {
new Clock();
}
public void actionPerformed(ActionEvent ae) {
if(ae.getSource()==jb_input){
int Hour=Integer.parseInt(jtf_hour.getText());
int Minute=Integer.parseInt(jtf_minute.getText());
int Second=Integer.parseInt(jtf_second.getText());
jp_clock.setHour(Hour);
jp_clock.setMinute(Minute);
jp_clock.setSecond(Second);
jp_clock.repaint();
}
}
}
class ClockPanel extends JPanel {
private double CenterX,CenterY;
private double Radius=150;
private double HPointX,HPointY,MPointX,MPointY,SPointX,SPointY;
private int Hour,Minute,Second;
private final double PI=3.1415926;
public int getHour() {
return Hour;
}
public void setHour(int hour) {
Hour = hour;
}
public int getMinute() {
return Minute;
}
public void setMinute(int minute) {
Minute = minute;
}
public int getSecond() {
return Second;
}
public void setSecond(int second) {
Second = second;
}
ClockPanel(){
this.Hour=0;
this.Minute=0;
this.Second=0;
}
ClockPanel(int Hour,int Minute,int Second){
this.Hour=Hour;
this.Minute=Minute;
this.Second=Second;
}
public void paintComponent(Graphics g){
super.paintComponent(g);
CenterX=this.getWidth()/2;
CenterY=this.getHeight()/2;
double HRadian=(PI/6)*Hour+(PI/360)*Minute+(PI/21600)*Second;
double MRadian=(PI/30)*Minute+(PI/1800)*Second;
double SRadian=(PI/30)*Second;
HPointX=CenterX+Radius*0.6*Math.sin(HRadian);
HPointY=CenterY-Radius*0.6*Math.cos(HRadian);
MPointX=CenterX+Radius*0.8*Math.sin(MRadian);
MPointY=CenterY-Radius*0.8*Math.cos(MRadian);
SPointX=CenterX+Radius*Math.sin(SRadian);
SPointY=CenterY-Radius*Math.cos(SRadian);
g.drawOval((int)(CenterX-Radius), (int)(CenterY-Radius), (int)(2*Radius),(int)(2*Radius));
g.drawString("12",(int)(CenterX),(int)(CenterY-Radius)+10);
g.drawString("3",(int)(CenterX+Radius)-6 , (int)(CenterY));
g.drawString("6", (int)(CenterX), (int)(CenterY+Radius));
g.drawString("9",(int)(CenterX-Radius), (int)(CenterY));
g.drawLine((int)(CenterX), (int)(CenterY), (int)(SPointX), (int)(SPointY));
g.drawLine((int)(CenterX), (int)(CenterY), (int)(MPointX), (int)(MPointY));
g.drawLine((int)(CenterX), (int)(CenterY), (int)(HPointX), (int)(HPointY));
}
}
__ __
|__| . | |__
|__| . | |__|
如上面这样的一个数字8,是所有线条显示的情况,其他数字均可如上组合,统计下来数字共由三行字符组成,最上面一行、中间一行、和下面一行,取时间比较容易,剩下就是输出打印的问题,而打印时每个数字进行转换,分别对应三行字串即可。可以写个类ClockNumber.java用于转换数字和三行字串。
public class ClockNumber {
public Character i;
public String top; //上面一行
public String mid; //中间一行
public String bot; //下面一行
//构造函数
public ClockNumber(Character i){
this.i=i;
}
public String getTop() {
swich(i){
case '1':
return " ";
case '2':
return " __ ";
//...以此类推,按不同数字上面不同的第一行给出字串即可。
// 别忘了实现冒号的显示
case ':':
return " ";
}
// 参考上面方法再实现getMid()和 getBot两个函数。
//...
}
另外,实现一下中间的分隔字串,两个点的方法。
打印时,分别输出三行即可:
如:
public class ClockView {
public String time;
public String topLine;
public String midLine;
public String botLine;
public ClockView(String time){
this.time = time;
}
public String getTopLine(){
//此处示例,即Charactor
for(int i=0; itime.length; i++){
Character n = time.charAt(i);
ClockNumber cn = new ClockNumber(n);
topLine += cn.top; // 将上面一行相应的字串连起来。
}
}
//...以此类推,完成midLine和botLine的get方法。
//测试示例:
public static void main(String[] args) {
Date now = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
String time= sdf.format(now); //类似:"19:24:58";
ClockView cv = new ClockView(time);
System.out.println(cv.topLine);
System.out.println(cv.midLine);
System.out.println(cv.botLine);
//ok, 搞定了!
}
}
public class Horologe{
private int hour;
private int minute;
private int second;
public void setTime(int hour,int minute,int second){ //设置时间的方法
this.hour = hour; //传进的参数赋值给成员变量
this.minute = minute;
this.second = second;
}
public String showTime(){ //显示时间的方法
return "小时:"+hour+" 分钟:"+minute+" 秒: "+second;
}
}
希望能够帮到你