JavaFx按钮不显示

时间:2018-05-01 11:29:58

标签: java macos user-interface button javafx

我试图在屏幕上显示一个按钮,但每次运行java程序时都没有显示。我不确定为什么没有出现。

代码:

package application;

import javafx.application.Application; 
import static javafx.application.Application.launch; 
import javafx.geometry.Insets; 
import javafx.geometry.Pos; 

import javafx.scene.Scene; 
import javafx.scene.control.Button; 
import javafx.scene.control.PasswordField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane; 
import javafx.scene.text.Text; 
import javafx.scene.control.TextField; 
import javafx.stage.Stage;  


public class Main extends Application {
@Override
public void start(Stage primaryStage) {
    try {
        BorderPane root = new BorderPane();
        Scene scene = new Scene(root,400,400);
        scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
        primaryStage.setScene(scene);
        primaryStage.show();
    } catch(Exception e) {
        e.printStackTrace();
    }

    Button button2 = new Button("Clear"); 
}

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

1 个答案:

答案 0 :(得分:0)

将按钮添加到根元素中的一个窗格,如下所示:

package application;

import javafx.application.Application; 
import static javafx.application.Application.launch; 
import javafx.geometry.Insets; 
import javafx.geometry.Pos; 

import javafx.scene.Scene; 
import javafx.scene.control.Button; 
import javafx.scene.control.PasswordField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane; 
import javafx.scene.text.Text; 
import javafx.scene.control.TextField; 
import javafx.stage.Stage;  


public class Main extends Application {
@Override
public void start(Stage primaryStage) {
    try {
        BorderPane root = new BorderPane();
        Button button2 = new Button("Clear"); 

        // this method is used to set the button to the top of the screen, check the documentation to get the other methods
        root.setTop(button2);

        Scene scene = new Scene(root,400,400);
        scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
        primaryStage.setScene(scene);
        primaryStage.show();
    } catch(Exception e) {
        e.printStackTrace();
    }


}

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