什么是javafx场景对象中的根节点?

时间:2016-11-19 05:11:05

标签: java

我尝试创建一个场景对象以在其组件上应用css样式,但看起来我在创建场景对象时遇到了麻烦。 编译器说:root不能为null。 请问任何提示?

这是我的代码:

导入javafx.scene.Parent; import javafx.scene.Scene;

/ *  *要更改此许可证标题,请在“项目属性”中选择“许可证标题”。  *要更改此模板文件,请选择“工具”|模板  *并在编辑器中打开模板。  * /

/ **  *  * @author Ahmad  * / 公共类NewJFrame扩展了javax.swing.JFrame {

private Parent root;
private Parent Parent;

/**
 * Creates new form NewJFrame
 */
public NewJFrame() {
    initComponents();
}

/**
 * This method is called from within the constructor to initialize the form.
 * WARNING: Do NOT modify this code. The content of this method is always
 * regenerated by the Form Editor.
 */
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {

    jButton1 = new javax.swing.JButton();
    jTextField1 = new javax.swing.JTextField();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    jButton1.setText("jButton1");

    jTextField1.setText("jTextField1");

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(jButton1)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
            .addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 297, Short.MAX_VALUE)
            .addContainerGap())
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jButton1)
                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addContainerGap(266, Short.MAX_VALUE))
    );

    pack();
}// </editor-fold>                        

/**
 * @param args the command line arguments
 */
public static void main(String args[]) {
    /* Set the Nimbus look and feel */
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
    /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
     * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */
    try {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>

    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new NewJFrame().setVisible(true);
        }
    });
}

// Variables declaration - do not modify                     
private javax.swing.JButton jButton1;
private javax.swing.JTextField jTextField1;
// End of variables declaration                   


Scene scene = new Scene(root, 400, 300);

}

1 个答案:

答案 0 :(得分:0)

您的代码无法运行,因为您未初始化root对象并将JavaFXSwing源代码混合在一起。 JavaFXSwing是不同的GUI框架。

如果您想创建Swing应用程序,可以按照本教程进行操作:http://beginnersbook.com/2015/07/java-swing-tutorial/

您可以在此处找到JavaFX示例应用程序: http://docs.oracle.com/javafx/2/get_started/hello_world.htm

或者,您可以使用JavaFX混合SwingJFXPanel(Swing应用程序中的JavaFX,请参阅https://docs.oracle.com/javase/8/javafx/interoperability-tutorial/swing-fx-interoperability.htm) 和SwingNode(JavaFX应用程序中的Swing,请参阅http://docs.oracle.com/javafx/8/embed_swing/jfxpub-embed_swing.htm)。