包裹在 ScrollPane 内 HBox 中的 JavaFX 标签不起作用
JavaFX label wrapped within HBox within ScrollPane not working
我尝试使用 ScrollPane
滚动和 VBox
视图 (scroll.setContent(vbox);
) 来建立聊天。每条消息都会在其中创建新的 HBox
和 Label
。
标签包含文本并声明为 text.setWrapText(true);
当有足够的 space 而无需滚动时它的工作正常:
但是当消息高度大于 VHox
高度(ScrollPane
的视图)时,包装不起作用:
编辑:相关代码:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class ChatExample extends Application
{
public static void main(String[] args)
{
launch(args);
}
@Override
public void start(Stage stage) throws Exception
{
ScrollPane scroll = new ScrollPane();
scroll.setFitToHeight(true);
scroll.setPrefSize(300, 300);
VBox chat = new VBox();
chat.setSpacing(10);
scroll.setContent(chat);
chat.getChildren().add(addMessage("This is a wrapped message, This is a wrapped message"));
chat.getChildren().add(addMessage("This is a wrapped message, This is a wrapped message"));
chat.getChildren().add(addMessage("This is a wrapped message, This is a wrapped message"));
chat.getChildren().add(addMessage("This is a wrapped message, This is a wrapped message"));
final Scene scene = new Scene(scroll, 300, 300);
stage.setScene(scene);
stage.show();
}
public HBox addMessage(String message)
{
HBox hbox = new HBox();
Label label = new Label(message);
label.setWrapText(true);
label.setMaxWidth(50);
hbox.getChildren().add(label);
return hbox;
}
}
感谢@sillyfly 写了一个小例子,我弄清楚了这种情况是什么情况。
scroll.setFitToHeight(true);
提出了这个问题,所以我删除了它
我尝试使用 ScrollPane
滚动和 VBox
视图 (scroll.setContent(vbox);
) 来建立聊天。每条消息都会在其中创建新的 HBox
和 Label
。
标签包含文本并声明为 text.setWrapText(true);
当有足够的 space 而无需滚动时它的工作正常:
但是当消息高度大于 VHox
高度(ScrollPane
的视图)时,包装不起作用:
编辑:相关代码:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class ChatExample extends Application
{
public static void main(String[] args)
{
launch(args);
}
@Override
public void start(Stage stage) throws Exception
{
ScrollPane scroll = new ScrollPane();
scroll.setFitToHeight(true);
scroll.setPrefSize(300, 300);
VBox chat = new VBox();
chat.setSpacing(10);
scroll.setContent(chat);
chat.getChildren().add(addMessage("This is a wrapped message, This is a wrapped message"));
chat.getChildren().add(addMessage("This is a wrapped message, This is a wrapped message"));
chat.getChildren().add(addMessage("This is a wrapped message, This is a wrapped message"));
chat.getChildren().add(addMessage("This is a wrapped message, This is a wrapped message"));
final Scene scene = new Scene(scroll, 300, 300);
stage.setScene(scene);
stage.show();
}
public HBox addMessage(String message)
{
HBox hbox = new HBox();
Label label = new Label(message);
label.setWrapText(true);
label.setMaxWidth(50);
hbox.getChildren().add(label);
return hbox;
}
}
感谢@sillyfly 写了一个小例子,我弄清楚了这种情况是什么情况。
scroll.setFitToHeight(true);
提出了这个问题,所以我删除了它