I want everytime i click on the button "bouton" to execute the function

boutonPane.Panel2(h, ....) which is supposed to display h circles. So i want 2 then 3 then 4, then 5... circles.

The problem is that it is not displaying the step with number 4. I see the function is called in the console but on the screen it does really 2, (press button) 3, (press button) 5, (press button)9. I dont see 4. I dont see 6,7,8.. Could you tell me what is the problem please? Here is the code:

 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(); 

          } 
      }
    }

Here is a the Panel class:

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

Try this:

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

What you did wrong was adding a new BoutonPane every time you clicked the button. The next time you clicked the button, you didn't click ONE button, but TWO buttons, adding two more boutonPanes, and two more buttons. This multiplies very quickly.

What I did was the following:

  • make boutonPane a class member variable
  • call window2() only once
  • create a method updateWindow2() for updating the circles. Call that method from window2() and actionPerformed().

其他提示

"But would you have an idea of another, correct, way to do what I want please?"

  • You should only have one panel for the circles. There's absolutely no need to keep creating new panel.

  • Use a List for Ellipse2D objects. Just loop through them in the paintComponent method.

  • When you want to add a new circle, just add a new Ellipse2D object to the List and call repaint()

Here's an example.

NOTE Accept Gijs Overvliet's answer, as his was the one that answered your problem. I just wanted to share some insight.

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();
            }
        });
    }
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top