Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java程序在MAC上未正确运行_Java_Swing_Concurrency_Jframe - Fatal编程技术网

Java程序在MAC上未正确运行

Java程序在MAC上未正确运行,java,swing,concurrency,jframe,Java,Swing,Concurrency,Jframe,我最近开始使用mac进行开发,我遇到了一个奇怪的问题 以下面的程序为例: public class Driver { public static void main(String [ ] args) { SolarSystem SSpanel = new SolarSystem(600, 600); SSpanel.drawSolarObject(0, 0, 30, "YELLOW"); } } SolarSystem类扩展了JFrame,基本

我最近开始使用mac进行开发,我遇到了一个奇怪的问题

以下面的程序为例:

public class Driver {

    public static void main(String [ ] args) {
        SolarSystem SSpanel = new SolarSystem(600, 600);
        SSpanel.drawSolarObject(0, 0, 30, "YELLOW");
    }
}
SolarSystem类扩展了JFrame,基本上在创建新的SolarSystem时,它会生成一个相同大小的面板

drawSolarObjects基本上绘制了一个特定颜色和大小的圆。finishedDrawing实际上使对象显示在面板上

上面的示例确实有效,但我有更复杂的需求,需要将其放入while循环

这就是它变得奇怪的地方,如果我在windows计算机上用cmd运行下面的程序,它工作正常,并将黄色圆圈打印到屏幕上。在我的mac电脑上,添加这个while循环会导致它只创建面板,而不绘制黄色圆圈

public class Driver{

    public static void main(String [ ] args) {
        boolean oMove = true;
        SolarSystem SSpanel = new SolarSystem(600, 600);
        while(oMove){
            SSpanel.drawSolarObject(0, 0, 30, "YELLOW");
            SSpanel.finishedDrawing();
        }
    }
}
我在我的循环中放了一个指纹来检查它是否在循环中运行,这表明它确实在循环中运行

有人知道这是什么原因吗

我正在添加这些功能,以便您能更好地了解情况

SolarSystem构造器:

public SolarSystem(int width, int height)
{
    this.width = width;
    this.height = height;

    this.setTitle("The Solar System");
    this.setSize(width, height);
    this.setBackground(Color.BLACK);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setVisible(true);      
}
drawSolarObject函数:

public void drawSolarObject(double distance, double angle, double diameter, String col)
{
    Color colour = this.getColourFromString(col);
    double centreOfRotationX = ((double) width) / 2.0; 
    double centreOfRotationY = ((double) height) / 2.0; 

    double rads = Math.toRadians(angle);
    double x = (int) (centreOfRotationX + distance * Math.sin(rads)) - diameter / 2;
    double y = (int) (centreOfRotationY + distance * Math.cos(rads)) - diameter / 2;

    synchronized (this)
    {
        if (things.size() > 1000)
        {
            System.out.println("\n\n");
            System.out.println(" ********************************************************* ");
            System.out.println(" ***** Only 1000 Entities Supported per Solar System ***** ");
            System.out.println(" ********************************************************* ");
            System.out.println("\n\n");

            this.dispatchEvent(new WindowEvent(this, WindowEvent.WINDOW_CLOSING));
        }
        else
        {
            SolarObject t = new SolarObject((int)x, (int)y, (int)diameter, colour);
            things.add(t);
        }
    }
}
FinishedAddressing函数:

public void finishedDrawing()
{
    try
    {
        this.repaint();
        Thread.sleep(30);
    }
    catch (Exception e) { }

    synchronized (this)
    {
        things.clear();
    }
}

这一切都可以在windows PC上正常工作

您的代码可能会占用Swing事件线程,从而阻止它在GUI上绘图,并有效地冻结您的程序。相反,使用一个循环,而不是一个while循环来实现你的目标

e、 g

顺便说一句,我要去

SSpanel.drawSolarObject(0, 0, 30, "YELLOW");
SSpanel.finishedDrawing();

在我的计时器代码中,但它没有意义,因为此代码不是“动态”的,不会更改任何内容或执行任何动画。

谢谢您的回答。你让我想到检查finishedDrawing()函数(我没有编写这个函数),它确实有一个thread.sleep(30)。这可能是一个愚蠢的问题,不延迟它吗?@user1680768:您的Swing代码中不应该有可能在Swing事件线程上运行的
线程。sleep(…)
。相反,再次使用摆动计时器。如果你没有写这个方法,是谁写的?我正在学习java,这是一个基于任务的东西,有已经编写的类和一些关于如何使用它们的文档。我在windows电脑上一切正常,这让我觉得代码不是问题所在。不幸的是,我现在需要继续在我的mac上工作,但无法让它工作。我正在尝试实现摆动计时器,但迄今为止运气不佳。我添加了更多关于函数的信息。我感谢你的帮助。
SSpanel.drawSolarObject(0, 0, 30, "YELLOW");
SSpanel.finishedDrawing();