2017-10-10 66 views
0

因此,我试图让文本在左边,按钮在右边,文本应该具有不变的大小,并且按钮应该调整大小以填充窗口的其余部分。将舞台划分为2个网格JavaFX

这是到目前为止我的结果:

so far...

我不希望我的文字了按钮,我希望他们能够分享整个窗口。

import javafx.application.Application; 
import javafx.geometry.Pos; 
import javafx.scene.Scene; 
import javafx.scene.control.Button; 
import javafx.scene.control.TextArea; 
import javafx.scene.layout.GridPane; 
import javafx.scene.layout.StackPane; 
import javafx.stage.Stage; 


public class Main extends Application { 

    GridPane buttons = new GridPane(); 
    GridPane textGrid = new GridPane(); 
    @Override 
    public void start(Stage primaryStage) { 

     StackPane root = new StackPane(); 
     Button button1 = new Button(); 
     Button button2 = new Button(); 
     Button button3 = new Button(); 
     Button button4 = new Button(); 
     Button button5 = new Button(); 

     button1.setText("Button1"); 
     button2.setText("Button4"); 
     button3.setText("Button3"); 
     button4.setText("Button4"); 
     button5.setText("Button5"); 


     TextArea text1 = new TextArea(); 
     text1.setText("Test"); 
     text1.setPrefSize(100, 100); 

     button1.prefWidthProperty().bind(buttons.widthProperty()); 
     button2.prefWidthProperty().bind(buttons.widthProperty()); 
     button3.prefWidthProperty().bind(buttons.widthProperty()); 
     button4.prefWidthProperty().bind(buttons.widthProperty()); 
     button5.prefWidthProperty().bind(buttons.widthProperty()); 

     button1.prefHeightProperty().bind(buttons.heightProperty()); 
     button2.prefHeightProperty().bind(buttons.heightProperty()); 
     button3.prefHeightProperty().bind(buttons.heightProperty()); 
     button4.prefHeightProperty().bind(buttons.heightProperty()); 
     button5.prefHeightProperty().bind(buttons.heightProperty()); 


     buttons.addColumn(0, button1, button2, button3, button4, button5); 

     textGrid.addColumn(0, text1); 


     Scene scene = new Scene(root, 280, 180); 

     root.getChildren().addAll(buttons, textGrid); 

     buttons.setAlignment(Pos.TOP_RIGHT); 
     textGrid.setAlignment(Pos.TOP_LEFT); 

     primaryStage.setScene(scene); 
     primaryStage.show(); 
    } 

    public static void main(String[] args) { 
     launch(args); 
    } 

} 
+0

使用'HBox'而不是'StackPane'?还是一个'BorderPane',具体取决于你希望他们在调整大小时的表现?请参阅http://docs.oracle.com/javase/8/javafx/layout-tutorial/builtin_layouts.htm#JFXLY102 –

+0

感谢您的建议,可能会使用HBox – murilo

回答

4

通常最好是让布局窗格处理布局管理,而不是试图通过绑定来管理布局。

这里有一个例子:

layout

import javafx.application.Application; 
import javafx.geometry.Insets; 
import javafx.scene.Scene; 
import javafx.scene.control.*; 
import javafx.scene.layout.*; 
import javafx.stage.Stage; 

import java.util.stream.IntStream; 

public class Main extends Application { 

    private static final int N_BUTTONS = 5; 

    @Override 
    public void start(Stage stage) { 
     VBox buttonLayout = new VBox(
       10, 
       IntStream.range(0, N_BUTTONS) 
         .mapToObj(this::createButton) 
         .toArray(Button[]::new) 
     ); 
     HBox.setHgrow(buttonLayout, Priority.ALWAYS); 

     TextArea textArea = new TextArea("Test"); 
     textArea.setPrefWidth(100); 
     textArea.setMaxWidth(TextArea.USE_PREF_SIZE); 
     textArea.setMinWidth(TextArea.USE_PREF_SIZE); 

     HBox layout = new HBox(10, textArea, buttonLayout); 
     layout.setPadding(new Insets(10)); 

     Scene scene = new Scene(layout); 

     stage.setScene(scene); 
     stage.show(); 
    } 

    private Button createButton(int i) { 
     Button button = new Button("Button " + i); 
//  button.setMaxWidth(Double.MAX_VALUE); 
     button.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE); 
     VBox.setVgrow(button, Priority.ALWAYS); 

     return button; 
    } 

    public static void main(String[] args) { 
     launch(args); 
    } 
} 

这里有一对夫妇的事情,我想指出,基于样本:

  1. 由于这些按钮是如此的相似,创建循环中的按钮而不是代码中的按钮。我在地图和toArray上使用了IntStream范围,但您可以使用循环标准(可能更容易理解)做同样的事情。
  2. 使用标准布局窗格的组合来实现您的布局。例如,按钮是垂直间隔的,所以将它们放在一个VBox中,文本和按钮相互水平,所以使用HBox。
  3. 在布局上使用约束来将它们按摩到执行您喜欢的布局中,例如,HBox.setHgrow(buttonLayout, Priority.ALWAYS);告诉Box始终将任何额外的额外空间分配给buttonLayout,以便按钮将填充任何其余区域。
  4. 在单个节点上设置约束以调整它们的大小,例如下面的代码为textArea创建了一个固定的宽度,它不会发生变化(如果您愿意,您可以使用类似的代码来建立固定的高度):

    textArea.setPrefWidth(100); 
    textArea.setMaxWidth(TextArea.USE_PREF_SIZE); 
    textArea.setMinWidth(TextArea.USE_PREF_SIZE); 
    
  5. 某些控件会自动扩展自己,超越自己的最大大小,按钮不被默认情况下,启用此行为,请使用以下代码(如果你只是想的宽度扩大,而不是高度,那么你只会设置最大宽度而不是最大尺寸):

    button.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE); 
    
  6. 而不是像本示例中那样定义代码中的布局,而是使用诸如SceneBuilder之类的工具以可视方式创建场景并将布局保存为FXML file,以便将布局与代码分开(类似地将任何样式放置在外部CSS文件)。