JavaFX不能使用onAction

时间:2016-05-29 16:56:26

标签: java javafx

你好我有JavaFX的奇怪问题我正在使用Controller Scene Builder为您提供。 问题是无关紧要单击时菜单栏上的菜单什么都不做。 这是我的控制器:

/**
* 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 FIle就是这个

<?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>

我没有收到任何错误并且程序正确启动, 我已尝试使用不同的菜单项但结果相同。

1 个答案:

答案 0 :(得分:2)

那是因为Menu没有至少Menuitem的人不会发生事件。

已经存在像你这样的问题了。这是一个解决方案 https://stackoverflow.com/a/19006643/5702956