本来按下“显示时间”后,TextArea应该不断刷新当前时间并且下面的字正常滚动的,为什么
我的代码按下后只是左滚动一下,右滚动呢,如图, 求解答,下面是我的代码和程序结果的图片, c币真的就剩这么多了
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import javax.swing.*;
import java.util.*;
public class Clock{
public static void main(String[] args){
new ThreadFrame();
}
}
class ThreadFrame extends JFrame implements Runnable, ActionListener{
private JTextArea area;
private JPanel pl, pl2;
private JLabel lb, blank;
private boolean stoptime = false;
private JButton bt[] = new JButton[4];
private Thread count, time, words;
private SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日 E\n a hh时mm分ss秒");
private int sumtime;
public ThreadFrame(){
this.setTitle("java动态时钟");
this.setBounds(600, 200, 250, 240);
this.setLayout(new FlowLayout());
area = new JTextArea(2, 25);
area.setText("welcome to use");
area.setFont(new Font("黑体", Font.BOLD, 12));
pl = new JPanel(new FlowLayout());
pl.setBackground(Color.BLUE);
pl.add(area);
this.add(pl);
pl2 = new JPanel();
pl2.setLayout(new GridLayout(2, 2, 10, 10));
pl2.setBackground(Color.CYAN);
//pl2.set
this.add(pl2);
lb = new JLabel("欢迎使用本时钟");
this.add(lb);
String a[] = {"显示时间", "停止显示", "倒计时", "停止计时"};
for(int i = 0; i < a.length; i++){
bt[i] = new JButton(a[i]);
pl2.add(bt[i]);
bt[i].addActionListener(this);
}
time = new Thread(this);
count = new Thread(this);
words = new Thread(this);
this.setVisible(true);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
words.start();
}
public void run(){
if(Thread.currentThread() == words){
while(true){
int x = lb.getBounds().x;
int y = lb.getBounds().y;
x+=5;
lb.setLocation(x, y);
if(x > 320){
x = 10;
lb.setLocation(x, y);
}
try{
words.sleep(1500); // 单位是毫秒
}catch(InterruptedException i1){}
}
}
if(Thread.currentThread() == count){
String strd = JOptionPane.showInputDialog(this, "格式:hh:mm:ss", "倒计时",
JOptionPane.PLAIN_MESSAGE);
try{
sumtime = (int)((Double.parseDouble(strd.substring(0, 2)))*3600 +(Double.parseDouble
(strd.substring(3, 5)))*60+(Double.parseDouble(strd.substring(6, 8))));
}catch(NullPointerException ne){
sumtime = 0;
};
while(true){
try{
area.setText(Integer.toString(sumtime/3600)+":"+
(Integer.toString((sumtime%3600)/60)+":"+
(Integer.toString(sumtime-(sumtime/3600)*3600-((sumtime%3600)/60)*60))));
count.sleep(1000);
}catch(InterruptedException e){}
sumtime--;
if(sumtime == -1)
return;
}
}
if(Thread.currentThread() == time){
while(true){
String str = format.format(new Date());
area.setText(str);
try{
Thread.sleep(1000);
}catch(InterruptedException e){}
if(stoptime == true)
return;
}
}
}
public void actionPerformed(ActionEvent e){
if(e.getSource() == bt[0]){
if(!time.isAlive()){
time = new Thread(this);
sumtime = 0;
stoptime = false;
time.start();
}
}else if(e.getSource() == bt[1]){
stoptime = true;
area.setText("welcome to use");
}else if(e.getSource() == bt[2]){
if(!count.isAlive()){
stoptime = true;
count = new Thread(this);
count.start();
}
}else if(e.getSource() == bt[3]){
if(count.isAlive()){
sumtime = 0;
area.setText("welcome to use");
}
}
}
}