【问题标题】:Auto Moving Image with a close Button and button event in JAVAJAVA中带有关闭按钮和按钮事件的自动移动图像
【发布时间】:2013-04-03 04:48:58
【问题描述】:

我有这个代码。在此代码中,图像使用 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();
   }
}

【问题讨论】:

  • “我的代码取自这里:” 为了尽快获得更好的帮助,请发布SSCCE(此处,未链接)。
  • 你确定过去 11 年 Java 和 Swing 没有任何变化

标签: java image animation awt double-buffering


【解决方案1】:

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

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

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

如果你想让你的按钮工作,你需要在你的代码中添加一些东西。首先,您需要将“implements ActionListener”添加到您的类头中。

public class MyImage extends JFrame implements ActionListener {

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

@Override
public void actionPerformed(ActionEvent e){

}

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

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

if(exitLoop == true) break;

如果布尔值exitLoop 为真,这将从循环中退出(中断)。

【讨论】:

  • 请解决我的另一个问题...关闭操作工作正常但按钮事件不起作用..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-12
  • 1970-01-01
  • 2013-03-03
  • 2020-07-11
  • 1970-01-01
相关资源
最近更新 更多