JavaFX StackPane/group阻止我调整屏幕大小
我不能让它调整大小,它总是为每个屏幕的首选大小。这对于全屏应用程序并不理想。它基本上变成了左上角的一个小盒子= 我已经花了好几天时间在这上面了,但都没法让它发挥作用 谁能告诉我我做错了什么?谢谢 主要类别:JavaFX StackPane/group阻止我调整屏幕大小,javafx,resize,stackpanel,Javafx,Resize,Stackpanel,我不能让它调整大小,它总是为每个屏幕的首选大小。这对于全屏应用程序并不理想。它基本上变成了左上角的一个小盒子= 我已经花了好几天时间在这上面了,但都没法让它发挥作用 谁能告诉我我做错了什么?谢谢 主要类别: public class Main extends Application { public static final String MAIN_SCREEN = "main"; public static final String MAIN_SCREEN_FXML = "../gui/m
public class Main extends Application {
public static final String MAIN_SCREEN = "main";
public static final String MAIN_SCREEN_FXML = "../gui/main.fxml";
public static final String CUSTOMER_SCREEN = "customer_main";
public static final String CUSTOMER_SCREEN_FXML = "../gui/customer_main.fxml";
@Override
public void start(Stage primaryStage) {
primaryStage.setFullScreen(true);
primaryStage.centerOnScreen();
ScreensController mainContainer = new ScreensController();
mainContainer.loadScreen(Main.MAIN_SCREEN,
Main.MAIN_SCREEN_FXML);
mainContainer.loadScreen(Main.CUSTOMER_SCREEN,
Main.CUSTOMER_SCREEN_FXML);
mainContainer.setScreen(Main.MAIN_SCREEN);
Group root = new Group();
root.getChildren().addAll(mainContainer);
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
mainContainer.requestLayout();
}
public static void main(String[] args) {
launch(args);
}
第一个屏幕控制器类具有FXML文件:
public class MainScreenController implements ControlledScreen, Initializable{
ScreensController myController;
@FXML
private VBox mainScreen;
@FXML
private void mainScreenClicked(){
myController.setScreen(Main.CUSTOMER_SCREEN);
}
@Override
public void initialize(URL arg0, ResourceBundle arg1) {
}
@Override
public void setScreenParent(ScreensController screenParent) {
myController = screenParent;
}
public class CustomerMenuController implements ControlledScreen, Initializable {
ScreensController myController;
@FXML
private FlowPane customerMenuFlow;
@Override
public void initialize(URL location, ResourceBundle resources) {
for (int i = 0; i < 3;i++){
new Customer();
}
Button [] menuButtons = new Button[Customer.customers.size()];
for (int i = 0; i < Customer.customers.size();i++){
menuButtons[i] = new Button("Customer " + i);
customerMenuFlow.getChildren().add(menuButtons[i]);
}
}
@Override
public void setScreenParent(ScreensController screenParent) {
myController = screenParent;
}
}
堆栈窗格,以获得良好的布局:
public class ScreensController extends StackPane {
public ScreensController(){
}
private HashMap<String, Node> screens = new HashMap<>();
public void addScreen(String name, Node screen) {
screens.put(name, screen);
}
public boolean loadScreen(String name, String resource) {
try {
FXMLLoader myLoader = new FXMLLoader(getClass().getResource(resource));
Parent loadScreen = (Parent) myLoader.load();
ControlledScreen myScreenControler =
((ControlledScreen) myLoader.getController());
myScreenControler.setScreenParent(this);
addScreen(name, loadScreen);
return true;
}catch(Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
return false;
}
}
public boolean setScreen(final String name) {
if(screens.get(name) != null) { //screen loaded
final DoubleProperty opacity = opacityProperty();
//Is there is more than one screen
if(!getChildren().isEmpty()){
Timeline fade = new Timeline(
new KeyFrame(Duration.ZERO,
new KeyValue(opacity,1.0)),
new KeyFrame(new Duration(1000),
new EventHandler() {
@Override
public void handle(Event t) {
//remove displayed screen
getChildren().remove(0);
//add new screen
getChildren().add(0, screens.get(name));
Timeline fadeIn = new Timeline(
new KeyFrame(Duration.ZERO,
new KeyValue(opacity, 0.0)),
new KeyFrame(new Duration(800),
new KeyValue(opacity, 1.0)));
fadeIn.play();
}
}, new KeyValue(opacity, 0.0)));
fade.play();
} else {
//no one else been displayed, then just show
setOpacity(0.0);
getChildren().add(screens.get(name));
Timeline fadeIn = new Timeline(
new KeyFrame(Duration.ZERO,
new KeyValue(opacity, 0.0)),
new KeyFrame(new Duration(2500),
new KeyValue(opacity, 1.0)));
fadeIn.play();
}
return true;
} else {
System.out.println("screen hasn't been loaded!\n");
return false;
}
}
public boolean unloadScreen(String name) {
if(screens.remove(name) == null) {
System.out.println("Screen didn't exist");
return false;
} else {
return true;
}
}
验证stackpane是否具有FXML文件的第二个控制器类:
public class MainScreenController implements ControlledScreen, Initializable{
ScreensController myController;
@FXML
private VBox mainScreen;
@FXML
private void mainScreenClicked(){
myController.setScreen(Main.CUSTOMER_SCREEN);
}
@Override
public void initialize(URL arg0, ResourceBundle arg1) {
}
@Override
public void setScreenParent(ScreensController screenParent) {
myController = screenParent;
}
public class CustomerMenuController implements ControlledScreen, Initializable {
ScreensController myController;
@FXML
private FlowPane customerMenuFlow;
@Override
public void initialize(URL location, ResourceBundle resources) {
for (int i = 0; i < 3;i++){
new Customer();
}
Button [] menuButtons = new Button[Customer.customers.size()];
for (int i = 0; i < Customer.customers.size();i++){
menuButtons[i] = new Button("Customer " + i);
customerMenuFlow.getChildren().add(menuButtons[i]);
}
}
@Override
public void setScreenParent(ScreensController screenParent) {
myController = screenParent;
}
}
您不应该使用primaryStage.setFullScreentrue;用于可调整大小的应用程序。无论如何,在这种情况下是不行的。删除该行后,应用程序将以其首选大小开始,但用户可以拖动窗口的角来调整应用程序的大小