java 怎么在一个JLabel上显示动态时间 用线程
3个回答
展开全部
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class FrameTest {
public static void main(String[] args) {
JFrame f = new MyFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
class MyFrame extends JFrame {
private JLabel label = new JLabel();
private Thread th;
public MyFrame() {
this.init();
this.add(label);
th = new Thread() {
public void run() {
while(true) {
Date d = new Date();
String s = String.format("%tT", d);
label.setText(s);
try {
Thread.sleep(1000);
} catch(Exception e) {
}
}
}
};
th.start();
}
private void init() {
this.setSize(300, 200);
this.setLocation(300, 200);
this.setLayout(new FlowLayout());
}
}
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class FrameTest {
public static void main(String[] args) {
JFrame f = new MyFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
class MyFrame extends JFrame {
private JLabel label = new JLabel();
private Thread th;
public MyFrame() {
this.init();
this.add(label);
th = new Thread() {
public void run() {
while(true) {
Date d = new Date();
String s = String.format("%tT", d);
label.setText(s);
try {
Thread.sleep(1000);
} catch(Exception e) {
}
}
}
};
th.start();
}
private void init() {
this.setSize(300, 200);
this.setLocation(300, 200);
this.setLayout(new FlowLayout());
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
方法与很多中,你也可以重新定义JLabel。下面是一种方法。
public class test extends JApplet
{
public void init()
{
newlabel nl=new newlabel();
Thread th=new Thread (nl);
th.start();
this.add(nl);
}
class newlabel extends JLabel implements Runnable
{
public newlabel()
{
super();
}
@Override
public void run() {
String date="yyyy-MM-dd-HH-mm-ss";
while( true) {
SimpleDateFormat bartDateFormat = new SimpleDateFormat(date);
String ddate = bartDateFormat.format(Calendar.getInstance().getTime());
this.setText(ddate);
try{
Thread.sleep(1000);
}catch(InterruptedException e){System.out.println(e);};
}
}
}
}
public class test extends JApplet
{
public void init()
{
newlabel nl=new newlabel();
Thread th=new Thread (nl);
th.start();
this.add(nl);
}
class newlabel extends JLabel implements Runnable
{
public newlabel()
{
super();
}
@Override
public void run() {
String date="yyyy-MM-dd-HH-mm-ss";
while( true) {
SimpleDateFormat bartDateFormat = new SimpleDateFormat(date);
String ddate = bartDateFormat.format(Calendar.getInstance().getTime());
this.setText(ddate);
try{
Thread.sleep(1000);
}catch(InterruptedException e){System.out.println(e);};
}
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Datetime extends JFrame implements Runnable {
public JLabel la = new JLabel();
SimpleDateFormat form = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String ss = form.format(new Date());
public Datetime(){
la.setSize(100,200);
this.add(la);
this.setSize(300,200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public void run() {
while (true) {
try {
la.setText(ss);
ss = form.format(new Date());
Thread.currentThread().sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
new Thread(new Datetime()).start();
}
}
import java.util.Date;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Datetime extends JFrame implements Runnable {
public JLabel la = new JLabel();
SimpleDateFormat form = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String ss = form.format(new Date());
public Datetime(){
la.setSize(100,200);
this.add(la);
this.setSize(300,200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public void run() {
while (true) {
try {
la.setText(ss);
ss = form.format(new Date());
Thread.currentThread().sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
new Thread(new Datetime()).start();
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询