使用Scrollpane在jfx中解决问题

时间:2017-10-29 08:42:29

标签: java css javafx scrollpane

我已经向VBox添加了一个标签,它被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();
        }
    }
}

输出是。 enter image description here

1 个答案:

答案 0 :(得分:0)

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