我正在寻找一种Java方法,可以全屏显示尺寸。 使用时,
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
double width = screenSize.getWidth();
double height = screenSize.getHeight();
或
double width = GraphicsEnvironment.getLocalGraphicsEnvironment().
getMaximumWindowBounds().getWidth();
每当屏幕更改分辨率时,宽度和高度值都会更改。我不理解这种度量,因为width和height值是原始类型double的(这意味着它们驻留在堆栈中)。
经过详尽搜索,我不得不求助于这个论坛,因为我没有找到任何类似availSize
的方法
答案 0 :(得分:0)
您可以使用以下方法以全屏方式获取所有内容,还可以调用静态方法来更改仅传递FXML名称(another.fxml)的场景。希望您正在寻找这个:
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.geometry.Rectangle2D;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.stage.Screen;
import javafx.stage.Stage;
import javafx.scene.layout.Region;
import javafx.scene.layout.StackPane;
import java.awt.*;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Main extends Application {
private static Stage mainStage;
public static Stage getStage() {
return mainStage;
}
@Override
public void start(Stage stage) throws Exception {
mainStage = stage;
FXMLLoader loader = new FXMLLoader(getClass().getResource("sample.fxml"));
Region contentRootRegion = (Region) loader.load();
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
double origW = d.width;
double origH = d.height;
// Set a default "standard" or "100%" resolution
// origW = 1360;
// origH = 700;
// If the Region containing the GUI does not already have a preferred width and height, set it.
// But, if it does, we can use that setting as the "standard" resolution.
if (contentRootRegion.getPrefWidth() == Region.USE_COMPUTED_SIZE)
contentRootRegion.setPrefWidth(origW);
else
origW = contentRootRegion.getPrefWidth();
if (contentRootRegion.getPrefHeight() == Region.USE_COMPUTED_SIZE)
contentRootRegion.setPrefHeight(origH);
else
origH = contentRootRegion.getPrefHeight();
//Wrap the resizable content in a non-resizable container (Group)
Group group = new Group(contentRootRegion);
//Place the Group in a StackPane, which will keep it centered
StackPane rootPane = new StackPane();
rootPane.getChildren().add(group);
//Create the scene iniitally at the "100%" size
Scene scene = new Scene(rootPane, origW, origH);
//Bind the scene's width and height to the scaling parameters on the group
group.scaleXProperty().bind(scene.widthProperty().divide(origW));
group.scaleYProperty().bind(scene.heightProperty().divide(origH));
//Set the scene to the window (stage) and show it
stage.setTitle("My App Name");
Screen screen = Screen.getPrimary();
Rectangle2D bounds = screen.getVisualBounds();
stage.setX(bounds.getMinX());
stage.setY(bounds.getMinY());
stage.setWidth(bounds.getWidth());
stage.setHeight(bounds.getHeight());
stage.setMaximized(true);
//stage.setFullScreen(true);
stage.setScene(scene);
stage.show();
}
public static void ChangeScene(String FXML) {
try {
FXMLLoader loader = new FXMLLoader(Main.class.getResource(FXML));
Region contentRootRegion = (Region) loader.load();
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
double origW = d.width;
double origH = d.height;
if (contentRootRegion.getPrefWidth() == Region.USE_COMPUTED_SIZE)
contentRootRegion.setPrefWidth(origW);
else
origW = contentRootRegion.getPrefWidth();
if (contentRootRegion.getPrefHeight() == Region.USE_COMPUTED_SIZE)
contentRootRegion.setPrefHeight(origH);
else
origH = contentRootRegion.getPrefHeight();
//Wrap the resizable content in a non-resizable container (Group)
Group group = new Group(contentRootRegion);
//Place the Group in a StackPane, which will keep it centered
StackPane rootPane = new StackPane();
rootPane.getChildren().add(group);
//Create the scene initially at the "100%" size
Scene scene = new Scene(rootPane, origW, origH);
//Bind the scene's width and height to the scaling parameters on the group
group.scaleXProperty().bind(scene.widthProperty().divide(origW));
group.scaleYProperty().bind(scene.heightProperty().divide(origH));
//Set the scene to the window (stage) and show it
Screen screen = Screen.getPrimary();
Rectangle2D bounds = screen.getVisualBounds();
Main.getStage().setX(bounds.getMinX());
Main.getStage().setY(bounds.getMinY());
Main.getStage().setWidth(bounds.getWidth());
Main.getStage().setHeight(bounds.getHeight());
//Main.getStage().setMaximized(true);
Main.getStage().setScene(scene);
Main.getStage().show();
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
public static void main(String[] args) {
launch(args);
}
}