抢分100分 题目:编写一个线程实现一个数字时钟的应用程序,该线程采用休眠的方式,绝大部分时间让系统使用。急啊,给我代码谢谢 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 强分public class 数字时钟 { public static void main(String[] args) { new Thread(new Runnable() { public void run() { 一个数字时钟,该线程采用休眠的方式,绝大部分时间让系统使用; } }).start(); }} /** * created at 2008/01/03 */package clock;import java.awt.FlowLayout;import java.util.Date;import javax.swing.JFrame;import javax.swing.JLabel;public class Clock implements Runnable { private JLabel lbvTime = null; public Clock() { JFrame frame = new JFrame(); frame.setTitle("Clock"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setBounds(300, 100, 190, 50); frame.getContentPane().setLayout(new FlowLayout()); frame.setResizable(false); lbvTime = new JLabel(); frame.add(lbvTime); frame.setVisible(true); } public void run() { while (true) { lbvTime.setText(new Date().toString()); try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); break; } } } public static void main(String[] args) { Thread t = new Thread(new Clock()); t.start(); }} 精简版/** * created at 2008/01/03 */package clock;import java.awt.FlowLayout;import java.util.Date;import javax.swing.JFrame;import javax.swing.JLabel;public class Clock { public static void main(String[] args) { new Thread(new Runnable() { public void run() { JFrame frame = new JFrame(); frame.setTitle("Clock"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setBounds(300, 100, 190, 50); frame.getContentPane().setLayout(new FlowLayout()); frame.setResizable(false); JLabel lbvTime = new JLabel(); frame.add(lbvTime); frame.setVisible(true); while (true) { lbvTime.setText(new Date().toString()); try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); break; } } } }).start(); }} 提示:可以用java.util.Timer+java.util.Timertask 最主要就是用到Thread.sleep(time)这个方法,其他的好说 6楼的 frame.add(lbvTime);这个语句好像有点问题改成 frame.getContentPane().add(lbvTime)就可以运行 frame.getContentPane()是1.4及以下版本用的我那个是1.5的 import java.awt.*;import javax.swing.*;import java.util.Date;public class Crystal extends JFrame implements Runnable{ private JLabel jl1,jl2; Date d; Thread t; public Crystal() { super(); t=new Thread(this); t.start(); jl1=new JLabel("显示时间"); jl2=new JLabel(); JPanel jp1=new JPanel(); JPanel jp2=new JPanel(); jp1.setLayout(new FlowLayout()); jp1.add(jl1); jp2.setLayout(new FlowLayout()); jp2.add(jl2); this.getContentPane().setLayout(new BorderLayout()); this.getContentPane().add(jp1,BorderLayout.NORTH); this.getContentPane().add(jp2,BorderLayout.CENTER); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public void run() { while(true) { try { t.sleep(1000); } catch(InterruptedException e) { System.out.print("线程中断"); } d=new Date(); jl2.setText(d.toString()); } } public static void main(String args[]) { Crystal c=new Crystal(); c.setBounds(300,300,300,300); c.setVisible(true); c.setResizable(false); }} package clock; import java.awt.FlowLayout; import java.util.Date; import javax.swing.JFrame; import javax.swing.JLabel; public class Clock implements Runnable { private JLabel lbvTime = null; public Clock() { JFrame frame = new JFrame(); frame.setTitle("Clock"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setBounds(300, 100, 190, 50); frame.getContentPane().setLayout(new FlowLayout()); frame.setResizable(false); lbvTime = new JLabel(); frame.add(lbvTime); frame.setVisible(true); } public void run() { while (true) { lbvTime.setText(new Date().toString()); try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); break; } } } public static void main(String[] args) { Thread t = new Thread(new Clock()); t.start(); } } import java.awt.*; import javax.swing.*; import java.util.Date; public class Crystal extends JFrame implements Runnable { private JLabel jl1,jl2; Date d; Thread t; public Crystal() { super(); t=new Thread(this); t.start(); jl1=new JLabel("显示时间"); jl2=new JLabel(); JPanel jp1=new JPanel(); JPanel jp2=new JPanel(); jp1.setLayout(new FlowLayout()); jp1.add(jl1); jp2.setLayout(new FlowLayout()); jp2.add(jl2); this.getContentPane().setLayout(new BorderLayout()); this.getContentPane().add(jp1,BorderLayout.NORTH); this.getContentPane().add(jp2,BorderLayout.CENTER); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public void run() { while(true) { try { t.sleep(1000); } catch(InterruptedException e) { System.out.print("线程中断"); } d=new Date(); jl2.setText(d.toString()); } } public static void main(String args[]) { Crystal c=new Crystal(); c.setBounds(300,300,300,300); c.setVisible(true); c.setResizable(false); } } public class 数字时钟 { public static void main(String[] args) { new Thread(new Runnable() { public void run() { 一个数字时钟,该线程采用休眠的方式,绝大部分时间让系统使用; } }).start(); }} package clock;import java.awt.FlowLayout;import java.util.Date;import javax.swing.JFrame;import javax.swing.JLabel;public class Clock { public static void main(String[] args) { new Thread(new Runnable() { public void run() { JFrame frame = new JFrame(); frame.setTitle("Clock"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setBounds(300, 100, 190, 50); frame.getContentPane().setLayout(new FlowLayout()); frame.setResizable(false); JLabel lbvTime = new JLabel(); frame.add(lbvTime); frame.setVisible(true); while (true) { lbvTime.setText(new Date().toString()); try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); break; } } } }).start(); }} jdk-6-rc-windows-i586.exe的rc什么意思? 如何设置socket超时的时间? 如何设置JTREE的直角连线的问题啊? 大家帮忙看看是什么问题,先谢了啊 在JAVA中那个包中是媒体的可以发出声音的 马上要考试,谁有关于java事件触发方面的代码?急! 出现这个错误提示为什么?请各位指点! 如何动态的使用对象! 谁有JAVAMAIL的例子?给我一个 java swing 调用dll后在窗口内切换输入法程序出现无响应! 问个关于IO问题, jvm crash 问题
public static void main(String[] args) {
new Thread(new Runnable() {
public void run() {
一个数字时钟,该线程采用休眠的方式,绝大部分时间让系统使用;
}
}).start();
}
}
* created at 2008/01/03
*/
package clock;import java.awt.FlowLayout;
import java.util.Date;import javax.swing.JFrame;
import javax.swing.JLabel;public class Clock implements Runnable { private JLabel lbvTime = null;
public Clock() {
JFrame frame = new JFrame();
frame.setTitle("Clock");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(300, 100, 190, 50);
frame.getContentPane().setLayout(new FlowLayout());
frame.setResizable(false);
lbvTime = new JLabel();
frame.add(lbvTime);
frame.setVisible(true);
} public void run() {
while (true) {
lbvTime.setText(new Date().toString());
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
break;
}
}
}
public static void main(String[] args) {
Thread t = new Thread(new Clock());
t.start();
}
}
/**
* created at 2008/01/03
*/
package clock;import java.awt.FlowLayout;
import java.util.Date;import javax.swing.JFrame;
import javax.swing.JLabel;public class Clock {
public static void main(String[] args) {
new Thread(new Runnable() {
public void run() {
JFrame frame = new JFrame();
frame.setTitle("Clock");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(300, 100, 190, 50);
frame.getContentPane().setLayout(new FlowLayout());
frame.setResizable(false);
JLabel lbvTime = new JLabel();
frame.add(lbvTime);
frame.setVisible(true);
while (true) {
lbvTime.setText(new Date().toString());
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
break;
}
}
}
}).start();
}
}
这个语句好像有点问题改成 frame.getContentPane().add(lbvTime)
就可以运行
我那个是1.5的
import javax.swing.*;
import java.util.Date;public class Crystal extends JFrame implements Runnable
{
private JLabel jl1,jl2;
Date d;
Thread t;
public Crystal()
{
super();
t=new Thread(this);
t.start();
jl1=new JLabel("显示时间");
jl2=new JLabel();
JPanel jp1=new JPanel();
JPanel jp2=new JPanel();
jp1.setLayout(new FlowLayout());
jp1.add(jl1);
jp2.setLayout(new FlowLayout());
jp2.add(jl2);
this.getContentPane().setLayout(new BorderLayout());
this.getContentPane().add(jp1,BorderLayout.NORTH);
this.getContentPane().add(jp2,BorderLayout.CENTER);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void run()
{
while(true)
{
try
{
t.sleep(1000);
}
catch(InterruptedException e)
{
System.out.print("线程中断");
}
d=new Date();
jl2.setText(d.toString());
}
}
public static void main(String args[])
{
Crystal c=new Crystal();
c.setBounds(300,300,300,300);
c.setVisible(true);
c.setResizable(false);
}
}
import java.util.Date; import javax.swing.JFrame;
import javax.swing.JLabel; public class Clock implements Runnable { private JLabel lbvTime = null; public Clock() {
JFrame frame = new JFrame();
frame.setTitle("Clock");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(300, 100, 190, 50);
frame.getContentPane().setLayout(new FlowLayout());
frame.setResizable(false);
lbvTime = new JLabel();
frame.add(lbvTime);
frame.setVisible(true);
} public void run() {
while (true) {
lbvTime.setText(new Date().toString());
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
break;
}
}
} public static void main(String[] args) {
Thread t = new Thread(new Clock());
t.start();
}
}
import javax.swing.*;
import java.util.Date; public class Crystal extends JFrame implements Runnable
{
private JLabel jl1,jl2;
Date d;
Thread t;
public Crystal()
{
super();
t=new Thread(this);
t.start();
jl1=new JLabel("显示时间");
jl2=new JLabel();
JPanel jp1=new JPanel();
JPanel jp2=new JPanel();
jp1.setLayout(new FlowLayout());
jp1.add(jl1);
jp2.setLayout(new FlowLayout());
jp2.add(jl2);
this.getContentPane().setLayout(new BorderLayout());
this.getContentPane().add(jp1,BorderLayout.NORTH);
this.getContentPane().add(jp2,BorderLayout.CENTER);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }
public void run()
{
while(true)
{
try
{
t.sleep(1000);
}
catch(InterruptedException e)
{
System.out.print("线程中断");
}
d=new Date();
jl2.setText(d.toString());
}
}
public static void main(String args[])
{
Crystal c=new Crystal();
c.setBounds(300,300,300,300);
c.setVisible(true);
c.setResizable(false);
}
}
public static void main(String[] args) {
new Thread(new Runnable() {
public void run() {
一个数字时钟,该线程采用休眠的方式,绝大部分时间让系统使用;
}
}).start();
}
}
import java.util.Date;import javax.swing.JFrame;
import javax.swing.JLabel;public class Clock {
public static void main(String[] args) {
new Thread(new Runnable() {
public void run() {
JFrame frame = new JFrame();
frame.setTitle("Clock");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(300, 100, 190, 50);
frame.getContentPane().setLayout(new FlowLayout());
frame.setResizable(false);
JLabel lbvTime = new JLabel();
frame.add(lbvTime);
frame.setVisible(true);
while (true) {
lbvTime.setText(new Date().toString());
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
break;
}
}
}
}).start();
}
}