gpt4 book ai didi

java - 为什么这个简单的循环会导致我的 JFrame 出现问题行为

转载 作者:行者123 更新时间:2023-12-01 21:14:28 26 4
gpt4 key购买 nike

我正在做 Java Exposure 教科书的作业,该教科书写于 2007 年。这本书包含一些代码,我通常会更新这些代码以使用一些更新的功能(只是基本的东西)。然而,在这方面我遇到了一个问题。我想做的就是替换 showsetVisible(true)并更改 FrameJFrame并添加 gfx.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 。但是,我注意到这实际上不会导致窗口关闭。如果我点击很多次,也许 1/30 次尝试就会关闭。如果我将延迟从 10 减少到 1,它通常会在 2 次尝试内关闭。这当然让我相信delay方法导致了这种不稳定的行为。我试过Thread.sleep ,但这当然行不通。 是否有任何简单的方法来获取此代码,以便当我点击关闭按钮时框架将关闭?如果没有,那么不那么简单的方法是什么?

这是代码:

// Lab30st.java
// The Screen Saver Program
// Student Version


import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.JOptionPane;


public class Lab30st
{
public static void main(String args[])
{
GfxApp gfx = new GfxApp();
gfx.setSize(800,600);
gfx.addWindowListener(new WindowAdapter() {public void
windowClosing(WindowEvent e) {System.exit(0);}});
gfx.show();
}
}


class GfxApp extends Frame
{

private int circleCount, circleSize;

public GfxApp()
{
circleCount = 50;
circleSize = 30;
}


class Coord
{
private int xPos;
private int yPos;

public Coord(int x, int y)
{
xPos = x;
yPos = y;
}
}

public void paint(Graphics g)
{
int incX = 5;
int incY = 5;
int diameter = 30;
int timeDelay = 10;
Circle c = new Circle(g,diameter,incX,incY,timeDelay);
for (int k = 1; k <= 2000; k++)
{
c.drawCircle(g);
c.hitEdge();
}

}
}



class Circle
{
private int tlX; // top-left X coordinate
private int tlY; // top-left Y coordinate
private int incX; // increment movement of X coordinate
private int incY; // increment movement of Y coordinate
private boolean addX; // flag to determine add/subtract of increment for X
private boolean addY; // flag to determine add/subtract of increment for Y
private int size; // diameter of the circle
private int timeDelay; // time delay until next circle is drawn



public Circle(Graphics g, int s, int x, int y, int td)
{
incX = x;
incY = y;
size = s;
addX = true;
addY = false;
tlX = 400;
tlY = 300;
timeDelay = td;
}

public void delay(int n)
{
long startDelay = System.currentTimeMillis();
long endDelay = 0;
while (endDelay - startDelay < n)
endDelay = System.currentTimeMillis();
}

public void drawCircle(Graphics g)
{
g.setColor(Color.blue);
g.drawOval(tlX,tlY,size,size);
delay(timeDelay);
if (addX)
tlX+=incX;
else
tlX-=incX;
if (addY)
tlY+=incY;
else
tlY-=incY;
}


public void newData()
{
incX = (int) Math.round(Math.random() * 7 + 5);
incY = (int) Math.round(Math.random() * 7 + 5);
}

public void hitEdge()
{
boolean flag = false;
if (tlX < incX)
{
addX = true;
flag = true;
}
if (tlX > 800 - (30 + incX))
{
addX = false;
flag = true;
}
if (tlY < incY + 30) // The +30 is due to the fact that the title bar covers the top 30 pixels of the window
{
addY = true;
flag = true;
}
if (tlY > 600 - (30 + incY))
{
addY = false;
flag = true;
}
if (flag)
newData();
}

}

最佳答案

您正在“卡住”事件调度线程

public void delay(int n)
{
long startDelay = System.currentTimeMillis();
long endDelay = 0;
while (endDelay - startDelay < n)
endDelay = System.currentTimeMillis();
}

这意味着尝试发生的所有其他事情(例如关闭窗口)必须等到线程从“ sleep ”状态中出来。基本上你不应该在 EDT 中进行延迟,它应该在不同的线程上,然后要求 EDT 线程更新。

您的“忙等待”延迟也可能会导致其他问题。您可以使用Thread.sleep()

来改善行为

参见Java Event-Dispatching Thread explanation

关于java - 为什么这个简单的循环会导致我的 JFrame 出现问题行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40518412/

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