带有 Jscrollpane 的 jfx 中的样式问题

Stylling issues in jfx with Scrollpane

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

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

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();
        }
    }
}

输出是。

我自己得到了答案。特别感谢 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();
        }
    }
}