java - 如何将两个不同的任务放入线程中

标签 java multithreading swing paint

我有以下代码。在这段代码中,我有一个从左向右移动的图像和一个有事件的按钮。但我想把这两个任务放在线程中。使其能够正常工作。这段代码的问题是按钮事件在到达最右边的点之前不起作用。

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

class MyImage extends JFrame implements ActionListener
{
   static int xPixel = 20;
   Image myImage, offScreenImage;
   Graphics offScreenGraphics;
   JPanel p = new JPanel();
   Button btn = new Button("bun");
   JFrame f = new JFrame();


   public MyImage()
   {
      myImage = Toolkit.getDefaultToolkit().getImage("mywineshoplogo.jpg");
      setExtendedState(JFrame.MAXIMIZED_BOTH);
      this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setVisible(true);
      add(p);
      p.add(btn);
      moveImage();
      btn.addActionListener(this);
   }

   public void update(Graphics g)
   {
      paint(g);
   }

   public void paint(Graphics g)
   {
      int width = getWidth();
      int height = getHeight();
      if (offScreenImage == null)
      {
         offScreenImage = createImage(width, height);
         offScreenGraphics = offScreenImage.getGraphics();
      }
// clear the off screen image  
      offScreenGraphics.clearRect(0, 0, width + 1, height + 1);
// draw your image off screen  
      offScreenGraphics.drawImage(myImage, xPixel, 10, this);
// draw your image off screen  
// show the off screen image  
      g.drawImage(offScreenImage, 0, 0, this);
// show the off screen image  
   }

   void moveImage()   //left to right move
   {
  Thread hilo = new Thread() {

  public void run() {
 try {

      for (int i = 0; i < 530; i++)
      {

         xPixel += 1;
         repaint();
        // then sleep for a bit for your animation  
         try
         {
            Thread.sleep(4);
         } /* this will pause for 50 milliseconds */

         catch (InterruptedException e)
         {
            System.err.println("sleep exception");
         }
      }
  } //try
  catch (Exception ex) {
      // do something...
  }
  }
  };
  hilo.start();
   }

/*   void moveimg()   // right to left move
   {
      for (int i = 529; i > 0; i--)
      {
         if (i == 1)
         {
            moveImage();
         }
         xPixel -= 1;
         repaint();
// then sleep for a bit for your animation  
         try
         {
            Thread.sleep(40);
         } // this will pause for 50 milliseconds 

         catch (InterruptedException e)
         {
            System.err.println("sleep exception");
          }
      } 
   } */     

   public void actionPerformed(ActionEvent ae)
   {
      try
      {
         if (ae.getSource() == btn)
         {
            p.setBackground(Color.RED);
         }
      }
      catch (Exception e)
      {
         System.out.println("error");
      }
   }

   public static void main(String args[])
   {
      MyImage me = new MyImage();
   }
}

最佳答案

每当您要在 GUI 代码中编写 Thread.sleep 时,请停下来并引入一个在 Swing 的 Timer 上计划的任务。这正是您的代码所需要的:将每个更新安排为 Timer 上的单独计划任务。 Timer 非常简单易用,例如 this official Oracle tutorial .

关于java - 如何将两个不同的任务放入线程中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16480969/

相关文章:

java - 调用单独的 Java 文件来打开第二个 GUI 窗口

java - JPanel 在 JFrame 中不可见

java - 两个同步的 ScrollPanes 未对齐

java - java中优化的线程安全列表

java - 如何在我的应用程序中实现 Memcache

java - Java游戏滞后

java - 使用 vertx 在 mongoDB 中批量写入

java - 我如何在这里等待java代码中的所有线程?

ios - 通过重试将 NSOperation 子类化为 Internet 操作

java - 编写一个遵循 GridBagLayout 的 JPanel 子类?