2016-02-23 114 views
0

我无法真正理解JavaFX中这种伸缩是如何工作的。我为HBox和TextField都设置了无穷大宽度,所以如果我们调整帧大小,TextField应该更大。请帮助我,我在这里错过了什么。谢谢。为什么我的文本框不能调整大小?

<GridPane fx:id="first" hgap="5" vgap="5" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1" > 

<columnConstraints> 
    <ColumnConstraints hgrow="ALWAYS" percentWidth="100.0" /> 
</columnConstraints> 

<VBox.margin> 
    <Insets left="10.0" top="15"/> 
</VBox.margin> 
<HBox GridPane.columnIndex="0" GridPane.rowIndex="0" prefWidth="Infinity" maxWidth="Infinity"> 

    <fx:define> 
     <ToggleGroup fx:id="myToggleGroup"/> 
    </fx:define> 
    <children> 
     <RadioButton text="System" toggleGroup="$myToggleGroup"> 
     </RadioButton> 
     <RadioButton text="Document" toggleGroup="$myToggleGroup"> 
      <HBox.margin> 
       <Insets left="200.0"/> 
      </HBox.margin> 
     </RadioButton> 
    </children> 
</HBox> 
<HBox GridPane.rowIndex="1" GridPane.columnIndex="0" > 
    <Label text="Name:" minWidth="50"> 
    </Label> 

    <TextField maxWidth="Infinity" minWidth="450" prefWidth="Infinity"> 
     <HBox.margin> 
      <Insets left="20.0"/> 
     </HBox.margin> 
    </TextField> 
</HBox> 

+0

参见如果HTTP://计算器。 com/questions/35438104/javafx-alignment-of-label-in-gridpane(这是不同的,但对网格窗格中布局的工作原理有完整的解释)有帮助。 –

+0

另外我只允许在这个项目中使用fxml。 – Joey

+0

无关紧要,该问题中的所有内容都可以通过FXML完成。 (虽然,“*只允许使用FXML”???你只能使用FXML编写应用程序,你需要在控制器中使用Java。) –

回答

0

缺省情况下,HBox将分配控制它们的优选的尺寸。我不知道他们会如何处理的"Infinity"首选大小(我从这个布局计算一些错误):我会删除所有prefWidth="Infinity",只是告诉HBox允许文本字段增长:

<TextField HBox.hgrow="ALWAYS" maxWidth="Infinity"> 

下面是一个完整的例子(略作修改,以适应成SSCCE):

<?xml version="1.0" encoding="UTF-8"?> 

<?import javafx.scene.layout.GridPane?> 
<?import javafx.scene.layout.HBox?> 
<?import javafx.scene.layout.ColumnConstraints?> 
<?import javafx.scene.control.RadioButton?> 
<?import javafx.scene.control.Label?> 
<?import javafx.scene.control.TextField?> 
<?import javafx.scene.control.ToggleGroup?> 
<?import javafx.geometry.Insets?> 

<GridPane fx:id="first" hgap="5" vgap="5" 
    xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1"> 

    <columnConstraints> 
     <ColumnConstraints hgrow="ALWAYS" percentWidth="100.0" /> 
    </columnConstraints> 

    <HBox GridPane.columnIndex="0" GridPane.rowIndex="0"> 

     <fx:define> 
      <ToggleGroup fx:id="myToggleGroup" /> 
     </fx:define> 
     <children> 
      <RadioButton text="System" toggleGroup="$myToggleGroup"> 
      </RadioButton> 
      <RadioButton text="Document" toggleGroup="$myToggleGroup"> 
       <HBox.margin> 
        <Insets left="200.0" /> 
       </HBox.margin> 
      </RadioButton> 
     </children> 
    </HBox> 
    <HBox GridPane.rowIndex="1" GridPane.columnIndex="0"> 
     <Label text="Name:" minWidth="50"> 
     </Label> 

     <TextField HBox.hgrow="ALWAYS" maxWidth="Infinity"> 
      <HBox.margin> 
       <Insets left="20.0" /> 
      </HBox.margin> 
     </TextField> 
    </HBox> 
</GridPane> 

并运行它的代码:

import java.io.IOException; 

import javafx.application.Application; 
import javafx.fxml.FXMLLoader; 
import javafx.scene.Scene; 
import javafx.stage.Stage; 

public class Main extends Application { 

    @Override 
    public void start(Stage primaryStage) throws IOException { 
     primaryStage.setScene(new Scene(FXMLLoader.load(getClass().getResource("layout.fxml")))); 
     primaryStage.show(); 
    } 

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

耶,谢谢,多数民众赞成它。 – Joey

+0

在旁注中,如果伸展我的第二个单选按钮总是停留在中间位置,我该如何实现? – Joey

相关问题