java - JAVA中带有关闭按钮和按钮事件的自动移动图像

标签 java image animation awt double-buffering

我有这个代码。在此代码中,图像使用 moveImage 方法从左到右移动,并使用代码中的 moveimg 方法从右到左移动。我现在想要的是处理按钮事件。代码中有一个按钮,我希望当我单击该按钮时它应该完成其工作。但它没有做..这里的代码是:

    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
   {
      for (int i = 0; i < 530; i++)
      {

         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");
         }
      }
   }

/*   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();
   }
}

最佳答案

如果您希望程序在按下关闭按钮时退出,您需要做一些事情。首先,扩展Frame,扩展JFrame。然后,在构造函数中调用 setVisible() 或 moveImage() 之前,添加以下代码:

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

这使得当您按下框架上的关闭按钮时,JFrame 会调用 System.exit(0)。

如果您想让按钮正常工作,您需要在代码中添加一些内容。首先,您需要将“implements ActionListener”添加到类 header 中。

public class MyImage extends JFrame implements ActionListener {

然后,在类的主体中添加以下方法:

@Override
public void actionPerformed(ActionEvent e){

}

然后,在创建新按钮后,在构造函数中添加 yourButton.addActionListener(this); 这将创建一个监听器并将其注册到您的按钮上。每当您的按钮有一个操作(例如单击)时,它就会调用 actionPerformed 方法。

如果您想使用它来退出 for 循环,我建议添加一个以 false 开头的 boolean 值,但在单击按钮时设置为 true。然后在 for 循环中添加

if(exitLoop == true) break;

如果 boolean 值 exitLoop 为 true,则从循环中退出(中断)。

关于java - JAVA中带有关闭按钮和按钮事件的自动移动图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15941202/

相关文章:

image - 如何使用 ImageMagick 模糊/像素化图像的一部分,从而产生大像素?

javascript - 如何继承css动画?

java - EHCache-org.ehcache.StateTransitionException : Persistence directory already locked by another process

java - 在 hazelcast 和 Java 中以编程方式设置 Near Cache 验证 Near 缓存是否从本地缓存返回数据

java - 如何显示html :text using <logic:iterate> for List<String> in Struts1. 1

java - Wicket:从绝对路径加载图像

java - 处理程序和调度程序之间的区别

java - 当鼠标悬停时如何更改java中扩展JButton的背景

javascript - 将饼图动画延迟到视口(viewport)中

iOS 动画 2D 游戏