不,它并不总是发生。在您的示例中,它发生了,因为您在显示舞台后设置了初始文本。
public class TextAlign extends Application {
/*
* (non-Javadoc)
* @see javafx.application.Application#start(javafx.stage.Stage)
*/
@Override
public void start(Stage primaryStage) throws Exception {
BorderPane bp = new BorderPane();
TextField tf = new TextField();
tf.setAlignment(Pos.CENTER);
tf.setPrefColumnCount(20);
tf.setMaxWidth(200);
// Default text is part of initialization.
tf.setText("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
bp.setCenter(tf);
Scene scene = new Scene(bp, 300, 300);
primaryStage.setScene(scene);
primaryStage.show();
}
}
此外,如果您使用 FXML(就像 JavaFX 中推荐的那样,即使不是强制性的),您也不会出现这种行为,因为一切都以正确的顺序完成。
编辑
根据您添加的信息,我会使用Label 而不是TextField。如果单词太长,请选择适当的textOverrun 和ellipsis,并添加Tooltip 以便在需要时查看整个标签。设置标签的样式以使您的标签看起来像想要的 TextField(我没有在这个例子中使用 css,但我应该有,如果我不懒惰的话)。
控制器 TextAlignCtrl.java
public class TextAlignCtrl extends BorderPane {
@FXML
private Label summary;
public TextAlignCtrl() {
FXMLLoader loader = new FXMLLoader(getClass().getResource("TextAlign.fxml"));
loader.setController(this);
loader.setRoot(this);
try {
loader.load();
} catch(IOException e) {
System.out.println("An error occurs trying to load the FXML TextAlign component." + e);
e.printStackTrace();
}
}
@FXML
private void initialize() {
summary.setText("Author - Music Name");
Tooltip tooltip = new Tooltip();
tooltip.textProperty().bind(summary.textProperty());
Tooltip.install(summary, tooltip);
}
@FXML
private void copyTextField(MouseEvent e) {
System.out.println("Mouse released on label");
}
@FXML
private void selectLabel(MouseEvent e) {
String lText = ((Label) e.getSource()).getText();
System.out.println("Label selection " + lText);
summary.setText(lText);
}
}
FXML TextAlign.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.VBox?>
<fx:root type="BorderPane" xmlns:fx="http://javafx.com/fxml/1">
<left>
<VBox>
<HBox>
<Label fx:id="lbl1" text="U2 - I Still Haven't Found What I'm Looking For"
onMouseClicked="#selectLabel" />
</HBox>
<HBox>
<Label fx:id="lbl2" text="Bob Marley - One love"
onMouseClicked="#selectLabel" />
</HBox>
<HBox>
<Label fx:id="lbl3" text="Scorpions - Still loving you" onMouseClicked="#selectLabel" />
</HBox>
<HBox>
<Label fx:id="lbl4" text="Queen - We will rock you." onMouseClicked="#selectLabel" />
</HBox>
</VBox>
</left>
<top>
<HBox alignment="CENTER">
<Label fx:id="summary" alignment="CENTER"
style="-fx-border-color:grey;-fx-border-width:2px;" onMouseReleased="#copyTextField"
pickOnBounds="false" prefHeight="35.0" prefWidth="250.0" textOverrun="CENTER_WORD_ELLIPSIS" ellipsisString="[...]" >
<font>
<Font name="Arial" size="16.0" />
</font>
</Label>
<!-- <TextField fx:id="textField" alignment="CENTER" disable="false" editable="true"
onMouseReleased="#copyTextField" style="-fx-text-overrun:CENTER;" pickOnBounds="false"
prefHeight="35.0" prefWidth="250.0" promptText="Author - Music Name" > <font>
<Font name="Arial" size="16.0" /> </font> </TextField> -->
</HBox>
</top>
</fx:root>
应用程序入口点MainTextAlign.java
public class MainTextAlign extends Application {
@FXML
private TextField tf;
/*
* (non-Javadoc)
* @see javafx.application.Application#start(javafx.stage.Stage)
*/
@Override
public void start(Stage primaryStage) throws Exception {
TextAlignCtrl bp = new TextAlignCtrl();
Scene scene = new Scene(bp, 300, 300);
primaryStage.setScene(scene);
primaryStage.show();
}
}