JavaFX窗口无法打开

如果我运行Main Class,则JavaFX无法打开。 Java符号出现在我的菜单栏中,但没有窗口出现。我在控制台中没有任何错误。我在Mac机器上,正在使用eclipse,以及e(fx)clipes和Gluon Scene Builder。该代码实际上是从教程中写下来的一对一的代码。在本教程中,运行配置JRE设置为JavaSE-1.8。 -但是,我测试了每个可用的JRE,但均未成功。我需要调整什么?

主类:

package application;

import java.io.IOException;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;


public class Main extends Application {
private Stage primaryStage; 


@Override
public void start(Stage primaryStage) {
    this.primaryStage = primaryStage; 
}

public void mainWindow() {
    try {
        FXMLLoader loader = new FXMLLoader(Main.class.getResource("MainWindow.fxml"));  
        AnchorPane pane = loader.load();
    
        primaryStage.setMinHeight(400.00);
        primaryStage.setMinWidth(500.00);
        
        MainWindowController mainWindowController = loader.getcontroller();
        mainWindowController.setMain(this);
        
        Scene scene = new Scene(pane);
        
        primaryStage.setScene(scene);
        primaryStage.show();
        
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

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

控制器类:

package application;

import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;

public class MainWindowController {

public Main main;
@FXML private Label label;
@FXML private TextField field;
@FXML private Button clear;
@FXML private Button changeText;


public void setMain(Main main) {
    // TODO Auto-generated method stub
    this.main = main;
}

 @FXML
 public void handleChangeText() {
     String text = field.getText();
     label.setText(text);
      
 }
 
 @FXML
 public void handleclear() {
     field.clear();
 }

}

FXML文件:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>

<AnchorPane prefHeight="400.0" prefWidth="500.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.MainWindowController">
   <children>
      <VBox alignment="CENTER" layoutY="83.0" spacing="30.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
         <children>
            <Label fx:id="label" text="Label">
               <font>
                  <Font size="24.0" />
               </font></Label>
            <HBox alignment="CENTER">
               <children>
                  <TextField fx:id="field" prefWidth="220.0" />
               </children>
            </HBox>
            <HBox alignment="CENTER" spacing="20.0">
               <children>
                  <Button fx:id="changeText" mnemonicParsing="false" onaction="#handleChangeText" text="Change Text" />
                  <Button fx:id="clear" mnemonicParsing="false" onaction="#handleclear" text="Clear" />
               </children>
            </HBox>
         </children>
      </VBox>
   </children>
</AnchorPane>
iCMS 回答:JavaFX窗口无法打开

您在启动方法中没有执行任何操作。您所做的只是设置了primaryStage变量,之后应调用mainWindow方法。

本文链接:https://www.f2er.com/1696177.html

大家都在问