button.setText("B")这条语句执行后,前台界面还没来得急刷新,紧接着又开始下面的延迟了。等到button.setText("C")结束后才刷新。所以你在setText("B")后面显示地刷新一下看看。

解决方案 »

  1.   

    我知道了,但是还不会解决。
    事件里面的button好像不是真正的jButton1,只是它的一个副本,所以它改变的中间过程不会显示。
    但是我把下面的语句:
        if(button.getText().equals("A"))
        {
      button.setText("B");
        }改成:
        if(button.getText().equals("A"))
        {
      jButton1.setText("B");
        }
    后,仍然无法显示出“B”来。请指教。
    是不是应该把这个事件写到“jPanel1”或“jFrame1”里面啊?
      

  2.   

    同一楼上,
    建议楼主自己写一个button的类,
    继承jButton,
    然后实现Runnable接口,
    新写一个方法,
    setDelayText(要改变的文字,时间);
    然后用sleep()延时,最后变成
    MyButton my = new MyButton();
    my.setText("A");
    my.setTextDelay("B",3000);
    //就是3秒后变成"B"setTextDelay中,大概是:
    this.sleep(3000);
    this.setText("B");
    凭空想象的,错了的话别骂我:)good luck
      

  3.   

    用Timer 结合TimeTask来实际这样功能比较好吧
      

  4.   

    我不会用线程编程,
    所以也不会用SLEEP 和 RUNNABLE。
    我把代码写在下面,
    上、下两端都不用看,
    只是一个建立界面的过程,
    看addactionlistener部分就好了。
    希望有经验的朋友能用线程给写个解决方法,以得学习!谢谢!
    (上面程序中不是"1 = i",是“小写L = i”,那是我用for写的延时语句,呵呵。)public class kkk
    {
    public static JButton jButton1 = new JButton();
    public static Container contentPane;
    public static JPanel jPanel1 = new JPanel();
    public static JFrame Frame1 = new JFrame("kkk");
    public static void main(String[] args)
    {
    //初始为A
    jButton1.setText("A");jButton1.addActionListener(
    new ActionListener()
    {
    public void actionPerformed(ActionEvent e)
    {
    JButton button = (JButton)e.getSource();

    //1)延时1秒…… //2)变为B
    button.setText("B");//或者是 jButton1.setText("B");
                      //听说actionlistener只有“开”和“关”两种状态,
                      //所以在这里不管是用jButton1还是button,过程都不会显示
                      //于是也就放弃了在这里尝试其它的方法。 //3)延时1秒…… //4)变为C
    button.setText("C");
    }
    });jPanel1.add(jButton1, BorderLayout.CENTER);
    Frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Frame1.setSize(new Dimension(550, 300));
    contentPane = Frame1.getContentPane();
    contentPane.add(jPanel1, BorderLayout.CENTER);
    Frame1.setResizable(false);
    Frame1.setVisible(true);
    }
    }
      

  5.   

    没必要使用多线程!原因就是Swing组件的更新和事件的响应都是在event-dispatching thread中完成的,而事件响应的时候,event-dispatching thread被事件响应方法占据,所以组件不会被更新。而直到事件响应方法退出时才有可能去更新Swing组件。把下面这条语句加到button.setText("B");后面就可以了this.paint(this.getGraphics());
      

  6.   

    试试这个,
    不过有个遗憾,就是在延时的时候,
    按钮始终是按下的状态,
    不知道怎么改过来。那位大虾帮忙搞定一下吧!good luckimport javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    public class kkk
    {
    public static myButton jButton1 = new myButton();
    public static Container contentPane;
    public static JPanel jPanel1 = new JPanel();
    public static JFrame Frame1 = new JFrame("kkk");
    public static void main(String[] args){
    jButton1.setText("A");

    jButton1.addActionListener(
    new ActionListener(){
    public void actionPerformed(ActionEvent e){
    myButton button = (myButton)e.getSource();

    // button.setText("B");

    button.setTextDelay("C",2000);
    }
    }
    );

    jPanel1.add(jButton1, BorderLayout.CENTER);
    Frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Frame1.setSize(new Dimension(550, 300));
    contentPane = Frame1.getContentPane();
    contentPane.add(jPanel1, BorderLayout.CENTER);
    Frame1.setResizable(false);
    Frame1.setVisible(true);
    }
    }class myButton extends JButton implements Runnable{

    private Thread t = null;

    private int iDelayMillSenc = 0;

    public myButton(){
    super();
    }

    public void setText(String str){
    super.setText(str);
    }


    public void setTextDelay(String str,int iDelayMillSenc){ this.iDelayMillSenc = iDelayMillSenc;

    if ( this.isSelected() == false) {
    t = new Thread(this);
    t.run();
    }

    this.setText(str);
    }

    public void run(){
    try{
      t.sleep(iDelayMillSenc);
    }catch(InterruptedException ie){
    ie.printStackTrace();
    }
    }

    }
      

  7.   

    看了myhotsun(科科) 的回复,
    有所醒悟,
    把setTextDelay这个方法加入
    this.paint(this.getGraphics());
    就可以了,如下所示:
    public void setTextDelay(String str,int iDelayMillSenc){ this.iDelayMillSenc = iDelayMillSenc;

    this.paint(this.getGraphics());

    if ( this.isSelected() == false) {
    t = new Thread(this);
    t.run();
    }

    this.setText(str);
    }最后感谢myhotsun(科科) ,谢谢!good luck