gpt4 book ai didi

java - 带有 Jscrollpane 的 jfx 中的样式问题

转载 作者:行者123 更新时间:2023-11-28 02:44:17 26 4
gpt4 key购买 nike

我在 VBox 中添加了一个 Label,它被 Scrollpane 进一步包装。这样做时,我的 Label 样式不起作用。

我这里有密码。当我删除滚动 Pane 时它工作正常。

package javasocket;

import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.VBox;
import javafx.scene.text.TextAlignment;

public class Controller {

VBox msg_vbox = new VBox();

@FXML
ScrollPane scrlpane;

@FXML
TextField message;

@FXML
protected void sendMessage() {

Label set_text = new Label();

set_text.setText("Server Says: \n" + message.getText());


set_text.setStyle("-fx-padding:10;-fx-margin:10;"
+ "-fx-background-color:teal;"
+ " -fx-background-insets: 5;"
+ "-fx-font-size:15;"
+ "-fx-background-radius: 3;");
set_text.setPrefSize(Double.MAX_VALUE, Double.compare(0, 500));
set_text.setWrapText(true);
set_text.setTextAlignment(TextAlignment.JUSTIFY);
msg_vbox.getChildren().addAll(set_text);
scrlpane.setContent(msg_vbox);
message.setText("");

}

@FXML
protected void check_key(KeyEvent ae) {

if (ae.getCode().equals(KeyCode.ENTER)) {
sendMessage();
}
}
}

输出是。 enter image description here

最佳答案

我自己得到了答案。特别感谢 Fabian。

package javasocket;

import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.Region;
import javafx.scene.layout.VBox;
import javafx.scene.text.TextAlignment;

public class Controller {

VBox msg_vbox = new VBox();

@FXML
ScrollPane scrlpane;

@FXML
TextField message;

@FXML
protected void sendMessage() {

//new label text with message.
Label set_text = new Label();
set_text.setText("Server Says: \n" + message.getText());
set_text.setStyle("-fx-padding:10;"
+"-fx-width:100%;"
+ "-fx-background-color:teal;"
+ " -fx-background-insets: 5;"
+ "-fx-font-size:15;"
+ "-fx-background-radius: 3;");

set_text.setPrefSize(Region.USE_COMPUTED_SIZE, Region.USE_COMPUTED_SIZE);
set_text.setWrapText(true);
set_text.setTextAlignment(TextAlignment.JUSTIFY);
set_text.setPrefWidth(600);


//VBox wrapper
msg_vbox.getChildren().addAll(set_text);
msg_vbox.setPrefWidth(600);

//Further wrapped by ScrollPane
scrlpane.fitToHeightProperty();
scrlpane.setContent(msg_vbox);
scrlpane.vvalueProperty().bind(msg_vbox.heightProperty()); //sets the scroll view to new element.
message.setText("");



}

@FXML
protected void check_key(KeyEvent ae) {

if (ae.getCode().equals(KeyCode.ENTER)) {
sendMessage();
}
}
}

关于java - 带有 Jscrollpane 的 jfx 中的样式问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46998325/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com