gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 02:52:42 24 4
gpt4 key购买 nike

我想制作一个 GUI 桌面应用程序,它应该可以在不同的系统上运行良好。在第一步中,我想创建一个在不同屏幕(如 1920*1080 和 800*600 )中显示良好的矩形。第一个系统中的矩形大小应为 900 * 500,第二个系统中的比例应为 500 * 350(比例只是示例!)我如何定义以这种方式工作的矩形?

最佳答案

您询问的是Responsive Design下面是您想要制作的示例。虽然不是最佳解决方案,但我的意思是可以对其进行修改以获得更好的性能(我还添加了一些代码来移动窗口,如果它是 StageStyle.UNDECORATED 拖动窗口即可看到此内容):

enter image description here

 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);
}

}

你是happy ?:)

关于javaFX - 如何创建可调整大小的矩形?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38175071/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com