2014-03-27 90 views
0

欲每次我点击按钮“布顿”来执行该功能的actionPerformed跳过步骤

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

    } 

    } 
+0

我希望现在编辑正确。有人有我的问题的答案吗? – user3460225

回答

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()调用该方法。
+0

它的工作!非常感谢!! – user3460225

+0

接受答案将不胜感激;) –

+0

我做了一个简单版本的面板,但与我真正使用它不起作用。我可以发送代码,但它使用项目的其他类和图片。你会建议我做什么?发送一切,但图片? – user3460225

2

“但是你会想出另一种正确的方法来做我想要的吗?

  • 您应该只有一个面板为界。有绝对不需要不断创建新面板。

  • 对于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(); 
      } 
     }); 
    } 
} 
+0

问题是我做了我的班级面板的简单版本,以便能够在网站上发布代码。实际上,即使Gijs回答,它也不适合我的真实面板。我应该怎么做?我可以向您发送我的真实面板的代码,但它使用该项目的其他类和图片。也许我可以给你发送该项目的文件夹? – user3460225