📜  JavaFX |举例说明

📅  最后修改于: 2022-05-13 01:58:09.582000             🧑  作者: Mango

JavaFX |举例说明

Line 是 JavaFX 的一部分。 Line 类表示二维空间中的一条线。
类的构造函数:

  1. Line() : 为 line 创建一个新实例
  2. Line(double startX, double startY, double endX, double endY) :创建一个具有指定起点和终点的新线

常用方法:

MethodExplanation
getEndX()returns the x coordinate for the end point
getEndY()returns the y coordinate for the end point
getStartX()returns the x coordinate for the start point
getStartY()returns the y coordinate for the start point
setEndX(double value)sets the x coordinate for the end point
setEndY(double value)sets the y coordinate for the end point
setStartX(double value)sets the x coordinate for the start point
setStartY(double value)sets the y coordinate for the start point

下面的程序说明了 Line 类:

  1. Java程序创建一条线,其起点和终点坐标作为参数传递:该程序创建一条由名称 line 指示的线(起点和终点作为参数传递)。 Line 将在场景中创建,而场景又将托管在舞台中。函数setTitle() 用于为舞台提供标题。然后创建一个组,并附加线。该组已连接到现场。最后调用 show() 方法显示最终结果。
Java
// Java program to create a line with starting
// and ending coordinates passed as arguments
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.shape.DrawMode;
import javafx.scene.layout.*;
import javafx.event.ActionEvent;
import javafx.scene.shape.Line;
import javafx.scene.control.*;
import javafx.stage.Stage;
import javafx.scene.Group;
 
public class line_0 extends Application {
 
    // launch the application
    public void start(Stage stage)
    {
         
        // set title for the stage
        stage.setTitle("creating line");
 
        // create a line
        Line line = new Line(10.0f, 10.0f, 200.0f, 140.0f);
 
        // create a Group
        Group group = new Group(line);
 
        // translate the line to a position
        line.setTranslateX(100);
        line.setTranslateY(100);
 
        // create a scene
        Scene scene = new Scene(group, 500, 300);
 
        // set the scene
        stage.setScene(scene);
 
        stage.show();
    }
 
    // Main Method
    public static void main(String args[])
    {
        // launch the application
        launch(args);
    }
}


Java
// Java program to create a line with starting
// and ending coordinates set using function
// setStartX(), setStartY() setEndX(),
// setEndY() function
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.shape.DrawMode;
import javafx.scene.layout.*;
import javafx.event.ActionEvent;
import javafx.scene.shape.Line;
import javafx.scene.control.*;
import javafx.stage.Stage;
import javafx.scene.Group;
 
public class line_1 extends Application {
 
    // launch the application
    public void start(Stage stage)
    {
        // set title for the stage
        stage.setTitle("creating line");
 
        // create a line
        Line line = new Line();
 
        // set starting position
        line.setStartX(10.0f);
        line.setStartY(10.0f);
 
        // set ending position
        line.setEndX(140.0f);
        line.setEndY(140.0f);
 
        // create a Group
        Group group = new Group(line);
 
        // translate the line to a position
        line.setTranslateX(100);
        line.setTranslateY(100);
 
        // create a scene
        Scene scene = new Scene(group, 500, 300);
 
        // set the scene
        stage.setScene(scene);
 
        stage.show();
    }
 
    // Main Method
    public static void main(String args[])
    {
         
        // launch the application
        launch(args);
    }
}


  1. 输出:

  1. Java程序使用函数setStartX()、setStartY()、setEndX()、setEndY()函数创建一条线的起点和终点坐标:该程序创建一条由名称 line 指示的线(起点和终点使用setEndX设置()、setEndY()、setStartX()、setStartY()函数)。 Line 将在场景中创建,而场景又将托管在舞台中。函数setTitle()用于为舞台提供标题。然后创建一个组,并附加线。组附加到场景。最后调用 show() 方法显示最终结果。

Java

// Java program to create a line with starting
// and ending coordinates set using function
// setStartX(), setStartY() setEndX(),
// setEndY() function
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.shape.DrawMode;
import javafx.scene.layout.*;
import javafx.event.ActionEvent;
import javafx.scene.shape.Line;
import javafx.scene.control.*;
import javafx.stage.Stage;
import javafx.scene.Group;
 
public class line_1 extends Application {
 
    // launch the application
    public void start(Stage stage)
    {
        // set title for the stage
        stage.setTitle("creating line");
 
        // create a line
        Line line = new Line();
 
        // set starting position
        line.setStartX(10.0f);
        line.setStartY(10.0f);
 
        // set ending position
        line.setEndX(140.0f);
        line.setEndY(140.0f);
 
        // create a Group
        Group group = new Group(line);
 
        // translate the line to a position
        line.setTranslateX(100);
        line.setTranslateY(100);
 
        // create a scene
        Scene scene = new Scene(group, 500, 300);
 
        // set the scene
        stage.setScene(scene);
 
        stage.show();
    }
 
    // Main Method
    public static void main(String args[])
    {
         
        // launch the application
        launch(args);
    }
}
  1. 输出:

注意:上述程序可能无法在在线 IDE 中运行。请使用离线编译器。
参考: https://docs.oracle.com/javase/8/javafx/api/javafx/scene/shape/Line.html