当前位置: 代码迷 >> Java相关 >> 嵌入到HTML
  详细解决方案

嵌入到HTML

热度:306   发布时间:2007-05-16 13:55:07.0
嵌入到HTML

请问一下高手,下面是Clock.java的代码,请问一下怎样可以把它嵌入到HTML的代码中去呢??




package clock;

import java.util.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class Clock extends JFrame implements Runnable{
public Clock()
{
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try
{
jbInit();
}
catch(Exception e)
{
e.printStackTrace();
}
}
Thread thread1;//定义线程对象
Color handColor;//定义表示时针、分针和秒针颜色的变量
Color numberColor;//定义表示时钟数字颜色的变量

JLabel jLabel1 = new JLabel();
public void init()
{
int x,y;
handColor = Color.blue;
numberColor = Color.darkGray;
setBackground(Color.white);
}
//paint is the main part of the program
public void paint(Graphics g)
{
int xh,yh,xm,ym,xs,ys,s = 0,m = 10,h=0,xpoint,ypoint;
int day,month,year,weekday;
Calendar c1 = Calendar.getInstance();
//或的当前时间
s = c1.get(Calendar.SECOND);
m = c1.get(Calendar.MINUTE);
h = c1.get(Calendar.HOUR_OF_DAY);
//获得当前日期
day = c1.get(Calendar.DATE);
month = c1.get(Calendar.MONTH) + 1;
year = c1.get(Calendar.YEAR);
weekday = c1.get(Calendar.DAY_OF_WEEK)-1;
//在标签控件上显示日期和时间
jLabel1.setText(" "+year+"年"+month+"月"+day+"日"+" "+"星期"+weekday+" "+h+":"+m+":"+s);
xpoint = 130;
ypoint = 100;
//计算时针、分针和秒针的位置
xs = (int)(Math.cos(s * 3.14f/30 -3.14f/2) * 45 + xpoint);
ys = (int)(Math.sin(s * 3.14f/30 -3.14f/2) * 45 + ypoint);
xm = (int)(Math.cos(m * 3.14f/30 -3.14f/2) * 40 + xpoint);
ym = (int)(Math.sin(m * 3.14f/30 -3.14f/2) * 40 + ypoint);
xh = (int)(Math.cos((h*30 + m/2) * 3.14f/180 - 3.14f/2) * 30 +xpoint);
yh = (int)(Math.sin((h*30 + m/2) * 3.14f/180 - 3.14f/2) * 30 +ypoint);
//绘制时钟背景
g.setColor(handColor);
g.clearRect(0,0,260,200);
g.drawOval(xpoint/2+10,ypoint/2-5,110,110);
g.setColor(numberColor);
g.drawString("9",xpoint-45,ypoint+3);
g.drawString("3",xpoint+40,ypoint+3);
g.drawString("12",xpoint-5,ypoint-37);
g.drawString("6",xpoint-3,ypoint+45);
g.setColor(getBackground());

g.setColor(numberColor);
g.drawString("",5,125);
//绘制时针、分针和秒针
g.drawLine(xpoint,ypoint,xs,ys);
g.setColor(handColor);
g.drawLine(xpoint,ypoint,xm,ym);
g.drawLine(xpoint,ypoint,xh,yh);
}

//定义线程的run方法
public void run()
{
Thread me = Thread.currentThread();
while(thread1 == me)
{
try
{
Thread.currentThread().sleep(100);
}
catch(InterruptedException e)
{

}
repaint();
}
}
//开始执行线程
public void start()
{
thread1 = new Thread(this);
thread1.start();
}
//停止线程
public void stop()
{
thread1 = null;
}
public void update(Graphics g)
{
paint(g);
}
private void jbInit() throws Exception
{
this.getContentPane().add(jLabel1,BorderLayout.SOUTH);
}
protected void processWindowEvent(WindowEvent e)
{
super.processWindowEvent(e);
if(e.getID() == WindowEvent.WINDOW_CLOSING)
{
System.exit(0);
}

}

public static void main(String[] args) {
Clock clock1 = new Clock();
clock1.init();
clock1.start();
clock1.setSize(280,230);
clock1.setResizable(false);
clock1.setVisible(true);

}
}


搜索更多相关的解决方案: HTML  

----------------解决方案--------------------------------------------------------
你做成applet就可以了
----------------解决方案--------------------------------------------------------
<HTML>
<HEAD>
</HEAD>
<BODY BGCOLOR="000000">
<CENTER>
<APPLET
code = "***.class"
width = "500"
height = "300"
>
</APPLET>
</CENTER>
</BODY>
</HTML> 用textpad 写进去, 存成*** ***.class 是你编译后的Class文件,放到一起就可以了
浏览器需要支持Applet
----------------解决方案--------------------------------------------------------
  相关解决方案