javafx中同时有2个或多个线程
我正在制作javaFX动画,我上了以下课程:javafx中同时有2个或多个线程,java,multithreading,javafx,Java,Multithreading,Javafx,我正在制作javaFX动画,我上了以下课程: public class Visual extends javax.swing.JFrame implements Runnable { private static final int JFXPANEL_WIDTH_INT = 360; private static final int JFXPANEL_HEIGHT_INT = 550; private static JFXPanel fxContainer; p
public class Visual extends javax.swing.JFrame implements Runnable {
private static final int JFXPANEL_WIDTH_INT = 360;
private static final int JFXPANEL_HEIGHT_INT = 550;
private static JFXPanel fxContainer;
private static Train train;
private static AtomicBoolean running;
public Visual(Train t) {
Thread thread = new Thread(this);//new thread start
thread.start();
running = new AtomicBoolean(true);
train = t;
this.setSize(new Dimension(JFXPANEL_WIDTH_INT, JFXPANEL_HEIGHT_INT));
this.setTitle(train.getId());
this.setResizable(false);
fxContainer = new JFXPanel();
fxContainer.setPreferredSize(new Dimension(JFXPANEL_WIDTH_INT, JFXPANEL_HEIGHT_INT));
add(fxContainer, BorderLayout.CENTER);
this.setVisible(true);
addWindowListener(exitListener);
}
@Override
public void run() {
while (train.getRoute() == null && running.get()) {
try {
Thread.sleep(250);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
}
while (running.get()) {
createScene(train);
try {
Thread.sleep(250);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
//Logger.logException(ex, "");
}
}
}
private static int nbSectors = 0;
private void createScene(Train t) {
if (t.getRoute() != null) {
nbSectors = 0;
for (Block s : t.getRoute().getBlocks()) {
nbSectors += s.getSectors().size();
}
System.out.println("number of sectors : " + t.getId());
Group root = new Group();
Scene scene = new Scene(root, 350, 550, Color.ALICEBLUE);
Line l = new Line(100, 70, 100, 450);
l.setStrokeWidth(4);
l.setStrokeLineCap(StrokeLineCap.ROUND);
int nbStations = t.getLine().stations.size() - 1;
int m = 0;
boolean skipped = false;
for (Block b : t.getRoute().getBlocks()) {
for (Sector s : b.getSectors()) {
if (s.isOccupied()) {
root.getChildren().add(new Circle(100, 70 + m * (380. / nbSectors), 20, Color.RED));
skipped = true;
break;
}
if (skipped) {
break;
}
m++;
}
m++;
}
int i = 0;
// System.out.println("nb de lignes : "+nbLines);
for (Station station : t.getLine().stations) {
root.getChildren().add(new Text(130, 70 + i * (380 / nbStations), station.getId()));
root.getChildren().add(new Circle(100, 70 + i * (380 / nbStations), 10, Color.BLACK));
i++;
}
for (int j = 0; j < nbSectors; j++) {
root.getChildren().add(new Line(95, 70 + j * (380 / nbSectors), 105, 70 + j * (380 / nbSectors)));
}
root.getChildren().add(l);
fxContainer.setScene(scene);
}
}
public final void stop() {
running.set(false);
}
WindowListener exitListener = new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
stop();
}
};
}
公共类Visual extends javax.swing.JFrame实现可运行{
专用静态最终int JFXPANEL_WIDTH_int=360;
专用静态最终int JFXPANEL_HEIGHT_int=550;
私有静态JFXPanel fxContainer;
私人静态列车;
私有静态原子运行;
公众视觉(t列){
线程线程=新线程(this);//新线程开始
thread.start();
运行=新原子布尔值(真);
列车=t;
此.setSize(新尺寸(JFXPANEL_WIDTH_INT,JFXPANEL_HEIGHT_INT));
this.setTitle(train.getId());
此参数为.setresizeable(false);
fxContainer=newjfxpanel();
setPreferredSize(新尺寸(JFXPANEL_WIDTH_INT,JFXPANEL_HEIGHT_INT));
添加(fxContainer,BorderLayout.CENTER);
此.setVisible(true);
addWindowListener(exitListener);
}
@凌驾
公开募捐{
while(train.getRoute()==null&&running.get()){
试一试{
睡眠(250);
}捕获(中断异常例外){
Thread.currentThread().interrupt();
}
}
(运行.get()时){
创建场景(火车);
试一试{
睡眠(250);
}捕获(中断异常例外){
Thread.currentThread().interrupt();
//Logger.logException(例如“”);
}
}
}
私有静态部门=0;
私家车(t列){
如果(t.getRoute()!=null){
nbc=0;
对于(块s:t.getRoute().getBlocks()){
nbSectors+=s.getSectors().size();
}
System.out.println(“扇区数:+t.getId());
组根=新组();
场景=新场景(root,350,550,Color.ALICEBLUE);
线l=新线(100、70、100、450);
l、 设定行程宽度(4);
l、 固定行程盖(行程盖圆形);
int nbStations=t.getLine().stations.size()-1;
int m=0;
布尔值=假;
对于(块b:t.getRoute().getBlocks()){
对于(扇区s:b.getSectors()){
如果(s.isOccupied()){
root.getChildren().add(新的圆圈(100,70+m*(380./nbSectors),20,Color.RED));
跳过=真;
打破
}
如果(跳过){
打破
}
m++;
}
m++;
}
int i=0;
//System.out.println(“nb对齐:+nbLines”);
用于(车站:t.getLine().stations){
root.getChildren().add(新文本(130,70+i*(380/nbStations),station.getId());
root.getChildren().add(新的圆圈(100,70+i*(380/nbStations),10,Color.BLACK));
i++;
}
对于(int j=0;j
例如,当我在GUI中执行一个newvisual(greenTrain)
时,一切都运行得很好。当我启动多个可视窗口时,会出现此问题。终端中没有错误,但只有最后创建的可视实例运行。之前的其他人什么也不做。我不明白为什么,因为每个人都应该在不同的线程中,不是吗
谢谢你的帮助;) 我认为没有足够的代码来确切地知道你为什么会得到你描述的行为;但是,您不能从后台线程修改UI。因此,至少应该在FX应用程序线程上执行
fxContainer.setScene(scene)
,使用带有javafx的Platform.runLater()
javax.swing.JFrame
?您正在混合Swing和javaFX。不确定GUI线程在这种情况下是如何共存的。所以我应该在run中添加pltaform.runlater?是的,AxelH有点奇怪,我知道是的,只需将UI代码(而不是线程。sleep(…)
)包装在平台中。runlater(…)
。