gpt4 book ai didi

java - 从一个类获取字符串以写入前屏幕类

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

我正在创建一个空中交通管制系统,它会生成随机数量的燃料和随机数量的飞机。我已经完成了这两个元素,但问题是,在输出框中,它全部显示并完美运行。我的问题是,我告诉它,如果没有飞机进来,则调用字符串“noplanes”,如果有飞机,则调用“KLM”。我无法让它用所有前端写入主类。

当我使用 Netbeans 拖放前端时,我编辑了屏幕中的一些代码:

enter public void StartSimulation()
{
Start pointer = new Start();
Plane incoming = new Plane();

//Needs a condition in here that checks if the plane and fuel has been
//Generated and also loop to keep up with the constant generated planes and fuel
jTextArea1.setText(incoming.planeName);

我已经尝试过,条件如下:

if (incoming.nextPlaneLanding != 167) 这会在输出框中一遍又一遍地生成第一个内容。我还尝试在飞机类中设置 boolean 值,但同样,即使周围存在以下条件,它也没有效果。 if (incoming.completed = true)

这是我飞机舱里的东西:

class Plane 
extends TimerTask
{
public int nextPlaneLoop = 0;
public int planes;
public int fuel;
public String planeName;
@Override
public void run()
{
if(nextPlaneLoop <=167)
{
//Currently only running 1 or 0 planes...
planes = (int) (Math.random()*((2-1)+1));
System.out.println("Random generated plane amount: "+planes);
System.out.println("Method called, one whole day loop");
//Adds to the plane in the airspace loop
nextPlaneLoop++;
System.out.println("Loop incrementing: "+nextPlaneLoop);

if(planes == 0)
{
System.out.println("No fuel is required as no planes are coming in");
planeName = "No incoming plane";
System.out.println("Planes name is: "+planeName);
System.out.println(" ");
}

else
{
//Amount of fuel
fuel = 30 + (int)(Math.random()*((120-30)+1));
System.out.println("Random fuel: "+fuel);
planeName = "KLM AirFrance";
System.out.println("Planes name is: "+planeName);
System.out.println(" ");
}
}
else
{
this.cancel();
System.out.println("Not in loop any more. End of day");
}
}
}

任何人都可以建议如何让名称显示在屏幕上,以便我可以尝试将它们添加到实际机场类别的队列中。

最佳答案

我认为Observer模式可能适合你。在 Java 中,这是通过 Observer 实现的和 Observable .

关于java - 从一个类获取字符串以写入前屏幕类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10042336/

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