在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);
}
}
答案 0 :(得分:1)
其中一条评论帮助我找到了解决方案,尽管它可能不是最佳解决方案: 将所有控件添加到场景后,我只需执行以下操作:
setMinWidth(getWidth());
setMinHeight(getHeight());
这是有效的,因为JavaFX最初将舞台设置为场景中控件所需的大小。
答案 1 :(得分:0)
聚会可能要迟了,但这是我发现的:
对于Textfield,有一个静态字段TextField.USE_COMPUTED_SIZE
,其值为-1
我认为您也可以将-1
传递到舞台上。