JavaFX窗口未打开

huangapple go评论76阅读模式
英文:

JavaFX Window not opening

问题

if我运行Main类则我的JavaFX不会打开在我的菜单栏中会出现一个Java符号但没有窗口显示出来控制台没有显示任何错误我使用的是Mac机器配合使用eclipse以及e(fx)clipse和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>
英文:

if i run the Main Class my JavaFX is not opening. A Java symbol appears in my menubar but no window appears. I don't get any error in the console. I am on a Mac machine and I am using eclipse, together with e(fx)clipes and Gluon Scene Builder. The code is actually 1 by 1 written down from a tutorial. In the tutorial the run configuration JRE is set to JavaSE-1.8. - however, I tested every available JRE, without any success. What do I need to adjust?

Main Class:

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(&quot;MainWindow.fxml&quot;));	
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);
}
}

Controller class:

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 file:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;?import javafx.scene.control.Button?&gt;
&lt;?import javafx.scene.control.Label?&gt;
&lt;?import javafx.scene.control.TextField?&gt;
&lt;?import javafx.scene.layout.AnchorPane?&gt;
&lt;?import javafx.scene.layout.HBox?&gt;
&lt;?import javafx.scene.layout.VBox?&gt;
&lt;?import javafx.scene.text.Font?&gt;
&lt;AnchorPane prefHeight=&quot;400.0&quot; prefWidth=&quot;500.0&quot; xmlns=&quot;http://javafx.com/javafx/11.0.1&quot; xmlns:fx=&quot;http://javafx.com/fxml/1&quot; fx:controller=&quot;application.MainWindowController&quot;&gt;
&lt;children&gt;
&lt;VBox alignment=&quot;CENTER&quot; layoutY=&quot;83.0&quot; spacing=&quot;30.0&quot; AnchorPane.bottomAnchor=&quot;0.0&quot; AnchorPane.leftAnchor=&quot;0.0&quot; AnchorPane.rightAnchor=&quot;0.0&quot; AnchorPane.topAnchor=&quot;0.0&quot;&gt;
&lt;children&gt;
&lt;Label fx:id=&quot;label&quot; text=&quot;Label&quot;&gt;
&lt;font&gt;
&lt;Font size=&quot;24.0&quot; /&gt;
&lt;/font&gt;&lt;/Label&gt;
&lt;HBox alignment=&quot;CENTER&quot;&gt;
&lt;children&gt;
&lt;TextField fx:id=&quot;field&quot; prefWidth=&quot;220.0&quot; /&gt;
&lt;/children&gt;
&lt;/HBox&gt;
&lt;HBox alignment=&quot;CENTER&quot; spacing=&quot;20.0&quot;&gt;
&lt;children&gt;
&lt;Button fx:id=&quot;changeText&quot; mnemonicParsing=&quot;false&quot; onAction=&quot;#handleChangeText&quot; text=&quot;Change Text&quot; /&gt;
&lt;Button fx:id=&quot;clear&quot; mnemonicParsing=&quot;false&quot; onAction=&quot;#handleClear&quot; text=&quot;Clear&quot; /&gt;
&lt;/children&gt;
&lt;/HBox&gt;
&lt;/children&gt;
&lt;/VBox&gt;
&lt;/children&gt;
&lt;/AnchorPane&gt;

答案1

得分: 0

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

英文:

You didn't do anything in you start method. All you did was set your primaryStage variable, you should call your mainWindow method after that.

huangapple
  • 本文由 发表于 2020年8月23日 20:37:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/63547039.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定