2
你好,我有一個奇怪的問題與JavaFX我使用的控制器場景生成器給你。 問題在於無論點擊菜單欄上的菜單什麼都不做。 這是我的控制器:JavaFX不能使用onAction
/**
* Sample Skeleton for 'Main.fxml' Controller Class
*/
package com.agronaut022.gui_changer.GUI.Scenes.Main;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLDecoder;
import java.security.CodeSource;
import java.util.ResourceBundle;
import org.apache.commons.configuration.ConfigurationException;
import com.agronaut022.gui_changer.GUI.classes.MessageBox;
import com.agronaut022.gui_changer.etc.Config;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ListView;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.ProgressBar;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
import junit.framework.Test;
public class MainController
`enter code here`{
@FXML
private ResourceBundle resources;
@FXML
private URL location;
@FXML
private ListView<?> ListView;
@FXML
private Button Change_Button;
@FXML
private Menu Help;
@FXML
private Menu about;
@FXML
private MenuBar MenuBar;
@FXML
private Pane pane;
@FXML
private ProgressBar ProgressBar;
@FXML
void initialize() {
assert Help != null : "fx:id=\"Help\" was not injected: check your FXML file 'Main.fxml'.";
assert ListView != null : "fx:id=\"ListView\" was not injected: check your FXML file 'Main.fxml'.";
assert Change_Button != null : "fx:id=\"Change_Button\" was not injected: check your FXML file 'Main.fxml'.";
assert about != null : "fx:id=\"about\" was not injected: check your FXML file 'Main.fxml'.";
assert MenuBar != null : "fx:id=\"MenuBar\" was not injected: check your FXML file 'Main.fxml'.";
assert pane != null : "fx:id=\"pane\" was not injected: check your FXML file 'Main.fxml'.";
assert ProgressBar != null : "fx:id=\"ProgressBar\" was not injected: check your FXML file 'Main.fxml'.";
Help.setOnAction(new EventHandler<ActionEvent>()
{
@Override
public void handle(ActionEvent event)
{
System.out.println("ABout");
Parent root;
try
{
root = FXMLLoader.load(getClass().getClassLoader().getResource("com/agronaut022/gui_changer/GUI/Scenes/About/About.fxml"),
resources);
Stage stage = new Stage();
stage.setTitle("About");
stage.setScene(new Scene(root, 600, 425));
stage.show();
} catch (IOException e)
{
e.printStackTrace();
}
}
});
/* Change_Button.setOnAction(new EventHandler<ActionEvent>()
{
@Override
public void handle(ActionEvent event)
{
Parent root;
try
{
root = FXMLLoader.load(getClass().getClassLoader().getResource("com/agronaut022/gui_changer/GUI/Scenes/About/About.fxml"),
resources);
Stage stage = new Stage();
stage.setTitle("About");
stage.setScene(new Scene(root, 600, 425));
stage.show();
} catch (IOException e)
{
e.printStackTrace();
}
}
}); */
}
}
而且我FXML文件爲這
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<Pane fx:id="pane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="- Infinity" minWidth="-Infinity" prefHeight="600.0" prefWidth="350.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.agronaut022.gui_changer.GUI.Scenes.Main.MainController">
<children>
<MenuBar id="MenuBar" fx:id="MenuBar" layoutY="2.0" prefHeight="25.0" prefWidth="350.0">
<menus>
<Menu mnemonicParsing="false" text="File">
<items>
<MenuItem mnemonicParsing="false" text="Import GUI" />
<MenuItem mnemonicParsing="false" text="Change Starmade Directory" />
<MenuItem mnemonicParsing="false" text="Create Details" />
</items></Menu>
<Menu mnemonicParsing="false" text="Backup" />
<Menu mnemonicParsing="false" text="Settings" />
<Menu fx:id="about" mnemonicParsing="false" text="About" />
<Menu fx:id="Help" mnemonicParsing="false" text="Help" />
</menus>
</MenuBar>
<ListView fx:id="ListView" layoutX="12.0" layoutY="43.0" prefHeight="450.0" prefWidth="326.0" />
<ProgressBar fx:id="ProgressBar" layoutX="12.0" layoutY="499.0" prefHeight="35.0" prefWidth="326.0" progress="0.0" />
<Button id="Change_Button" fx:id="Change_Button" layoutX="12.0" layoutY="540.0" mnemonicParsing="false" prefHeight="48.0" prefWidth="326.0" text="Button" />
</children>
</Pane>
,我沒有得到任何錯誤,並在PROGRAMM正確啓動, 我已經嘗試使用不同勢菜單項,但是同樣的結果。
哦謝謝,不知道這個,我爭取它會像在WindowsForms。 – Agronaut022