JavaFX窗体、控件常用属性设置

104 篇文章 5 订阅

01 设置鼠标在控件上显示图标:

Button控件调用Cursor的枚举类型创建鼠标图标:

Button showAlertButton = new Button("Show Alert");
showAlertButton.setCursor(Cursor.WAIT);

对Scene实例调用Cursor的静态方法cursor创建鼠标图标实例:

Cursor waitCur = Cursor.cursor("WAIT")
scene.setCursor(waitCur);

02 获取显示屏幕尺寸、设置主窗体位置、尺寸:

private Stage setScreenDetails(Stage stage) {
        // 获取显示屏尺寸。
        Rectangle2D rectangle2D = Screen.getPrimary().getBounds();
        double width = rectangle2D.getWidth();
        double height = rectangle2D.getHeight();

        // 设置主窗体坐标
        stage.setX(width / 5);
        stage.setY(height / 5);
        
        // 设置主窗体尺寸。
        stage.setWidth(width * 3 / 5);
        stage.setHeight(height * 3 / 5);
        stage.setResizable(true);
        stage.setMinWidth(300);
        stage.setMinHeight(400);
//        primaryStage.setMaxWidth(width / 2);
//        primaryStage.setMaxHeight(height / 2);

        return stage;
    }

03 设置控件的属性Effect,包括阴影,旋转,平移。

private void start02(Stage primaryStage) throws Exception {
        // 设置阴影。
        Button button = new Button("Close");
        button.setEffect(new DropShadow());
        
        // 无设置。
        Button button1 = new Button("Start");
        
        // 设置平移、旋转。
        CheckBox checkBox = new CheckBox("CheckBox Test");
        checkBox.setEffect(new DropShadow());
        checkBox.getTransforms().addAll(new Translate(150, 150), new Rotate(30));
        
        VBox root = new VBox(3);
        root.getChildren().addAll(button, button1, checkBox);
        
        Scene scene = new Scene(root);
        primaryStage.setScene(scene);
        primaryStage.setTitle("Testing LayoutBounds");
        primaryStage.show();
        
        // 打印控件属性。
        System.out.println("button(layoutBounds)  = " + button.getLayoutBounds());
        System.out.println("button(boundsInLocal) = " + button.getBoundsInLocal());

        System.out.println("button1(layoutBounds)  = " + button1.getLayoutBounds());
        System.out.println("button1(boundsInLocal) = " + button1.getBoundsInLocal());
        
        System.out.println("checkBox(layoutBounds)  = " + checkBox.getLayoutBounds());
        System.out.println("checkBox(boundsInLocal) = " + checkBox.getBoundsInLocal());
    }

运行结果:

04 设置控件的边框,边框类型、宽度、颜色。

能够设置边框属性的控件必须是类Region的实例或其子类的实例。

package javafx8.ch09;


import static javafx.scene.input.MouseEvent.MOUSE_CLICKED;

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.CheckBox;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Border;
import javafx.scene.layout.BorderStroke;
import javafx.scene.layout.BorderStrokeStyle;
import javafx.scene.layout.BorderWidths;
import javafx.scene.layout.CornerRadii;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;


/**
 * @copyright 2023-2022
 * @package   javafx8.ch09
 * @file      ConsumingEvents1.java
 * @date      2023-06-22 15:18
 * @author    qiao wei
 * @version   1.0
 * @brief     调用consume方法,终止Event Filter或Event Handler的事件链。
 * @history
 */
public class ConsumingEvents extends Application {
    
    public ConsumingEvents() {}
    
    @Override
    public void start(Stage primaryStage) throws Exception {
        // Initialize widget.
        Circle circle = new Circle (50, 50, 50);
        circle.setFill(Color.CORAL);
        Rectangle rect = new Rectangle(100, 100);
        rect.setFill(Color.TAN);
        
        // 初始化布局管理器控件root,设置控件root中控件距边框距离,设置控件间距离和空间root的边框属性。
        HBox root = new HBox();
        root.setPadding(new Insets(20, 30 , 50,100));
        root.setSpacing(20);
        root.setBorder(
            new Border(
                new BorderStroke(
                    Color.GREEN,
                    BorderStrokeStyle.SOLID,
                    new CornerRadii(10),
                    new BorderWidths(3)
                )
            )
        );
        
        // 初始化复选框控件consumeEventCombo,设置控件consumeEventCombo的边框属性。
        consumeEventCombo.setBorder(
            new Border(
                new BorderStroke(
                    Color.RED,
                    BorderStrokeStyle.DOTTED,
                    new CornerRadii(10),
                    new BorderWidths(10)
                )
            )
        );
        root.getChildren().addAll(circle, rect, consumeEventCombo);
        Scene scene = new Scene(root);

        // Register mouse-clicked event handlers to all nodes, except the rectangle and checkbox.
        EventHandler<MouseEvent> handler = event -> handleEvent(event);
        EventHandler<MouseEvent> circleMeHandler = event -> handleEventForCircle(event);

        // 使用方法addEventHandler()注册点击鼠标事件。事件处理顺序为Bubbling。
        primaryStage.addEventHandler(MOUSE_CLICKED, handler);
//        scene.addEventHandler(MOUSE_CLICKED, circleMeHandler);
        root.addEventHandler(MOUSE_CLICKED, handler);
//        root.addEventHandler(MOUSE_CLICKED, circleMeHandler);
//        scene.addEventHandler(MOUSE_CLICKED, handler);
        scene.addEventHandler(MOUSE_CLICKED, circleMeHandler);
//        circle.addEventHandler(MOUSE_CLICKED, circleMeHandler);
        circle.addEventHandler(MOUSE_CLICKED, handler);

        primaryStage.setScene(scene);
        primaryStage.setTitle("Consuming Events");
        primaryStage.show();
    }

    public static void main(String[] args) {
        try {
            Application.launch(ConsumingEvents.class, args);
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }

    private void handleEvent(MouseEvent event) {
        System.out.println("--------------------event handler");
        print(event);
        System.out.println("--------------------event handler\n");
    }

    private void handleEventForCircle(MouseEvent event) {
        print(event);
        
        // Consume event chain after the checkbox is selected
        if (consumeEventCombo.isSelected()) {
            System.out.println("event consume function^^^^^^^^^^^^^^^^^^^^^^^^^^");
            event.consume();
            System.out.println("event consume function^^^^^^^^^^^^^^^^^^^^^^^^^^\n");
        }
    }

    private void print(MouseEvent event) {
        String type = event.getEventType().getName();
        String source = event.getSource().getClass().getSimpleName();
        String target = event.getTarget().getClass().getSimpleName();
        
        // Get coordinates of the mouse cursor relative to the event source
        double x = event.getX();
        double y = event.getY();
        
        System.out.println(
            "Type = " + type +
            ", Target = " + target +
            ", Source = " + source +
            ", Location(" + x + ", " + y + ")"
        );
    }

    private final CheckBox consumeEventCombo = new CheckBox("Consume Mouse Click at Circle");
}

 运行结果:

  • 7
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
JavaFX提供了许多GUI组件,如Button、TextField、Label等。对于这些组件的自动化测试,可以使用JavaFX自带的测试工具,即TestFX。 TestFX是一个开源的JavaFX GUI测试框架,它提供了一系列API和工具,可以方便地测试各种JavaFX应用程序的GUI组件和交互行为。 使用TestFX进行自动化测试,需要编写一些测试用例代码,其中包括以下内容: 1. 导入TestFX相关的依赖库 2. 创建JavaFX应用程序场景(Scene)对象 3. 使用TestFX提供的API查找和操作GUI组件 4. 编写测试用例代码,验证应用程序的行为和功能 下面是一个简单的TestFX测试用例的示例代码: ```java import org.junit.jupiter.api.Test; import org.testfx.api.FxRobot; import org.testfx.api.FxToolkit; import org.testfx.framework.junit5.ApplicationTest; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.layout.StackPane; import javafx.stage.Stage; public class MyTest extends ApplicationTest { private Button button; @Override public void start(Stage stage) throws Exception { button = new Button("Click Me"); StackPane root = new StackPane(button); Scene scene = new Scene(root, 300, 200); stage.setScene(scene); stage.show(); } @Test public void testButtonClick() { FxRobot robot = new FxRobot(); robot.clickOn(button); // TODO: add assertion } @Override public void stop() throws Exception { FxToolkit.hideStage(); release(new KeyCode[] {}); release(new MouseButton[] {}); } } ``` 这个测试用例创建了一个包含一个Button组件的JavaFX场景,并测试了Button组件的点击事件。具体来说,它使用TestFX提供的FxRobot对象模拟用户点击Button组件,并在测试方法中添加了一个断言,以验证点击事件是否产生了预期的结果。 需要注意的是,为了使用TestFX进行自动化测试,需要在测试工程中添加TestFX相关的依赖库,例如: ```xml <dependency> <groupId>org.testfx</groupId> <artifactId>testfx-core</artifactId> <version>4.0.16-alpha</version> <scope>test</scope> </dependency> ``` 这个依赖库包含了TestFX的核心功能,可以方便地进行GUI组件的查找和操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值