📜  JavaFX |矩形和圆角矩形示例

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

JavaFX |矩形和圆角矩形示例

Rectangle 类是 JavaFX 的一部分。 Rectangle 类创建一个具有指定宽度、高度和位置的矩形。
默认情况下,矩形有尖角,但边缘可以通过应用弧高和宽度来圆角。

构造函数:

  1. Rectangle():创建一个空的矩形实例
  2. Rectangle(double w, double h):创建一个具有指定宽度和高度的矩形
  3. Rectangle(double x, double y, double w, double h):创建一个具有指定宽高和位置的矩形
  4. Rectangle(double w, double h, Paint f) : 创建一个指定宽度和高度的矩形并填充

常用方法

methodexplanation
getArcHeight()returns the arc height of the rectangle
getArcWidth()returns the arc width of the rectangle
getHeight()returns the height of the rectangle
getWidth()returns the width of the rectangle
getX()Gets the value of the property x.
getY()Gets the value of the property y.
setArcHeight(double v)sets the arc height of the rectangle
setArcWidth(double v)sets the arc width of the rectangle
setHeight(double value)sets the height of the rectangle
setWidth(double value)sets the width of rectangle
setX(double value)set the x coordinate of position of rectangle
setY(double value)set the y coordinate of position of rectangle

示例程序来说明 Rectangle 类的使用

该程序创建一个名称为 rectangle 的 Rectangle(位置的坐标以及高度和宽度作为参数传递)。矩形将在场景内创建,而场景又将托管在舞台内。函数setTitle() 用于为舞台提供标题。然后创建一个组,并附加圆圈。该组已连接到现场。最后调用 show() 方法显示最终结果。

程序创建一个矩形并将其添加到场景中

Java
// Java Program to create a rectangle and add it to the scene
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.control.*;
import javafx.stage.Stage;
 
import javafx.scene.Group;
public class Rectangle_0 extends Application {
 
    // launch the application
    public void start(Stage stage)
    {
        // set title for the stage
        stage.setTitle("creating Rectangle");
 
        // create a rectangle
        Rectangle rectangle = new Rectangle(100.0d, 100.0d, 120.0d, 80.0d);
 
        // create a Group
        Group group = new Group(rectangle);
 
        // create a scene
        Scene scene = new Scene(group, 500, 300);
 
        // set the scene
        stage.setScene(scene);
 
        stage.show();
    }
 
    public static void main(String args[])
    {
        // launch the application
        launch(args);
    }
}


Java
// Java Program to create a rounded rectangle
// and set a fill and add it to the scene
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.control.*;
import javafx.stage.Stage;
 
import javafx.scene.Group;
public class Rectangle_1 extends Application {
 
    // launch the application
    public void start(Stage stage)
    {
        // set title for the stage
        stage.setTitle("creating Rectangle");
 
        // create a rectangle
        Rectangle rectangle = new Rectangle(100.0d, 100.0d, 120.0d, 80.0d);
 
        // set fill for rectangle
        rectangle.setFill(Color.BLUE);
 
        // set rounded corners
        rectangle.setArcHeight(10.0d);
        rectangle.setArcWidth(10.0d);
 
        // create a Group
        Group group = new Group(rectangle);
 
        // create a scene
        Scene scene = new Scene(group, 500, 300);
 
        // set the scene
        stage.setScene(scene);
 
        stage.show();
    }
 
    public static void main(String args[])
    {
        // launch the application
        launch(args);
    }
}


输出

程序创建一个圆角矩形并设置填充并将其添加到场景中

该程序创建一个名称为 rectangle 的 Rectangle(位置的坐标以及高度和宽度作为参数传递)。圆角将使用 setArcHeight() 和 setArcWidth()函数设置。矩形的填充将使用 setFill()函数设置。矩形将在场景内创建,而场景又将托管在舞台内。函数setTitle() 用于为舞台提供标题。然后创建一个组,并附加圆圈。该组已连接到现场。最后调用 show() 方法显示最终结果。

Java

// Java Program to create a rounded rectangle
// and set a fill and add it to the scene
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.control.*;
import javafx.stage.Stage;
 
import javafx.scene.Group;
public class Rectangle_1 extends Application {
 
    // launch the application
    public void start(Stage stage)
    {
        // set title for the stage
        stage.setTitle("creating Rectangle");
 
        // create a rectangle
        Rectangle rectangle = new Rectangle(100.0d, 100.0d, 120.0d, 80.0d);
 
        // set fill for rectangle
        rectangle.setFill(Color.BLUE);
 
        // set rounded corners
        rectangle.setArcHeight(10.0d);
        rectangle.setArcWidth(10.0d);
 
        // create a Group
        Group group = new Group(rectangle);
 
        // create a scene
        Scene scene = new Scene(group, 500, 300);
 
        // set the scene
        stage.setScene(scene);
 
        stage.show();
    }
 
    public static void main(String args[])
    {
        // launch the application
        launch(args);
    }
}

输出:

注意:以下程序可能无法在在线 IDE 中运行,请使用离线编译器

参考:
https://docs.oracle.com/javase/8/javafx/api/javafx/scene/shape/Rectangle.html