简体   繁体   中英

Number axis does not display correctly javafx

For some reasons, I would like to create a lonely Number axis (without any chart) in my javafx application. My problem is that the axis does not seem to expand at all, therefore no value is readable from the axis.

Here is an exmaple of my problem :

package application;

import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.chart.NumberAxis;
import javafx.scene.layout.BorderPane;
public class Main extends Application {
    @Override
    public void start(Stage primaryStage) {
        try {
            BorderPane root = new BorderPane();
            NumberAxis axis = new NumberAxis(0, 100, 1);
            axis.setMinWidth(300);
            axis.setPrefWidth(300);
            axis.setMaxWidth(300);
            axis.setLabel("testAxis");
            root.getChildren().add(axis);

            Scene scene = new Scene(root,400,400);

            primaryStage.setScene(scene);
            primaryStage.show();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        launch(args);
    }
}

Could anyone help me with that ?

I assume that an axis without a chart won't work. How about when u just draw your own Axis with a Line and the scale on it?

    Pane root = new Pane();

    Scene scene = new Scene(root, 300, 250);

    Line lineScal = new Line(0, 50, 600, 50);
    lineScal.setStroke(Color.LIGHTBLUE);
    for (int i = 600; i >= 0; i = i - 50)

    {

        Line line1 = new Line(i, 50, i, 30);

        Text scaleText = new Text(i, 20, Double.toString(i));
        line1.setStroke(Color.LIGHTGRAY);

        root.getChildren().addAll(line1,scaleText);

    }

    root.getChildren().add(lineScal);
    primaryStage.setTitle("Hello World!");
    primaryStage.setScene(scene);
    primaryStage.show();
}

/**
 * @param args the command line arguments
 */
public static void main(String[] args)
{
    launch(args);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM