首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >actionPerformed方法的难点

actionPerformed方法的难点
EN

Stack Overflow用户
提问于 2012-03-08 19:55:55
回答 1查看 350关注 0票数 0

嗨,伙计们,当提交按钮被点击时,我开始递增,当它是2时,我想改变图片,当我4岁时,我想改变到另一张图片,但它没有做到,所以如果有人有想法,那就太好了。我正在使用eclipse,程序正在编译和运行。以下是代码

代码语言:javascript
复制
/** Here is the GUI of the program
 * class name SlideShowGui.java
 * @author Kiril Anastasov
 * @date 07/03/2012
 */

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;   

public class SlideShowGui extends JPanel  implements ActionListener, Runnable
{
    JLabel name, comments, images;
    JTextField namejtf, commentsjtf, captionjtf;
    JButton submit;
    ImageIcon pictures;


    SlideShowGui()
    {


        name = new JLabel("Name:");
        this.add(name);

        namejtf = new JTextField(15);
        this.add(namejtf);

        comments = new JLabel("Comments:");
        this.add(comments);

        commentsjtf = new JTextField(15);
        this.add(commentsjtf);

        submit = new JButton("Submit");
        this.add(submit);
        submit.addActionListener(this);


        pictures = new ImageIcon("galileo1.jpg");
        images = new JLabel(pictures);
        this.add(images);


        pictures = new ImageIcon("galileo2.jpg");
        this.add(images);



        captionjtf = new JTextField(24);
        this.add(captionjtf);

    }

    public void actionPerformed(ActionEvent ae)
    {
        Thread t = new Thread(this);
        t.start();

        if(ae.getSource() == submit)
        {

            int i = 0;
            boolean go = true;
            while(go)
            {

                i++;
                System.out.println(i);

                try 
                { 
                    Thread.sleep(2000);
                    if(i == 2)
                    {
                        pictures = new ImageIcon("galileo2.jpg");
                        images.setIcon(pictures);   
                    }


                } 
                catch (InterruptedException ie) 
                {
                     System.out.println("thread exception");
                }
//              pictures = new ImageIcon("galileo2.jpg");
//              images.setIcon(pictures);




            System.out.println("test");
        }


    }



}

    public void run() 
    {

    }
}

/**The driver class of the program. Here is the JFrame 
 * class name TestSlideShow.java
 * @author Kiril Anastasov
 * @date 07/03/2012
 */

import java.awt.*;
import javax.swing.*;
public class TestSlideShow 
{
    public static void main(String[] args) 
    {
        JFrame application = new JFrame();
        SlideShowGui panel = new SlideShowGui();
        application.add(panel);
        application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        application.setSize(300,600);
        application.setLocation(400,100);
        application.setVisible(true);


    }

}
EN

回答 1

Stack Overflow用户

发布于 2012-03-08 20:00:09

千万不要在Swing中使用Thread.sleep(),也不要从Swing Listener中进行初始化,这行代码会导致在JComponent中重新绘制时冻结或停止,请使用Swing Timer,rest在我的answer to your previous post中,clean_up此代码

编辑-> here is four ways how to do it for Swing

票数 8
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9617264

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档