javafx应用程序中未显示javafx按钮
我在样品中做的纽扣。fxml在我使用scenebuilder创建的编译后没有显示。我也尝试过重新编译。在尝试了一切之后,我变得无助,问题仍然是他们。帮点忙就好了
梅因。爪哇
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
try {
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
Scene scene = new Scene(root, 300, 275);
primaryStage.setTitle("Hello World");
primaryStage.show();
} catch (Exception e)
{
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
控制员。爪哇
package sample;
import javafx.event.ActionEvent;
import java.util.Random;
public class Controller {
public void generateRandom(ActionEvent event)
{
Random rand = new Random();
int number = rand.nextInt(50)+1;
System.out.println(Integer.toString(number));
}
}
样品。fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane prefHeight="476.0" prefWidth="497.0" xmlns="http://javafx.com/javafx/8.0.121" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
<children>
<Button fx:id="clickme" layoutX="192.0" layoutY="299.0" mnemonicParsing="false" onAction="#generateRandom" prefHeight="52.0" prefWidth="93.0" text="Click me" />
<Label layoutX="155.0" layoutY="152.0" prefHeight="71.0" prefWidth="166.0" />
</children>
</AnchorPane>
# 1 楼答案
你在
Main
中创建了一个新的Scene
,但从未在舞台上设置场景在创建标题后添加
primaryStage.setScene(scene);
以将场景添加到舞台: