far*_*raa 0 css java javafx javafx-2 scenebuilder
我在TabPane中有3个标签,每个标签都有一个不同文本和不同长度的文本区域.我想根据每个标签中的长度自动调整文本区域.我不明白我该怎么办?使用场景构建器?css?javaFX方法?提前致谢 ...
我想你要求文本区域根据显示的文本增长或缩小?
如果是,请查看此代码是否有帮助:
import java.util.concurrent.Callable;
import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.TextArea;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class AutosizingTextArea extends Application {
@Override
public void start(Stage primaryStage) {
TextArea textArea = new TextArea();
textArea.setMinHeight(24);
textArea.setWrapText(true);
VBox root = new VBox(textArea);
Scene scene = new Scene(root, 600, 400);
primaryStage.setScene(scene);
primaryStage.show();
// This code can only be executed after the window is shown:
// Perform a lookup for an element with a css class of "text"
// This will give the Node that actually renders the text inside the
// TextArea
Node text = textArea.lookup(".text");
// Bind the preferred height of the text area to the actual height of the text
// This will make the text area the height of the text, plus some padding
// of 20 pixels, as long as that height is between the text area's minHeight
// and maxHeight. The minHeight we set to 24 pixels, the max height will be
// the height of its parent (usually).
textArea.prefHeightProperty().bind(Bindings.createDoubleBinding(new Callable<Double>(){
@Override
public Double call() throws Exception {
return text.getBoundsInLocal().getHeight();
}
}, text.boundsInLocalProperty()).add(20));
}
public static void main(String[] args) {
launch(args);
}
}
Run Code Online (Sandbox Code Playgroud)
如果你想使这个可重用,那么你可以考虑子类化TextArea.(一般来说,我不喜欢继承控制类.)这里棘手的部分是执行TextArea扩展后的代码,一旦它被添加到实时场景图中(这是查找工作所必需的).执行此操作的一种方法(这有点像黑客,imho)是使用a AnimationTimer进行查找,一旦查找成功,您就可以停止查找.我在这里嘲笑这件事.
| 归档时间: |
|
| 查看次数: |
1829 次 |
| 最近记录: |