java输入时间显示钟表;要求输入一个时分秒,输出一个静态的钟表,显示你输入的时间。
2个回答
展开全部
按照你的要求编写的java输入时间显示钟表的程序如下:
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));
}
}
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));
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
请问你的钟表是图像吗!如果是在网页上面,建议直接下一个js
如果是swing,那么提点建议吧,用线程!
如果是swing,那么提点建议吧,用线程!
追问
我要的是程序啊
追答
ok,有答案啦!是我想复杂啦!抱歉!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询