1

在此处输入图像描述

自 24 小时前以来,我一直在做这件事。我有一个自定义 jpanel,使用计时器在其上绘制一个简单的动画。我想要的是在皮卡丘和佐助之间的中心显示这个面板(直到现在我一直在尝试这样做),以便动画开始并仅在单击此按钮时发生。

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

public class App extends JFrame {
private static JPanel p53 = new JPanel();
private static JButton fire = new JButton("Attack");
private AttackFX attackfx = new AttackFX();

public App() {

fire.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent atk) {
                    p53.add(attackfx);
                    Timer timer1 = new Timer(800, new ActionListener() {
                        @Override
                        public void actionPerformed(ActionEvent ld2) {
                        }
                    });
                    timer1.start();
                    timer1.setRepeats(false);
            }
        });
}

public static void main(String[] a) {
App Battleframe = new App();
Battleframe.add(fire);
Battleframe.add(p53);
Battleframe.setResizable(false);
Battleframe.setTitle("OnFight.Combat_Arena_Beta");
Battleframe.setSize(381, 400);
Battleframe.setLocationRelativeTo(null);
Battleframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Battleframe.setVisible(true);
}

class AttackFX extends JPanel {

    private int xCoor = 1;

 public AttackFX() {
 Timer tm1 = new Timer(100, new TimerListener2());
 tm1.start();
 tm1.setRepeats(true);
 }

 @Override
 protected void paintComponent(Graphics g) {
 super.paintComponent(g);

 xCoor += 1;

 g.setColor(Color.cyan);
 g.fillOval(xCoor, 40, 8, 8);
 }

 class TimerListener2 implements ActionListener {
 @Override
 public void actionPerformed(ActionEvent e) {
 repaint();
 }
 }

}
}

在这件事上,我真的需要你的帮助。明天将介绍我正在制作的整个程序。这是完成它的最后一步。我已经尽我所能自己解决这个问题,但没有成功。

4

1 回答 1

1

这是一些工作代码,当单击按钮时会生成一个圆圈,并在达到某个 x 值后使其消失:

public class App extends JFrame implements ActionListener {

  private static final long serialVersionUID = 1L;

  public static void main(String[] a) {
    App app = new App();
    app.createControls();
  }

  public App() {
    setResizable(false);
    setTitle("OnFight.Combat_Arena_Beta");
    setSize(381, 400);
    setLocationRelativeTo(null);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLayout(new BorderLayout());
  }

  private void createControls() {
    JPanel upper = new JPanel();

    JButton fire = new JButton("Attack");
    fire.addActionListener(this);
    upper.add(fire);

    this.add(upper, BorderLayout.NORTH);
    setVisible(true);
  }

  /*
   * When the "Attack" button is pressed the time will start and the
   * circle will start painting.
   */
  @Override
  public void actionPerformed(ActionEvent e) {
    AttackFX attackfx = new AttackFX();

    this.add(attackfx, BorderLayout.CENTER);
    this.validate();
  }

  class AttackFX extends JPanel implements ActionListener {

    private static final long serialVersionUID = 1L;
    private int xCoor = 100;
    private Timer t;

    public AttackFX() {
        t = new Timer(10, this);
        t.start();
    }


    public void paintComponent ( Graphics graphics ) {
        super.paintComponent ( graphics );
        Graphics2D graphics2d = ( Graphics2D ) graphics;

        graphics.setColor ( Color.cyan );
        graphics2d.fillOval(xCoor, 40, 8, 8);
    }

    /*
     * The timer will fire an action every 10 milliseconds, moving
     * our circle by 1 each time.
     * 
     * I unfortunately had to hard code a value that the circle should stop at
     * but I am sure you can find a way around this.
     */
    @Override
    public void actionPerformed(ActionEvent e) {

        if (xCoor > 250) {
            t.stop();
            setVisible(false);
        }

        xCoor++;
        repaint();
    }
  }
}

我不知道 setVisible() 是不是最好的方法,但它比删除 JPanel 更安全。让我知道你的想法。

于 2013-10-20T10:26:39.067 回答