JavaFx按钮不显示
我试图只在屏幕上显示一个按钮,但每次运行java程序时,都没有显示任何内容。我不知道为什么什么都没有出现 代码:JavaFx按钮不显示,java,macos,user-interface,button,javafx,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
将按钮添加到根元素中的一个窗格中,如下所示:
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);
}
}
本教程要求我将我的软件包从应用程序更改为其他软件包,这真的有必要吗?不,只需将按钮添加到场景中的部分,这是一个糟糕的教程。它只显示了如何使用
按钮作为场景的根。这个应该更适合:
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);
}
}