我是想让程序一运行 JLabal标签上的文字就显示,而不是点击按钮。现在还不知道
怎么设置!!!
代码在下面
import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
class TimeDemo extends JFrame implements ActionListener{
JButton bStart,bStop,bContinue;
//JLabel 标签showWord
JLabel showWord;
Timer time;
int number=0,start=1;
char[] chainWord;
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==time) {
showWord.setText(""+chainWord[number]);
number++;
if(number==chainWord.length-1)
number=0;
}
else if(e.getSource()==bStart)
time.start();
else if(e.getSource()==bContinue)
time.restart();
else if(e.getSource()==bStop)
time.stop();
}
public TimeDemo() {
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(200, 200, 300, 180);
//---------------------------------------------
//创建一个计时器,每隔1000 ms 循环
time=new Timer(1000, this);
//创建JLabel面板
showWord=new JLabel();
//这里怎么设置一开始就启动?
//创建面版显示字体格式
showWord.setFont(new Font("宋体", Font.BOLD, 58));
//设置显示的文字居中
showWord.setHorizontalAlignment(JLabel.CENTER);
//设置按钮
bStart=new JButton("开始");
bStop=new JButton("暂停");
bContinue=new JButton("继续");
//添加监视器
bStart.addActionListener(this);
bContinue.addActionListener(this);
bStop.addActionListener(this);
//添加一个JPanel容器
JPanel pNorth=new JPanel();
//将按钮添加到JPanel容器
pNorth.add(bStart);
pNorth.add(bStop);
pNorth.add(bContinue);
//将JLabel面板和JPanel容器添加到JFrame
this.add(showWord,BorderLayout.CENTER);
this.add(pNorth,BorderLayout.NORTH);
validate();
chainWord=new char[5];
for (int k = 0,i='我'; k < chainWord.length;i++, k++) {
chainWord[k]=(char)i;
}
}
}
public class Test13 {
public static void main(String[] args) {
// TODO Auto-generated method stub
new TimeDemo();
}
}