简体   繁体   English

在JFrame中使用线程

[英]using thread in JFrame

i have a mainFrame that contains panel. 我有一个包含面板的mainFrame。 i want in this panel a thread that changes label image as long as the application is running... 我希望在此面板中有一个线程可以在应用程序运行时更改标签图像...

when i created the panel that implements runnable, and then created an instance of this panel in the mainframe the application goes into infinite loop... my code as follows: 当我创建实现可运行的面板,然后在大型机中创建该面板的实例时,应用程序进入无限循环……我的代码如下:

public mainFrame()
{
     BanerPanel baner = new BanerPanel();
     baner.run();
}

public class Banner_Panel extends JPanel implements Runnable {

    public Banner_Panel() {
        initComponents();
        imgPath = 2;
        imgLbl = new JLabel(new ImageIcon(getClass().getResource("/Photos/banner_4-01.png")));
        add(imgLbl);
        //run();
    }
    @Override
    public void run() {
        while(true)
        {
            try {
            while (true) {
                Thread.sleep(3000);
                switch(imgPath)
                {
                    case 1:
                        imgLbl.setIcon(new ImageIcon(getClass().getResource("/Photos/banner_4-01.png")));
                        imgPath = 2;
                        break;
                    case 2:
                        imgLbl.setIcon(new ImageIcon(getClass().getResource("/Photos/banner_1-01.png")));
                        imgPath = 3;
                        break;
                    case 3:
                        imgLbl.setIcon(new ImageIcon(getClass().getResource("/Photos/banner_2-01.png")));
                        imgPath = 4;
                        break;
                    case 4:
                        imgLbl.setIcon(new ImageIcon(getClass().getResource("/Photos/banner_3-01.png")));    
                        imgPath = 1;
                        break;
                }

            }
            } catch (InterruptedException iex) {}
        }
    }
  • Don't call JLabel#setIcon(...) in a background thread as this must be called on the Swing event thread or EDT. 不要在后台线程中调用JLabel#setIcon(...) ,因为必须在Swing事件线程或EDT上调用它。 Instead, why not simply us a Swing Timer? 相反,为什么不简单地给我们一个Swing计时器呢?
  • Also, there's no need to continually read in the image from the disk. 此外,也无需连续从磁盘读取映像。 Instead, read the images in once and place the ImageIcons in an array or ArrayList<ImageIcon> and simply iterate through the icons in your Swing Timer. 取而代之的是,一次读取图像并将ImageIcons放置在数组或ArrayList<ImageIcon>并仅通过Swing Timer中的图标进行迭代。
  • Your code actually doesn't use a background thread as you're calling run() directly on your Runnable object which isn't doing any threading at all. 您的代码实际上不使用后台线程,因为您直接在不执行任何线程处理的Runnable对象上调用run() Please read the threading tutorial to see how to use Runnables and Threads (hint you call start() on the Thread). 请阅读线程教程以了解如何使用Runnables和Threads(在线程上调用start()提示)。

For example 例如

// LABEL_SWAP_TIMER_DELAY a constant int = 3000
javax.swing.Timer myTimer = new javax.swing.Timer(LABEL_SWAP_TIMER_DELAY, 
      new ActionListener(){
  private int timerCounter = 0;

  actionPerformed(ActionEvent e) {
    // iconArray is an array of ImageIcons that holds your four icons.
    imgLbl.setIcon(iconArray[timerCounter]);
    timerCounter++;
    timerCounter %= iconArray.length;
  }
});
myTimer.start();

For more, please check out the Swing Timer Tutorial . 有关更多信息,请查看Swing Timer Tutorial

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM