gpt4 book ai didi

java - Thread.exit()行:not available error in integrating javafx into swing using jfxpanel

转载 作者:行者123 更新时间:2023-12-02 09:15:04 24 4
gpt4 key购买 nike

我正在尝试在 swing 应用程序中使用 fxml。我现在面临的问题是 jframe 显示,但 javafx 组件看不到。没有给出错误,但在调试时,它给出“Thread.exist() line: not available”错误。我这里有完整的代码:-

package nonResponsiveButtons;

import java.awt.Color;
import javafx.embed.swing.JFXPanel;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.Pane;
import javafx.application.Platform;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class NonResponsiveButtons extends JFrame {

public static void main(String[] args) {
new NonResponsiveButtons();
}

public NonResponsiveButtons(){

SwingUtilities.invokeLater(new Runnable() {

@Override
public void run() {
// TODO Auto-generated method stub
try{
new JFXPanel();
BottomPanel bottomPanel = new BottomPanel();
add(bottomPanel);
}
catch(Exception e){
System.out.println("Error in swing utilities thread :" + e.getMessage());
}

}
});

this.setSize(600, 600);
getContentPane().setLayout(null);
add(jPanel());


setVisible(true);
}

private JPanel jPanel(){
JPanel panel = new JPanel();
panel.setSize(600,500);
panel.setBackground(Color.blue);
return panel;
}

private class BottomPanel extends JPanel{
private JFXPanel jfxPanel;
private Pane scenePane;
private Button btn1;
private Button btn2;
private Button btn3;

private BottomPanel(){
setSize(600, 100);
setLocation(0, 500);
setLayout(null);



Platform.runLater(new Runnable(){
@Override
public void run(){
getScenePane().getChildren().addAll(getBtn1(),getBtn2(),getBtn3());
getjfxPanel().setScene(new Scene(getScenePane()));

}

});
}

private JFXPanel getjfxPanel(){
if(jfxPanel == null){
jfxPanel = new JFXPanel();
jfxPanel.setSize(600,200);
}
return jfxPanel;
}

private Pane getScenePane(){
if(scenePane == null){
scenePane = new Pane();
scenePane.setStyle("-fx-background-color:#666666");
}
return scenePane;
}

/*
* using getters will avoid :-
* 1. null pointer exceptions
* 2. standard coding format
* 3. makex programming felxible
*/
private Button getBtn1(){
if(btn1 == null){
btn1 = new Button();
btn1.setPrefSize(100, 50);
btn1.setLayoutX(80);
}
return btn1;
}
private Button getBtn2(){
if(btn2 == null){
btn2 = new Button();
btn2.setPrefSize(100, 50);
btn2.setLayoutX(80);
}
return btn2;
}
private Button getBtn3(){
if(btn3 == null){
btn3 = new Button();
btn3.setPrefSize(100, 50);
btn3.setLayoutX(80);
}
return btn3;
}
}


}

最佳答案

您看不到 JFXPanel,因为您尚未将 JFXPanel 添加到 BottomPanel。在 BottomPanel 的构造函数中,只需添加 add(getjfxPanel());

你的构造函数将如下所示:

private BottomPanel(){
setSize(600, 100);
setLocation(0, 500);
setLayout(null);

Platform.runLater(new Runnable(){
@Override
public void run(){
getScenePane().getChildren().addAll(getBtn1(),getBtn2(),getBtn3());
getjfxPanel().setScene(new Scene(getScenePane()));
}
});
add(getjfxPanel());
}

注意:添加此项后,您将只能看到一个按钮。代码没有问题,您正在将按钮添加到 Pane 中,这就是它们彼此重叠的原因。只需将 Pane 替换为 HBox,您就可以看到所有按钮,因为 HBox 按水平顺序对齐控件,这正是您想要的(可能是;))

关于java - Thread.exit()行:not available error in integrating javafx into swing using jfxpanel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25832726/

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