我无法调整大小,它总是针对每个屏幕的首选大小。这对于全屏应用程序来说并不理想。它基本上只是左上角的一个小盒子=(
我现在已经花了好几天但却无法上班。
有谁能告诉我我做错了什么?谢谢
主要课程:
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 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;
}
}
}
接口,以便每个屏幕都知道其父级:
public interface ControlledScreen {
public void setScreenParent(ScreensController screenPage);
}
第二个控制器类,用于验证堆栈窗是否有效(具有FXML文件):
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;
}
}
答案 0 :(得分:0)
您不应该将primaryStage.setFullScreen(true);
用于可调整大小的应用程序。反正不是在这种情况下。删除该行后,应用程序将以其首选大小开始,但用户可以拖动窗口的角来调整应用程序的大小。