import java.applet.*;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.net.URL;
import java.text.SimpleDateFormat;
import javax.swing.*;
public class MyLabel extends Applet{
private ImageIcon bgImage;
private JLabel Label;
private JLabel label_time;
private JLabel label_run;
private ImageIcon []image;
private static int imageNum;
private boolean running;
private ShowTime showTime;
public void init() {
label_time = new JLabel();
label_run = new JLabel("哈哈哈哈哈....");
Label = new JLabel();
showTime = new ShowTime();
image = new ImageIcon[3];
try {
bgImage = new ImageIcon
(new URL(this.getClass().getResource("")+"bgImage.jpg"));
for (int i = 0; i < 3; i++) {
image[i] = new ImageIcon
(new URL(this.getClass().getResource("")+"image"+i+".jpg"));
}
}
catch (Exception e) {
System.out.println("Load image failed!");
return;
}
this.setSize(Toolkit.getDefaultToolkit().getScreenSize());
imageNum = 0;
setLayout(null);
Label.setSize(300, 300);
Label.setLocation(0, 0);
label_time.setSize(200, 30);
label_time.setLocation(100, 50);
label_run.setSize(200, 50);
label_run.setLocation(0, 400);
add(Label);
add(label_time);
add(label_run);
add(new JTextField(10));
running = true;
showTime.start();
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
imageNum++;
repaint(); // 调用paint( )方法
}
});
}
public void update(Graphics g){
paint(g);
}
public void start(){
this.setVisible(true);
}
public void stop() {
running = false;
}
public void paint(Graphics g) {
/* Draw the line */
g.clearRect(0, 0, getSize().width, getSize().height);
bgImage.paintIcon(this, g, 0, 0);
g.drawImage(image[imageNum%3].getImage(), getSize().width/2 - image[imageNum%3].getIconWidth()/2,
100, image[imageNum%3].getIconWidth(), image[imageNum%3].getIconHeight(), this);
paintComponents(g);
}
public class ShowTime extends Thread{
public void run() {
// TODO Auto-generated method stub
int count = 0;
while(running){
count++;
if(count%10 == 0) {
SimpleDateFormat tempDate = new SimpleDateFormat("yyyy-MM-dd" + " " + "hh:mm:ss");
String datetime = tempDate.format(new java.util.Date());
label_time.setText(datetime);
}
label_run.setLocation((10*count)%1024, 400);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
温馨提示:内容为网友见解,仅供参考