actionPerformed跳过一步

时间:2014-03-27 09:24:21

标签: java swing graphics actionlistener paintcomponent

我希望每次点击“bouton”按钮执行功能

boutonPane.Panel2(h,....),它应该显示h个圆圈。所以我想2然后3然后4,然后5 ...圈。

问题是它没有显示数字4的步骤。我看到该功能在控制台中被调用,但在屏幕上它实际上是2,(按下按钮)3,(按下按钮)5,(按下按钮)9。我没看到4.我看不到6,7,8 ..你能告诉我有什么问题吗?这是代码:

 public class Window extends JFrame implements ActionListener {
        int lg = 1000; int lrg = 700;
        int h = 2;

        Panel b = new  Panel(); 

        private JButton btn = new JButton("Start");

        JButton bouton = new JButton();

        private JPanel container = new JPanel();

        public Window(){ 
            this.setTitle("Animation");
            this.setSize(300, 300);
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            this.setLocationRelativeTo(null);
            container.setBackground(Color.white);
            container.setLayout(new BorderLayout());
            JPanel top = new JPanel(); 

            btn.addActionListener(this); 
            top.add(btn);
            container.add(top);
            this.setContentPane(container);
            this.setVisible(true); 
      }

      public void Window2()
      {

            System.out.println("windows2");

            this.setTitle("ADHD");
            this.setSize(lg, lrg);
            this.setLocationRelativeTo(null); 

            bouton.addActionListener(this); 


            if(h<11)
            {
                Panel boutonPane = new Panel();
                boutonPane.Panel2(h, Color.BLUE ,lg, lrg, this.getGraphics());

                System.out.println("draw"+h);

                boutonPane.add(bouton);

                this.add(boutonPane);
                this.setContentPane(boutonPane);
                this.revalidate();
                this.repaint();

            }


            this.setVisible(true);  

      }




      public void actionPerformed(ActionEvent e) 
      { 
          if((JButton)e.getSource()==btn) 
          { 
              System.out.println("pressed0");
              Window2(); 

          } 
          if((JButton)e.getSource()==bouton) 
          { 
              h++;
              System.out.println("pressed"+h);
              Window2(); 

          } 
      }
    }

这是Panel类:

public class Panel extends JPanel
{ 

    int m;
    int i=1;

    int a=0, b=0, tremp=0;
    Color cc;
    int lgi, lrgi;
    int [] ta;
    int [] tb;

    Graphics gi;

    int u=0;

    Panel()
    {

    }

    public void Panel2(int n, Color c, int lg, int lrg, Graphics g){
        m=n;
        cc=c;
        gi=g;
        lgi=lg;
        lrgi=lrg;
        ta = new int [n]; ta[0]=0;
        tb = new int [n]; tb[0]=0;

    }

    public void paintComponent( final Graphics gr){

        gr.setColor(Color.red);

        for(int it=0; it<m;it++)
        {
            ta[it]=100*it;
            tb[it]=100*it;
            gr.fillOval(ta[it],tb[it], 150, 150);
        }

    }

    }

2 个答案:

答案 0 :(得分:2)

  

&#34;但是你会想到另一种正确的方法来做我想做的事吗?&#34;

  • 您应该只为圈子设置一个面板。 绝对无需继续创建新面板。

  • Ellipse2D个对象使用List。只需在paintComponent方法中循环使用它们。

  • 如果您想添加新圈子,只需向Ellipse2D添加新的List对象,然后致电repaint()

以下是一个例子。

注意接受Gijs Overvliet的回答,因为他是回答你问题的人。我只是想分享一些见解。

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Ellipse2D;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class EllipseList extends JPanel {

    private static final int D_W = 700;
    private static final int D_H = 500;
    private static final int CIRCLE_SIZE = 50;

    private List<Ellipse2D> circles;
    private double x = 0;
    private double y = 0;

    private CirclePanel circlePanel = new CirclePanel();

    public EllipseList() {
        circles = new ArrayList<>();

        JButton jbtAdd = createButton();
        JFrame frame = new JFrame();
        frame.add(jbtAdd, BorderLayout.NORTH);
        frame.add(circlePanel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    private JButton createButton() {
        JButton button = new JButton("Add");
        button.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                circles.add(new Ellipse2D.Double(x, y, CIRCLE_SIZE, CIRCLE_SIZE));
                x += CIRCLE_SIZE * 0.75;
                y += CIRCLE_SIZE * 0.75;
                circlePanel.repaint();
            }
        });
        return button;
    }

    public class CirclePanel extends JPanel {

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D)g;
            g2.setPaint(Color.RED);
            for (Ellipse2D circle : circles) {

                g2.fill(circle);
            }
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(D_W, D_H);
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new EllipseList();
            }
        });
    }
}

答案 1 :(得分:1)

试试这个:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Window extends JFrame implements ActionListener
{
   int lg = 1000;
   int lrg = 700;
   int h = 2;

   Panel b = new Panel();

   private JButton btn = new JButton("Start");

   JButton bouton = new JButton();

   private JPanel container = new JPanel();
   Panel boutonPane = new Panel();

   public Window()
   {
      this.setTitle("Animation");
      this.setSize(300, 300);
      this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      this.setLocationRelativeTo(null);
      container.setBackground(Color.white);
      container.setLayout(new BorderLayout());
      JPanel top = new JPanel();

      btn.addActionListener(this);
      top.add(btn);
      container.add(top);
      this.setContentPane(container);
      this.setVisible(true);
   }

   public void Window2()
   {

      System.out.println("windows2");

      this.setTitle("ADHD");
      this.setSize(lg, lrg);
      this.setLocationRelativeTo(null);

      bouton.addActionListener(this);

      if (h < 11)
      {

         boutonPane.Panel2(h, Color.BLUE, lg, lrg, this.getGraphics());

         System.out.println("draw" + h);

         boutonPane.add(bouton);

         this.add(boutonPane);
         this.setContentPane(boutonPane);
         updateWindow2();

      }

      this.setVisible(true);

   }

   public void updateWindow2()
   {
      boutonPane.Panel2(h, Color.BLUE, lg, lrg, this.getGraphics());
      this.revalidate();
      this.repaint();
   }

   public void actionPerformed(ActionEvent e)
   {
      if ((JButton) e.getSource() == btn)
      {
         System.out.println("pressed0");
         Window2();

      }
      if ((JButton) e.getSource() == bouton)
      {
         h++;
         System.out.println("pressed" + h);
         updateWindow2();

      }
   }

   public static void main(String[] args)
   {
      Test t = new Test();
   }
}

你做错了是每次点击按钮都添加一个新的BoutonPane。下次单击该按钮时,您没有单击“一个”按钮,而是单击两个按钮,再添加两个boutonPanes,再单击两个按钮。这很快就成倍增加。

我做的是以下内容:

  • 使boutonPane成为类成员变量
  • 只调用一次窗口2()
  • 创建方法updateWindow2()以更新圈子。从window2()和actionPerformed()。
  • 调用该方法