当前位置: 代码迷 >> Java相关 >> 初学者 求大神帮忙
  详细解决方案

初学者 求大神帮忙

热度:8981   发布时间:2013-02-25 21:45:54.0
菜鸟 求大神帮忙
package helloworld;

import java.awt.*;
import javax.swing.*;
import java.util.Date;
import java.text.SimpleDateFormat;

public class MyDate {

String str = null;
JFrame f = new JFrame("MyDate!");
Container c = f.getContentPane();
JLabel L = new JLabel("现在时间:");
JLabel dat = new JLabel();

public static void main(String[] args) {

new MyDate();
}

public MyDate() {

new ThreadDate().start();

}

class ThreadDate extends Thread {

public void run() {
f.setBounds(150, 150, 260, 180);

while (true) {
Date dd = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
str = sdf.format(dd);
try {
sleep(1000);
} catch (Exception e) {
e.printStackTrace();
}

f.setLayout(null);
c.add(L);
c.add(dat);
dat.setText(str);
L.setBounds(5, 5, 80, 20);
dat.setBounds(100, 60, 200, 30);
f.setVisible(true);
f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);
}
}

}

}


怎样将时间(str)变大并且变成绿色显示

------解决方案--------------------------------------------------------
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.JFrame;
import javax.swing.JLabel;

public class MyDate {

String str = null;

JFrame f = new JFrame("MyDate!");

Container c = f.getContentPane();

JLabel L = new JLabel("现在时间:");

JLabel dat = new JLabel();


public static void main(String[] args) {

new MyDate();
}

public MyDate() {

new ThreadDate().start();

}

class ThreadDate extends Thread {

public void run() {
f.setBounds(150, 150, 260, 180);

while (true) {
Date dd = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
str = sdf.format(dd);
try {
sleep(1000);
} catch (Exception e) {
e.printStackTrace();
}

f.setLayout(null);
c.add(L);
c.add(dat);
dat.setForeground(Color.GREEN);
Font font = new Font(dat.getFont().getName(),Font.BOLD,20);
dat.setFont(font);
dat.setText(str);
L.setBounds(5, 5, 80, 20);
dat.setBounds(100, 60, 200, 30);
f.setVisible(true);
f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);
}
}

}

}
  相关解决方案