我已经查看了其他有相同问题的问题,并且我已经实现了所有解决方案,但我仍然遇到此错误:
Error resolving onMouseClicked='#handleButtonAction', either the event handler is not in the Namespace or there is an error in the script.
以下是我的main,controller和fxml文档:
package application;
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.fxml.FXMLLoader;
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
try {
AnchorPane root = (AnchorPane)FXMLLoader.load(getClass().getResource("Sample.fxml"));
Scene scene = new Scene(root,562.0,389.0);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
控制器:
package application;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Pane;
public class SampleController implements Initializable{
@FXML
private ImageView msgIcon, connectionIcon, settingsIcon, powerIcon, msgUp, connectionUp, settingsUp;
private Pane connectionPane, msgPane, settingsPane, tabPane;
@FXML
private void handleButtonAction(ActionEvent event)
{
if(event.getTarget() == msgIcon)
{
msgPane.setVisible(true);
connectionPane.setVisible(false);
settingsPane.setVisible(false);
msgUp.setVisible(true);
connectionUp.setVisible(false);
settingsUp.setVisible(false);
}
else if(event.getTarget() == connectionIcon)
{
msgPane.setVisible(false);
connectionPane.setVisible(true);
settingsPane.setVisible(false);
msgUp.setVisible(false);
connectionUp.setVisible(true);
settingsUp.setVisible(false);
}
else if(event.getTarget() == settingsIcon)
{
msgPane.setVisible(false);
connectionPane.setVisible(false);
settingsPane.setVisible(true);
msgUp.setVisible(false);
connectionUp.setVisible(false);
settingsUp.setVisible(true);
}
else if(event.getTarget() == powerIcon)
{
}
}
@Override
public void initialize(URL url, ResourceBundle rb) {
}
}
FXML: 这里有很多但是为ImageView msgIcon添加了事件处理程序。这仅仅导致错误。如果我删除了OnMouseClicked = ...则没有错误。
<?xml version="1.0" encoding="UTF-8"?>
<?package application?>
<?import com.jfoenix.controls.JFXButton?>
<?import com.jfoenix.controls.JFXComboBox?>
<?import com.jfoenix.controls.JFXListView?>
<?import com.jfoenix.controls.JFXTextArea?>
<?import com.jfoenix.controls.JFXTextField?>
<?import com.jfoenix.controls.JFXToggleButton?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.text.Font?>
<AnchorPane prefHeight="529.0" prefWidth="459.0" style="-fx-background-color: TRANSPARENT;" xmlns="http://javafx.com/javafx/9.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.SampleController">
<children>
<Pane fx:id="msgPane" layoutY="61.0" prefHeight="469.0" prefWidth="459.0" style="-fx-background-color: #2c2049;">
<children>
<Pane layoutX="141.0" prefHeight="467.0" prefWidth="318.0">
<children>
<JFXTextField focusColor="#405aa801" layoutX="15.0" layoutY="428.0" prefHeight="31.0" prefWidth="248.0" promptText="Messege..." style="-fx-border-radius: 10 10 10 10; -fx-background-color: #574387; -fx-background-radius: 10 10 10 10; -fx-prompt-text-fill: #ffffff;" unFocusColor="#4d4d4d00">
<opaqueInsets>
<Insets left="10.0" />
</opaqueInsets>
<padding>
<Insets left="10.0" />
</padding>
</JFXTextField>
<ImageView fitHeight="31.0" fitWidth="35.0" layoutX="275.0" layoutY="430.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../Resources/icons8_Sent_50px_2.png" />
</image>
</ImageView>
<JFXTextArea editable="false" prefHeight="414.0" prefWidth="318.0" />
</children>
</Pane>
<Pane prefHeight="467.0" prefWidth="141.0" style="-fx-border-color: #949299; -fx-border-style: hidden solid hidden hidden;">
<children>
<JFXListView prefHeight="467.0" prefWidth="140.0" style="-fx-background-color: #2c2049;" />
</children>
</Pane>
</children>
</Pane>
<Pane fx:id="tabPane" prefHeight="61.0" prefWidth="459.0" style="-fx-background-color: #2c2049; -fx-border-color: #949299; -fx-border-style: hidden hidden solid hidden;">
<children>
<ImageView fx:id="settingsIcon" fitHeight="31.0" fitWidth="31.0" layoutX="286.0" layoutY="17.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../Resources/icons8_Settings_64px_1.png" />
</image>
</ImageView>
<ImageView fx:id="connectionIcon" fitHeight="31.0" fitWidth="31.0" layoutX="156.0" layoutY="17.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../Resources/icons8_Add_New_48px_1.png" />
</image>
</ImageView>
<ImageView fx:id="msgIcon" fitHeight="41.0" fitWidth="38.0" layoutX="24.0" layoutY="14.0" onMouseClicked="#handleButtonAction" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../Resources/icons8_Video_Chat_64px_1.png" />
</image>
</ImageView>
<ImageView fx:id="powerIcon" fitHeight="31.0" fitWidth="24.0" layoutX="404.0" layoutY="21.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../Resources/icons8_Shutdown_52px.png" />
</image>
</ImageView>
<ImageView fx:id="msgUp" fitHeight="24.0" fitWidth="17.0" layoutX="35.0" layoutY="44.0" pickOnBounds="true" preserveRatio="true" visible="false">
<image>
<Image url="@../Resources/icons8_Sort_Up_52px.png" />
</image>
</ImageView>
<ImageView fx:id="connectionUp" fitHeight="24.0" fitWidth="17.0" layoutX="163.0" layoutY="44.0" pickOnBounds="true" preserveRatio="true" visible="false">
<image>
<Image url="@../Resources/icons8_Sort_Up_52px.png" />
</image>
</ImageView>
<ImageView fx:id="settingsUp" fitHeight="24.0" fitWidth="17.0" layoutX="293.0" layoutY="44.0" pickOnBounds="true" preserveRatio="true" visible="false">
<image>
<Image url="@../Resources/icons8_Sort_Up_52px.png" />
</image>
</ImageView>
</children>
</Pane>
<Pane fx:id="msgPane" layoutY="60.0" prefHeight="469.0" prefWidth="459.0" style="-fx-background-color: #2c2049;" visible="false">
<children>
<Label layoutX="108.0" layoutY="50.0" prefHeight="31.0" prefWidth="89.0" text="Theme:" textFill="#dcdada">
<font>
<Font name="System Bold" size="12.0" />
</font>
</Label>
<JFXComboBox layoutX="256.0" layoutY="53.0" prefHeight="25.0" prefWidth="106.0" style="-fx-background-color: #4f3b7c;" />
</children>
</Pane>
<Pane fx:id="connectionPane" layoutY="60.0" prefHeight="469.0" prefWidth="459.0" style="-fx-background-color: #2c2049;" visible="false">
<children>
<Label layoutX="90.0" layoutY="59.0" prefHeight="25.0" prefWidth="76.0" text="Username" textFill="#a9a9a9">
<font>
<Font size="15.0" />
</font>
</Label>
<JFXTextField focusColor="#4f3b7c" layoutX="193.0" layoutY="59.0" prefHeight="25.0" prefWidth="175.0" style="-fx-prompt-text-fill: #a9a9a9; -fx-text-inner-color: #a9a9a9;" />
<Label layoutX="77.0" layoutY="103.0" prefHeight="25.0" prefWidth="89.0" text="Port Number" textFill="#a9a9a9">
<font>
<Font size="15.0" />
</font>
</Label>
<JFXTextField focusColor="#4f3b7c" layoutX="193.0" layoutY="103.0" prefHeight="25.0" prefWidth="175.0" style="-fx-prompt-text-fill: #a9a9a9; -fx-text-inner-color: #a9a9a9;" />
<Label layoutX="97.0" layoutY="151.0" prefHeight="25.0" prefWidth="63.0" text="Target IP" textFill="#a9a9a9">
<font>
<Font size="15.0" />
</font>
</Label>
<JFXTextField focusColor="#4f3b7c" layoutX="193.0" layoutY="151.0" prefHeight="25.0" prefWidth="175.0" style="-fx-prompt-text-fill: #a9a9a9; -fx-text-inner-color: #a9a9a9;" />
<JFXToggleButton layoutX="82.0" layoutY="199.0" text="Show Time" textFill="DARKGRAY" toggleColor="#4f3b7c" toggleLineColor="#7a5fb7" />
<JFXToggleButton layoutX="255.0" layoutY="199.0" text="Hosting" textFill="DARKGRAY" toggleColor="#4f3b7c" toggleLineColor="#7a5fb7" />
<JFXButton layoutX="93.0" layoutY="280.0" prefHeight="38.0" prefWidth="106.0" style="-fx-background-color: #4f3b7c;" text="Save Connection" textFill="WHITE" />
<JFXButton layoutX="258.0" layoutY="280.0" prefHeight="38.0" prefWidth="106.0" style="-fx-background-color: #4f3b7c;" text="Load Connection" textFill="WHITE" />
<JFXButton layoutX="93.0" layoutY="347.0" prefHeight="38.0" prefWidth="106.0" style="-fx-background-color: #4f3b7c;" text="Validate" textFill="WHITE" />
<JFXButton layoutX="258.0" layoutY="347.0" prefHeight="38.0" prefWidth="106.0" style="-fx-background-color: #4f3b7c;" text="Connect" textFill="WHITE" />
</children>
</Pane>
</children>
</AnchorPane>
我觉得这是我缺少/不理解的根本。请让我知道您认为我的问题是什么。感谢。