2014-05-12 35 views
0

我试图调整TextField的大小,点击了startBtnClick,我似乎无法使其工作。基本上我想按单击按钮时缩小字段110单位。在按钮上调整TextField的大小点击

在行动事件:

@FXML 
protected void startBtnClick(){ 
    double txtWidth = this.url.getWidth(); 
    this.url.setPrefWidth(txtWidth - 110); 
} 

XML:

<?xml version="1.0" encoding="UTF-8"?> 
<AnchorPane fx:id="anchorPane" minHeight="200.0" minWidth="200.0" prefHeight="600.0" prefWidth="1200.0" styleClass="anchor-pane" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2" fx:controller="phantom.FXMLDocumentController"> 
    <children> 
     <Button id="beginSurfing" fx:id="quitSurfing" mnemonicParsing="false" onAction="#stopSurfing" prefHeight="29.0" prefWidth="93.00009999999747" text="Quit Surfing" visible="false" AnchorPane.rightAnchor="9.0" AnchorPane.topAnchor="8.0" /> 
     <GridPane prefHeight="600.0" prefWidth="1200.0" style="" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0"> 
      <children> 
       <AnchorPane prefHeight="200.0" prefWidth="200.0" GridPane.columnIndex="0" GridPane.rowIndex="0"> 
        <children> 
         <TextField fx:id="url" disable="false" onAction="#urlGo" opacity="1.0" prefWidth="965.0" scaleZ="1.0" style="" translateZ="0.0" AnchorPane.leftAnchor="122.0" AnchorPane.rightAnchor="113.0" AnchorPane.topAnchor="9.0"> 
          <stylesheets> 
           <URL value="@../CSS/textField.css" /> 
          </stylesheets> 
         </TextField> 
         <Button id="beginSurfing" fx:id="startSurfing" mnemonicParsing="false" onAction="#beginSurfing" prefHeight="29.0" prefWidth="93.00009999999747" text="Start Surfing" AnchorPane.rightAnchor="12.0" AnchorPane.topAnchor="9.0" /> 
         <Label fx:id="timer" alignment="CENTER" contentDisplay="CENTER" prefHeight="29.0" prefWidth="94.0" text="" textAlignment="CENTER" underline="false" AnchorPane.rightAnchor="11.0" AnchorPane.topAnchor="9.0"> 
          <font> 
           <Font name="System Bold" size="20.0" /> 
          </font> 
         </Label> 
         <Button id="beginSurfing" fx:id="nextAdBtn" mnemonicParsing="false" onAction="#nextAd" prefHeight="30.000099999997474" prefWidth="94.0" text="Next Ad" visible="false" AnchorPane.rightAnchor="12.0" AnchorPane.topAnchor="8.0" /> 
        </children> 
       </AnchorPane> 
       <!-- 
        The rest of the gird 
        Chopped off for readability 
       --> 
      </children> 
     </GridPane> 
    </children> 
</AnchorPane> 
+0

我想'setPrefWidth'是不正确的方法。我会测试并回答 – diegoaguilar

+0

不完全相关,但可能会给你线索:http://stackoverflow.com/questions/20304933/how-to-get-javafx-nodes-textarea-textfield-to-resize-correctly-when-user -drag –

+1

您需要设置按钮的布局参数,而不仅仅是首选值。为了效率和稳定性,Android可以(并经常)覆盖您的首选值。 – zgc7009

回答

3

由于您的按钮处于AnchorPane,你有两个左,右锚件设置,宽度为将由这些锚点控制,而不是由prefWidth属性控制。一个AnchorPane可能不是去,如果你希望能够动态地调整控制的最佳方式,但你可以做

AnchorPane.setRightAnchor(url, AnchorPane.getRightAnchor(url)+110); 
+0

太棒了!这就像一个魅力! –