【问题标题】:JavaFx button does not showJavaFx 按钮不显示
【发布时间】:2018-10-11 10:41:48
【问题描述】:

我试图只在屏幕上显示一个按钮,但每次我运行 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);
}
}

【问题讨论】:

标签: java macos user-interface button javafx


【解决方案1】:

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

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);
}
}

【讨论】:

    猜你喜欢
    • 2014-07-28
    • 2019-04-27
    • 1970-01-01
    • 2015-07-22
    • 1970-01-01
    • 2017-12-17
    • 2021-10-15
    相关资源
    最近更新 更多