在Swing中调用后台线程

问题描述 投票:2回答:5

第一个代码:

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

public class cos {
   public static int a;

   private static JLabel labeler;

   // public static Runnable r1;

   private JFrame frame;

   /**
    * Launch the application.
    */
   public static void main(String[] args) {
      a = 0;
      EventQueue.invokeLater(new Runnable() {
         public void run() {
            try {
               cos window = new cos();
               window.frame.setVisible(true);
            } catch (Exception e) {
               e.printStackTrace();
            }
         }
      });

   }

   /**
    * Create the application.
    */
   public cos() {
      initialize();
   }

   /**
    * Initialize the contents of the frame.
    */
   public void initialize() {
      frame = new JFrame();
      frame.setBounds(100, 100, 205, 194);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      JLabel lblTime = new JLabel("Time:");
      frame.getContentPane().add(lblTime, BorderLayout.WEST);

      final JLabel labeler = new JLabel("");
      frame.getContentPane().add(labeler, BorderLayout.CENTER);

      JButton btnNewButton = new JButton("New button");
      btnNewButton.addActionListener(new ActionListener() {
         Runnable r1 = new Runnable() {
            public void run() {
               while (a <= 10) {
                  a = a + 1;
                  labeler.setText(Integer.toString(a));
                  try {
                     Thread.sleep(1000);
                  } catch (InterruptedException e) {
                     // TODO Auto-generated catch block
                     e.printStackTrace();
                  }
               }

            }
         };

         public void actionPerformed(ActionEvent arg0) {
            Thread threder = new Thread(r1);
            threder.start();
            // liczniczek bla = new liczniczek();

         }
      });
      frame.getContentPane().add(btnNewButton, BorderLayout.SOUTH);

   }

   public void licznik() {
      while (a < 60) {
         a = a + 1;
         labeler.setText(Integer.toString(a));
         try {
            Thread.sleep(1000);
         } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
         }
      }
   }
}

现在是我的问题。我想使用这样的代码:

             Runnable r1 = new Runnable(){
                public void run(){
                           licznik();

                }
            };

但是那不起作用。我必须怎么做才能分隔此代码?对不起,我的英语不好

Sierran。

java multithreading swing
5个回答
3
投票

在EDT期间切勿使用Thread#sleep(int),请确保只有该线程才可以正常工作(使用EDT阻止),>

Runnable r1 = new Runnable(){
   public void run(){
       licznik();
   }
};

与您将普通电话称为licznik();相同,这是错误的,您必须用这种方式包装

Runnable r1 = new Runnable(){
   public void run(){
       labeler.setText(Integer.toString(a));    
   }
};

但是再次没有Thread#sleep(int),您有三个选择

1)将Thread更改为javax.swing.Timer

2)将Thread更改为Runnable#Thread,可以在其中延迟Thread#sleep(int),但是输出到GUI的必须是

Runnable r1 = new Runnable(){
   public void run(){
       labeler.setText(Integer.toString(a));    
   }
};

3)使用SwingWorker,其中输出在EDT中,您也可以使用Thread#sleep(int)

示例Thread#sleep(int) during EDT

put all together

编辑

  • 请勿将保留字用作类,方法,变量,无论编程语言中的任何名称(表示cos

  • 您的代码通过实现我在此处发布的所有三个选项,


2
投票

您的意思是“它不起作用”?这个对我有用。您如何尝试使用此代码,以及在运行它时遇到什么错误或问题?我自己,但是我将使用SwingWorker,并通过SwingWorker的发布/处理方法对设置JLabel的文本。要了解有关如何使用此功能的更多信息,请参见本教程:Concurrency in Swing


1
投票

我收集到您希望函数licznik()在单独的线程中运行。您创建了一个Runnable,但是您需要做更多的事情才能使其run()方法执行。有两种方法可以做到这一点:


1
投票

Runnable接口没有licznik()方法。您可以使用licznik()方法创建实现Runnable的类。


0
投票

查看https://github.com/greggwon/Ham下的GitHub。查看https://github.com/greggwon/Ham/blob/master/SwingUtil/src/org/wonderly/swing/ComponentUpdateThread.java中的源代码,以了解如何将整个细节打包到一个使用匿名内部类完成工作的类中。现在可以将其更改为lambda,但是我已经有好几年没有使用Java了,因此也没有进行更改。

© www.soinside.com 2019 - 2024. All rights reserved.