gpt4 book ai didi

JavaFX 不能使用 onAction

转载 作者:行者123 更新时间:2023-11-30 08:36:58 25 4
gpt4 key购买 nike

您好,我在使用 JavaFX 时遇到了一个奇怪的问题,我正在使用 Scene Builder 为您提供的 Controller 。问题是无论菜单栏上的菜单在单击时什么都不做。这是我的 Controller :

/**
* 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>

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

最佳答案

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

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

关于JavaFX 不能使用 onAction,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37512566/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com