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

时间:2014-09-14 11:27:33

标签: java multithreading swing javafx jframe

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

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


}

1 个答案:

答案 0 :(得分:0)

您无法看到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());
}

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