javaFX - 如何创建可调整大小的矩形?
javaFX - How to create a resizable rectangle?
我想制作一个 GUI 桌面应用程序,它应该 运行 在不同的系统上运行良好。在第一步中,我想创建一个在不同屏幕(如 1920*1080 和 800*600)中显示良好的矩形。第一个系统中的这个矩形大小应该是 900 * 500,第二个系统中它的比例应该是 500 * 350(比例只是例子!)我如何定义一个以这种方式工作的矩形?
JavaFX 包中的 Rectange
class 本身就是一个可调整大小的矩形,因为您也可以更改 width/height 和位置。你只需要弄清楚当前的分辨率并改变它的大小。
你问的是 Responsive Design。下面是你想要的例子 make.Although 不是最好的解决方案,我的意思是可以修改它以获得更好的性能(我也添加了一些代码移动 window 如果它是 StageStyle.UNDECORATED
拖动 Window 可以看到这个):
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 吗?:)
我想制作一个 GUI 桌面应用程序,它应该 运行 在不同的系统上运行良好。在第一步中,我想创建一个在不同屏幕(如 1920*1080 和 800*600)中显示良好的矩形。第一个系统中的这个矩形大小应该是 900 * 500,第二个系统中它的比例应该是 500 * 350(比例只是例子!)我如何定义一个以这种方式工作的矩形?
JavaFX 包中的 Rectange
class 本身就是一个可调整大小的矩形,因为您也可以更改 width/height 和位置。你只需要弄清楚当前的分辨率并改变它的大小。
你问的是 Responsive Design。下面是你想要的例子 make.Although 不是最好的解决方案,我的意思是可以修改它以获得更好的性能(我也添加了一些代码移动 window 如果它是 StageStyle.UNDECORATED
拖动 Window 可以看到这个):
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 吗?:)