gpt4 book ai didi

java - actionPerformed 跳过一步

转载 作者:行者123 更新时间:2023-12-01 23:06:29 27 4
gpt4 key购买 nike

我希望每次单击按钮“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();

}
}
}

这是一个面板类:

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

}

}

最佳答案

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

  • 您应该只有一个圆圈面板。 绝对不需要继续创建新面板。

  • 使用列表来表示Ellipse2D对象。只需在 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();
}
});
}
}

关于java - actionPerformed 跳过一步,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22683155/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com