一个简单的问题,希望有一个简单的解决方案。我的JavaFX应用程序中有一个Label
,我希望按钮位于段落的结尾。在本段末,我的意思是好像它是另一个角色。我不能将X和Y设置为特定值,因为段落的长度并不总是相同,因此此按钮应位于的位置并不总是相同。
下面,红色是我想要的按钮。有什么办法以编程方式找到这一点吗?
谢谢
答案 0 :(得分:2)
我认为最简单的方法是创建自己的包装方法,根据可用宽度将段落字符串转换为文本(或标签)对象的数组,然后将它们放入带Button的FlowPane中在末尾。我不认为您可以轻松检索Label的实际末端坐标,因为它是覆盖Node边界的矩形。
在多个Text对象连接在一起的意义上,类似于该问题的解决方案:JavaFX: setting background color for Text controls
答案 1 :(得分:2)
您可以使用辅助方法将长文本分成代表单个文本行的List
个节点中的Text
个。然后,将Text
添加到FlowPane
之后,只需在末尾添加Button
。
看看下面演示的MCVE:
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.StringTokenizer;
public class TextFlowSample extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
// Simple interface
VBox root = new VBox(5);
root.setPadding(new Insets(10));
root.setAlignment(Pos.CENTER);
String longText = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc nibh sapien, commodo in libero ac, feugiat accumsan odio. Aliquam erat volutpat. Integer accumsan sapien at elit aliquet sagittis. Praesent fermentum nec nunc ultrices mollis. Nam in odio ullamcorper, eleifend quam quis, aliquam sem. Nullam feugiat ex nec elit rutrum blandit. Etiam ut mauris magna. Proin est nunc, viverra quis tempus sed, dictum in lacus.";
// Create a FlowPane to hold our Text
FlowPane flowPane = new FlowPane();
// Now, our button
Button button = new Button("Click This!");
// Let's use our custom method to get a list of Text objects to add to the FlowPane
// Here we can set our max length and determine if we'll allow words to be broken up
flowPane.getChildren().addAll(getTextsFromString(longText, 75, false));
flowPane.getChildren().add(button);
root.getChildren().add(flowPane);
// Show the Stage
primaryStage.setWidth(425);
primaryStage.setHeight(300);
primaryStage.setScene(new Scene(root));
primaryStage.show();
}
/**
* Helper method to convert a long String into a List of Text objects
*/
private List<Text> getTextsFromString(String string, int maxLineLength, boolean breakWords) {
List<Text> textList = new ArrayList<>();
// If we are breaking up words, just cut into rows up to the max length
if (breakWords) {
int index = 0;
while (index < string.length()) {
textList.add(new Text(string.substring(index, Math.min(index + maxLineLength, string.length()))));
index += maxLineLength;
}
} else {
// First, let's insert linebreaks into the String when max length is reached. The tokenizer here
// allows us to split the string and keep the spaces, making it easy to loop through later.
StringTokenizer tokenizer = new StringTokenizer(string, " ", true);
StringBuilder sb = new StringBuilder();
int currentLength = 0;
while (tokenizer.hasMoreTokens()) {
String word = tokenizer.nextToken();
// If the next word would exceed our max length, add the new line character
if (currentLength + word.length() > maxLineLength) {
sb.append("\n");
currentLength = 0;
}
sb.append(word);
currentLength += word.length();
}
// Now we can split the string using the \n as a delimiter
List<String> rows = Arrays.asList(sb.toString().split("\n"));
for (String row : rows) {
textList.add(new Text(row));
}
}
return textList;
}
}
结果: