简体   繁体   English

Thread.exit()行:使用jfxpanel将javafx集成到swing中时出错

[英]Thread.exit() line:not available error in integrating javafx into swing using jfxpanel

I am trying to use fxml inside swing application. 我试图在swing应用程序中使用fxml。 The problem now I am facing is the jframe shows up but the javafx components coundn't be seen. 我现在面临的问题是jframe出现,但javafx组件不会被看到。 No error is given but when debugged, it gives "Thread.exist() line: not available" error. 没有给出错误但是在调试时,它给出了“Thread.exist()行:not available”错误。 I have the complete code here :- 我在这里有完整的代码: -

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;
        }
    }


}

You can't see the JFXPanel because you haven't added JFXPanel to the BottomPanel . 您无法看到JFXPanel因为您尚未将JFXPanel添加到BottomPanel Inside the constructor of BottomPanel , just add add(getjfxPanel()); BottomPanel的构造函数中,只需添加add(getjfxPanel());

Your constructor will look like this : 您的构造函数将如下所示:

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());
}

Note: After adding this you will be able to see just one Button . 注意:添加此功能后,您只能看到一个Button There is no problem with the code, you are adding Buttons to Pane , which is why they are on top of each other. 代码没有问题,你要向Pane添加按钮,这就是为什么它们彼此重叠。 Just replace your Pane with a HBox and you will be able to see all the buttons because HBox aligns controls in horizontal order, just what you want( may be ;) ) 只需用HBox替换你的Pane ,你就可以看到所有的按钮,因为HBox按照水平顺序对齐控件,只是你想要的(可能是;))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM