简体   繁体   English

javaFX-如何创建可调整大小的矩形?

[英]javaFX - How to create a resizable rectangle?

I want to produce a GUI desktop app and it should run on different systems well. 我想生产一个GUI桌面应用程序,它应该可以在不同的系统上很好地运行。 In first Step I want to create a rectangle that appears well in different screens like 1920*1080 and 800*600 . 在第一步中,我想创建一个在1920 * 1080和800 * 600等不同屏幕上都能很好显示的矩形。 This rectangle size in first system should be 900 * 500 and in second one its scale should be 500 * 350 (The Scales are just examples!) How can i define a rectangle that work in this way ? 在第一个系统中,此矩形大小应为900 * 500,在第二个系统中,其缩放比例应为500 * 350(“缩放”仅是示例!)我如何定义以这种方式工作的矩形?

The Rectange class in JavaFX package itself is a resizable rectangle already since you can change the width/height and the location as well. JavaFX包本身的Rectange类本身已经是可调整大小的矩形,因为您还可以更改宽度/高度和位置。 You just need to figure out the current resolution and change the size of it. 您只需要确定当前分辨率并更改其大小即可。

See Java Doc 请参阅Java文档

You are asking about Responsive Design .Below is an example of what you want to make.Although is not best solution,with this i mean it can be modified for better performance(I also have added some code to move the window if it is StageStyle.UNDECORATED Drag the Window to have see this): 您正在询问响应式设计。以下是您要制作的示例。虽然不是最佳解决方案,但我想可以对其进行修改以获得更好的性能(如果是StageStyle.UNDECORATED我还添加了一些代码来移动窗口StageStyle.UNDECORATED拖动窗口以查看此内容:

在此处输入图片说明

 import javafx.application.Application;
import javafx.scene.Cursor;
import javafx.scene.Scene;
import javafx.scene.input.MouseButton;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Color;
import javafx.stage.Screen;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

public class FX extends Application {

    int screenWidth = (int) Screen.getPrimary().getBounds().getWidth();
    int screenHeight = (int) Screen.getPrimary().getBounds().getHeight();

    Stage stage;
    Scene scene;

    int initialX;
    int initialY;

    @Override
    public void start(Stage s) throws Exception {

        // root
        BorderPane root = new BorderPane();
        root.setStyle("-fx-background-color:rgb(186,153,122,0.7); -fx-background-radius:30;");

        // Responsive Design
        int sceneWidth = 0;
        int sceneHeight = 0;
        if (screenWidth <= 800 && screenHeight <= 600) {
            sceneWidth = 600;
            sceneHeight = 350;
        } else if (screenWidth <= 1280 && screenHeight <= 768) {
            sceneWidth = 800;
            sceneHeight = 450;
        } else if (screenWidth <= 1920 && screenHeight <= 1080) {
            sceneWidth = 1000;
            sceneHeight = 650;
        }

        // Scene
        stage = new Stage();
        stage.initStyle(StageStyle.TRANSPARENT);
        scene = new Scene(root, sceneWidth, sceneHeight, Color.TRANSPARENT);

        // Moving
        scene.setOnMousePressed(m -> {
            if (m.getButton() == MouseButton.PRIMARY) {
                scene.setCursor(Cursor.MOVE);
                initialX = (int) (stage.getX() - m.getScreenX());
                initialY = (int) (stage.getY() - m.getScreenY());
            }
        });

        scene.setOnMouseDragged(m -> {
            if (m.getButton() == MouseButton.PRIMARY) {
                stage.setX(m.getScreenX() + initialX);
                stage.setY(m.getScreenY() + initialY);
            }
        });

        scene.setOnMouseReleased(m -> {
            scene.setCursor(Cursor.DEFAULT);
        });

        stage.setScene(scene);
        stage.show();
    }

    /**
     * Main Method
     * 
     * @param args
     */
    public static void main(String[] args) {
        launch(args);
    }

}

Are you happy ?:) 开心吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM