我刚刚使用netbins IDE使用javaFX创建了一个非常小的项目,用户将获得一些随机数。这是我的输出:
我的问题是:
我的代码:
GenerateRandomNumber.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package generaterandomnumber;
import java.io.IOException;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
/**
*
* @author USER
*/
public class GenerateRandomNumber extends Application {
private Stage stage;
private double decorationWidth;
private double decorationHeight;
@Override
public void start(Stage primaryStage) throws Exception {
this.stage = stage;
final double initialSceneWidth = 500;
final double initialSceneHeight = 300;
final Parent root=FXMLLoader.load(getClass().getResource("main.fxml"));
Scene scene = new Scene(root, initialSceneWidth, initialSceneHeight);
scene.getStylesheets().add(getClass().getResource("main.css").toExternalForm());
primaryStage.setTitle("Generating Random Numbers");
primaryStage.setScene(scene);
primaryStage.show();
this.decorationWidth = initialSceneWidth - scene.getWidth();
this.decorationHeight = initialSceneHeight - scene.getHeight();
}
public static void main(String[] args) {
launch(args);
}
}
main.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import java.net.URL?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane id="AnchorPane" prefHeight="300.0" prefWidth="500.0" styleClass="mainFxmlClass" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.141" fx:controller="generaterandomnumber.MainController">
<stylesheets>
<URL value="@main.css" />
</stylesheets>
<children>
<Button fx:id="Clickme" layoutX="209.0" layoutY="182.0" mnemonicParsing="false" onAction="#generateRandom" prefHeight="32.0" prefWidth="85.0" text="Click me" />
<Label fx:id="myMessage" layoutX="139.0" layoutY="53.0" prefHeight="96.0" prefWidth="220.0" />
<Button fx:id="Exit" layoutX="226.0" layoutY="228.0" mnemonicParsing="false" onAction="#exit" prefHeight="25.0" prefWidth="50.0" text="Exit" />
</children>
</AnchorPane>
的main.css
.mainFxmlClass {
}
#Clickme{
-fx-font-size: 15px;
-fx-background-color: blue;
-fx-text-fill: white;
-fx-padding: 6 6 6 6;
-fx-border-radius: 8;
-fx-fill-width: bold;
}
#Exit{
-fx-font-size: 15px;
-fx-background-color: blueviolet;
-fx-text-fill: white;
-fx-fill-width: bold;
}
#myMessage{
-fx-font-size: 25px;
-fx-background-color:wheat;
-fx-text-fill: purple;
-fx-padding: 6 6 6 6;
-fx-border-radius: 8;
-fx-fill-width: bold;
-fx-text-alignment: center;
}
.root{
-fx-background-color:whitesmoke;
}
MainController.java
package generaterandomnumber;
import java.net.URL;
import java.util.Random;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
public class MainController implements Initializable {
@FXML
private Label myMessage;
public void generateRandom(ActionEvent event){
Random rand = new Random();
int myrand = rand.nextInt(50)+1;
myMessage.setText(Integer.toString(myrand));
}
public void exit(ActionEvent event){
System.exit(0);
}
@Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
}
答案 0 :(得分:2)
要将生成的数字放在中心,您必须将 alignment =&#34; CENTER&#34; 添加到标签元素:
<Label fx:id="myMessage" alignment="CENTER" layoutX="139.0" layoutY="53.0" prefHeight="96.0" prefWidth="220.0">
要修复窗口大小,请添加到 GenerateRandomNumber.start()方法:
primaryStage.setResizable(false);
setResizable()方法允许设置属性resizable
的值