在JavaFX Scene Builder中,我可以选择Control
或Container
的最小,最大和首选宽度和高度的值。有一些预定义的值,例如USE_COMPUTED_SIZE
。
我得出的结论是,由于某些原因,我不喜欢在当前项目中使用FXML,现在我想知道如何在不使用FXML时设置这些值。
我该怎么做呢?
我尝试了以下代码:
initializeControls();
addControls();
setMinHeight(Control.USE_COMPUTED_SIZE);
setMinWidth(Control.USE_COMPUTED_SIZE);
addActionListeners();
addFocusChangeListeners();
但是,在尝试创建舞台时会导致异常:
Exception in thread "JavaFX Application Thread" java.lang.IllegalArgumentException: The width and height must be >= 0. Got: width=-1; height=-1
at com.sun.glass.ui.Window.setMinimumSize(Window.java:984)
at com.sun.javafx.tk.quantum.WindowStage.setMinimumSize(WindowStage.java:291)
at javafx.stage.Stage.impl_visibleChanging(Stage.java:1154)
at javafx.stage.Window$9.invalidated(Window.java:743)
at javafx.beans.property.BooleanPropertyBase.markInvalid(BooleanPropertyBase.java:109)
at javafx.beans.property.BooleanPropertyBase.set(BooleanPropertyBase.java:143)
at javafx.stage.Window.setShowing(Window.java:841)
at javafx.stage.Window.show(Window.java:856)
at javafx.stage.Stage.show(Stage.java:255)
at dictionary.MainApp.trainVocablesButtonActionPerformed(MainApp.java:281)
at dictionary.MainApp.lambda$addActionListeners$2(MainApp.java:240)
at dictionary.MainApp$$Lambda$281/81350454.handle(Unknown Source)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Node.fireEvent(Node.java:8216)
at javafx.scene.control.Button.fire(Button.java:185)
at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3724)
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3452)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1728)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2461)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:348)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:273)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:382)
at com.sun.glass.ui.View.handleMouseEvent(View.java:553)
at com.sun.glass.ui.View.notifyMouse(View.java:925)
at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
at com.sun.glass.ui.gtk.GtkApplication.lambda$null$45(GtkApplication.java:126)
at com.sun.glass.ui.gtk.GtkApplication$$Lambda$42/379110473.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)
我猜想我是从错误的类等中获取了
USE_COMPUTED_SIZE
值,但是我从哪里得到正确的值呢?还是整个方法都有些错误?我的最终目标是使舞台占用所需的空间来显示其中的所有控件和容器,但不要更多。
编辑#1:
更正:它应该占用所需的空间作为“最小宽度和高度”,以便窗口仍可调整大小,但不能做得太小以致不再显示控件。
编辑#2:
尝试在较小的示例应用程序上设置最小宽度+高度的结果:
代码:
package javafxapplication3;
import javafx.application.Application;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Control;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.paint.Color;
import javafx.scene.paint.Paint;
import javafx.scene.paint.RadialGradientBuilder;
import javafx.scene.paint.Stop;
import javafx.stage.Stage;
public class GridPaneExample extends Application {
private final Paint background = RadialGradientBuilder.create()
.stops(new Stop(0d, Color.TURQUOISE), new Stop(1, Color.web("3A5998")))
.centerX(0.5d).centerY(0.5d).build();
@Override
public void start(Stage primaryStage) {
GridPane root = new GridPane();
root.setPadding(new Insets(5));
root.setHgap(2);
root.setVgap(2);
root.setAlignment(Pos.CENTER);
final TextField tex = new TextField();
tex.setPrefWidth(250);
tex.setPrefHeight(50);
tex.setPromptText("Press the buttons to Display text here");
tex.setFocusTraversable(false);
GridPane.setColumnSpan(tex, 7);
GridPane.setRowSpan(tex, 2);
root.add(tex, 0, 0);
GridPane.setHalignment(tex, HPos.CENTER);
final Button[][] btn = new Button[5][5];
char ch = 'A';
for ( int i = 0; i < btn.length; i++) {
for ( int j = 0; j < btn.length; j++) {
btn[i][j] = new Button("" + ch);
btn[i][j].setPrefSize(50, 50);
root.add(btn[i][j], j, i+2);
ch++;
btn[i][j].setOnMouseClicked((mouseEvent) -> {
Button b = (Button) mouseEvent.getSource();
tex.appendText(b.getText());
});
}
}
Scene scene = new Scene(root,background);
primaryStage.setTitle("Grid Pane Example");
primaryStage.setScene(scene);
primaryStage.setWidth(Control.USE_COMPUTED_SIZE);
primaryStage.setHeight(Control.USE_COMPUTED_SIZE);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
最佳答案
评论之一帮助我找到了一个解决方案,尽管它可能不是最佳解决方案:
将所有控件添加到场景之后,我只需执行以下操作:
setMinWidth(getWidth());
setMinHeight(getHeight());
之所以可行,是因为JavaFX最初将舞台设置为场景中控件所需的大小。
关于不使用FXML的Stage的JavaFx USE_COMPUTED_SIZE,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28573700/