gpt4 book ai didi

Java 按钮不起作用

转载 作者:行者123 更新时间:2023-12-01 16:40:03 25 4
gpt4 key购买 nike

按钮正在触发事件,但未显示。它创建新的圆圈,但不绘制新的圆圈。有什么我没做的事吗? (第一个 Java 小程序)。这可能是我错过的一些愚蠢的东西..

import java.applet.*;


public class CircleApp extends Applet {

public circle[] circles;
public Button regen;

public CircleApp(){
}

public void init()
{
this.setSize(400, 400);
genCircles(getWidth(), getHeight());
regen = new Button("New Circles");
add(regen);
}

public boolean action(Event e, Object args)
{
genCircles(getWidth(), getHeight());
return true;
}

public void genCircles(int wid, int hei)
{
circles = new circle[20];
Random gen = new Random();
for (int i = 0; i < 20; i++)
{
int x = gen.nextInt(wid);
int y = gen.nextInt(hei);
int rx = Math.min(x, wid - x);
int ry = Math.min(y, hei - y);
int r = Math.min(rx, ry);

circles[i] = new circle(new Point(x, y), gen.nextInt(r));
}

for (int i = 0; i < 20; i++)
{
for (int j = i; j < 20; j++)
{
if (circles[i].intersects(circles[j]))
{
circles[i].intersects = true;
break;
}
}
}
}

public void paint(Graphics g)
{
for (int i = 0; i < 20; i++)
{
g.setColor((circles[i].intersects) ? Color.blue : Color.magenta);
g.drawOval(circles[i].location.x,
circles[i].location.y,
circles[i].radius,
circles[i].radius);
}
}
}

import java.awt.Point;


public class circle {
public Point location;
public int radius;
public boolean intersects;

public circle(Point h, int radius)
{
this.location = h;
this.radius = radius;
}
public circle()
{
this(new Point(0, 0), 1);
}
public boolean intersects(circle other)
{
return (this.location.distance(other.location) <
(double)(this.radius + other.radius));
}
}

感谢您的帮助!!

最佳答案

您需要调用repaint()生成新圆圈后重新绘制它们。

另外,与您的问题没有直接关系,但是action()已弃用 - 使用 action listener而是在按钮上。

关于Java 按钮不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4778463/

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