Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如果需要从主类调用Stage,如何拆分类文件上的JavaFX节点?_Java_Javafx - Fatal编程技术网

如果需要从主类调用Stage,如何拆分类文件上的JavaFX节点?

如果需要从主类调用Stage,如何拆分类文件上的JavaFX节点?,java,javafx,Java,Javafx,我非常喜欢从main方法调用类/对象。这样,我在main方法中没有完整的代码(这感觉不像是面向对象编程) 现在我有了一个简单的代码,它使用JavaFX画了一条线。线是一个节点,位于场景内部。但这都是主要的方法 我的主课叫示例。它包含完整的代码 我试过: public static LineClass extends Example 然后我在那里放了适当的代码。编译器不允许我编译它,因为需要从main方法调用launch()。然后我做了编译器要求我做的事情,但它只会发现更多的错误 我的代码(工作

我非常喜欢从main方法调用类/对象。这样,我在main方法中没有完整的代码(这感觉不像是面向对象编程)

现在我有了一个简单的代码,它使用JavaFX画了一条线。线是一个节点,位于场景内部。但这都是主要的方法

我的主课叫示例。它包含完整的代码

我试过:

public static LineClass extends Example
然后我在那里放了适当的代码。编译器不允许我编译它,因为需要从main方法调用launch()。然后我做了编译器要求我做的事情,但它只会发现更多的错误

我的代码(工作时):


如何拆分更多类的代码(假设每个类都有自己的.class文件)?我的目标是将行对象/节点的JavaFX代码(图形)放在不同的.class文件中,而不是将所有这些代码放在main方法中,以避免混乱。提前感谢。

只需创建另一个具有所需功能的类,并从
start()
实例化它即可

然后

import javafx.application.Application ;
import javafx.scene.stage.Stage ;
import javafx.scene.Scene ; 

public class DrawingLine extends Application{ 
   @Override 
   public void start(Stage stage) { 

      LineClass lc = new LineClass();

      //Creating a Scene 
      Scene scene = new Scene(lc.getView(), 600, 300); 

      //Setting title to the scene 
      stage.setTitle("Sample application"); 

      //Adding the scene to the stage 
      stage.setScene(scene); 

      //Displaying the contents of a scene 
      stage.show(); 
   }      
   public static void main(String args[]){ 
      launch(args); 
   } 
} 

谢谢@James\u D,但这段代码无法编译。我测试了Eclipse和NetBeans。您得到了什么编译错误?对我来说很好,是的。我还包括了“packagedrawingline;”(我承认我错过了它),但它仍然返回错误。可能我的IDE配置不正确?然而,我所讨论的代码编译成功了。@PeterAnson,这些错误是不同的。所以我猜你之前没有添加导入。您似乎仍然缺少对
Parent
@PeterAnson的导入,真的,您是否费心阅读错误消息?父项的导入位置在哪里?
import javafx.scene.Group ;
import javafx.scene.Parent ;
import javafx.scene.shape.Line ;

public class LineClass {

    private final Group root ;

    public LineClass() {

        //Creating a line object 
        Line line = new Line(); 

        //Setting the properties to a line 
        line.setStartX(100.0); 
        line.setStartY(150.0); 
        line.setEndX(500.0); 
        line.setEndY(150.0); 

        root = new Group(line); 
    }

    public Parent getView() {
        return root ;
    }
}
import javafx.application.Application ;
import javafx.scene.stage.Stage ;
import javafx.scene.Scene ; 

public class DrawingLine extends Application{ 
   @Override 
   public void start(Stage stage) { 

      LineClass lc = new LineClass();

      //Creating a Scene 
      Scene scene = new Scene(lc.getView(), 600, 300); 

      //Setting title to the scene 
      stage.setTitle("Sample application"); 

      //Adding the scene to the stage 
      stage.setScene(scene); 

      //Displaying the contents of a scene 
      stage.show(); 
   }      
   public static void main(String args[]){ 
      launch(args); 
   } 
}