gpt4 book ai didi

java - 我怎样才能 "auto-adjust"javafx中的类别轴标签?

转载 作者:太空宇宙 更新时间:2023-11-04 10:28:02 24 4
gpt4 key购买 nike

我正在使用 JavaFX 编写一个小直方图。我使用BarChart作为节点,并将XYChart系列添加到BarChart中。我的代码运行得很好,直到我发现这个:

Picture of category labels grouped at the origin

标签彼此重叠。

然后当我手动调整窗口大小时:

Picture of category labels correctly positioned after resizing window

所以,我的问题是如何让它在不手动执行的情况下自行调整?

这是我的代码的简要 View :

    //  Make an axis for x and one for y. Then, create a chart for the histogram by using x and y axis.
final CategoryAxis xAxis = new CategoryAxis();
final NumberAxis yAxis = new NumberAxis();
final BarChart<String,Number>histogramChart = new BarChart<String,Number>(xAxis,yAxis);
BorderPane myBorderPane = new BorderPane(); // Use a border pane for the histogram and the button.
Button btEnter = new Button("ENTER");
TextField tfDirectory = new TextField();
Scanner input;
XYChart.Series seriesOfAlphabet = new XYChart.Series();
HBox hbxDirectoryAndButton = new HBox();
// A final string array for the arphabet
final static String[] alphabets= {"A","B","C","D","E","F"
,"G","H","I","J","K","L"
,"M","N","O","P","Q","R"
,"S","T","U","V","W","X"
,"Y","Z"};

// Main to launch the application
public static void main(String[] args) {
Application.launch(args);
}

@Override
public void start(Stage primaryStage) throws Exception {
// Set the title of the stage
primaryStage.setTitle("Histogram Of Occurences of Letters by Luckas(YingHong Huang");
// Set the title of the chart
histogramChart.setTitle("Histogram");
xAxis.setLabel("Alphets"); // Set label of the xAxis
yAxis.setLabel("Occurences"); // Set the label of yAxis
tfDirectory.setLayoutY(20); // Set the height of the text field for the file input
tfDirectory.setLayoutX(100); // Set the length of the text field for the file input
seriesOfAlphabet.setName("Occurences");
btEnter.setOnAction(e->{
openFile();
});
histogramChart.getData().add(seriesOfAlphabet);
hbxDirectoryAndButton.getChildren().addAll(tfDirectory,btEnter);
hbxDirectoryAndButton.setSpacing(10);
myBorderPane.setCenter(histogramChart);
myBorderPane.setBottom(hbxDirectoryAndButton);
myBorderPane.setPadding(new Insets(5,5,5,5));
Scene histogramScene = new Scene(myBorderPane,1500,700);
primaryStage.setScene(histogramScene);
primaryStage.show();
}

最佳答案

我今天也遇到了同样的问题。我使用 Javafx Scene Builder 并在属性中取消选择“动画”解决了问题。希望对您有所帮助。

关于java - 我怎样才能 "auto-adjust"javafx中的类别轴标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50319724/

24 4 0