gpt4 book ai didi

java - 所有事件均不适用于 JavaFX 的 Eclipse

转载 作者:行者123 更新时间:2023-11-30 03:45:05 26 4
gpt4 key购买 nike

我正在尝试将在 JavaFX SceneBuilder 中创建的 GUI 集成到 java 程序中。我在 Accordion 容器内有一些按钮、 slider 和复选框,并且这些对象的事件从未得到处理。它总是给出这个错误:

Exception in thread "JavaFX Application Thread" java.lang.IllegalArgumentException: argument type mismatch
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.reflect.misc.Trampoline.invoke(Unknown Source)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.reflect.misc.MethodUtil.invoke(Unknown Source)
at javafx.fxml.FXMLLoader$MethodHandler.invoke(Unknown Source)
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(Unknown Source)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source)
at com.sun.javafx.event.EventUtil.fireEvent(Unknown Source)
at javafx.event.Event.fireEvent(Unknown Source)
at javafx.scene.Scene$ClickGenerator.postProcess(Unknown Source)
at javafx.scene.Scene$ClickGenerator.access$7900(Unknown Source)
at javafx.scene.Scene$MouseHandler.process(Unknown Source)
at javafx.scene.Scene$MouseHandler.access$1500(Unknown Source)
at javafx.scene.Scene.impl_processMouseEvent(Unknown Source)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Unknown Source)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(Unknown Source)
at com.sun.glass.ui.View.handleMouseEvent(Unknown Source)
at com.sun.glass.ui.View.notifyMouse(Unknown Source)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$141(Unknown Source)
at com.sun.glass.ui.win.WinApplication$$Lambda$37/1109371569.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

如何注册这些事件?我在JavaFX网站上研究过:http://docs.oracle.com/javafx/2/events/convenience_methods.htm和网络的其他部分,但我找不到一种方法来让代码捕获事件。

这是我的 FXML 文件:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.media.*?>
<?import javafx.scene.image.*?>
<?import javafx.scene.*?>
<?import javafx.scene.text.*?>
<?import javafx.scene.effect.*?>
<?import javafx.scene.canvas.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>

<Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="583.0" prefWidth="900.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="edu.westga.PictureDrawer.main.MainClass">
<children>
<Accordion fx:id="theMainMenu" onMouseClicked="#useMenu" prefHeight="583.0" prefWidth="202.0">
<panes>
<TitledPane text="FILE">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="225.0" prefWidth="200.0">
<children>
<Group>
<children>
<Button id="uploadImageButton" fx:id="uploadImageButton" layoutX="22.0" layoutY="38.0" mnemonicParsing="false" prefHeight="25.0" prefWidth="159.0" text="Upload" />
<Label alignment="CENTER" layoutX="19.0" layoutY="14.0" prefHeight="25.0" prefWidth="164.0" text="Open From File" textAlignment="CENTER">
<font>
<Font name="System Bold" size="14.0" />
</font>
</Label>
</children>
</Group>
<Separator layoutY="64.0" prefHeight="12.0" prefWidth="183.0" />
<Group>
<children>
<TextField id="imageLinkBox" fx:id="imageLinkBox" alignment="CENTER" layoutX="7.0" layoutY="95.0" prefHeight="50.0" prefWidth="182.0" promptText="(insert web link)">
<font>
<Font name="System Bold" size="10.0" />
</font>
</TextField>
<Button id="downloadImageButton" fx:id="downloadImageButton" layoutX="19.0" layoutY="156.0" mnemonicParsing="false" prefHeight="25.0" prefWidth="159.0" text="Download" />
<Label alignment="CENTER" layoutX="17.0" layoutY="70.0" prefHeight="25.0" prefWidth="164.0" text="Open From Internet" textAlignment="CENTER">
<font>
<Font name="System Bold" size="14.0" />
</font>
</Label>
</children>
</Group>
<Separator layoutX="1.0" layoutY="189.0" prefHeight="12.0" prefWidth="200.0" />
<Group>
<children>
<Button fx:id="replaceBothImages" layoutX="22.0" layoutY="386.0" mnemonicParsing="false" onMouseClicked="#replaceImages" prefHeight="25.0" prefWidth="159.0" text="Replace Images" />
<CheckBox fx:id="replaceEditedImage" layoutX="17.0" layoutY="422.0" mnemonicParsing="false" onMouseClicked="#toggleSelection" prefHeight="20.0" prefWidth="169.0" text="Replace Edited Image">
<font>
<Font size="14.0" />
</font>
</CheckBox>
<ImageView id="loadedImagePreview" fx:id="loadedImagePreview" cache="true" fitHeight="163.0" fitWidth="192.0" layoutX="5.0" layoutY="201.0" pickOnBounds="true">
<cursor>
<Cursor fx:constant="DISAPPEAR" />
</cursor>
<image>
<Image url="@no.jpg" />
</image>
</ImageView>
</children>
</Group>
</children>
</AnchorPane>
</content>
</TitledPane>
<TitledPane text="EDIT">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
<children>
<Group layoutX="-5.0" layoutY="1.0">
<children>
<Label alignment="CENTER" layoutX="4.0" layoutY="1.0" prefHeight="38.0" prefWidth="144.0" text="Pixels Per Width">
<font>
<Font name="System Bold" size="14.0" />
</font>
</Label>
<Slider fx:id="pixelsPerWidthSlider" layoutX="7.0" layoutY="36.0" onScroll="#changePixelPerSquare" prefHeight="23.0" prefWidth="186.0">
<effect>
<DropShadow spread="0.16" />
</effect>
</Slider>
<Label fx:id="pixelsPerWidth" alignment="CENTER" layoutX="151.0" layoutY="7.0" prefHeight="26.0" prefWidth="40.0" text="0">
<font>
<Font name="System Bold" size="14.0" />
</font>
</Label>
</children>
</Group>
<Group layoutX="-5.0" layoutY="1.0">
<children>
<Label id="pixelsPerLength" fx:id="pixelsPerLength" alignment="CENTER" layoutX="150.0" layoutY="67.0" prefHeight="26.0" prefWidth="42.0" text="0">
<font>
<Font name="System Bold" size="14.0" />
</font>
</Label>
<Slider id="pixelsPerLengthSlider" fx:id="pixelsPerLengthSlider" layoutX="7.0" layoutY="97.0" onScroll="#changePixelPerSquare" prefHeight="23.0" prefWidth="186.0" snapToTicks="true">
<effect>
<DropShadow spread="0.16" />
</effect>
</Slider>
<Label fx:id="pixelsPerLength" alignment="CENTER" layoutX="4.0" layoutY="61.0" prefHeight="38.0" prefWidth="144.0" text="Pixels Per Length">
<font>
<Font name="System Bold" size="14.0" />
</font>
</Label>
</children>
</Group>
</children>
</AnchorPane>
</content>
</TitledPane>
<TitledPane text="SETTINGS">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
<children>
<Group>
<children>
<Group>
<children>
<RadioButton fx:id="jpegRadioButton" layoutX="28.0" layoutY="41.0" mnemonicParsing="false" onMouseClicked="#changeRadioButtonSetValue" prefHeight="36.0" prefWidth="51.0" selected="true" text="JPEG" textAlignment="CENTER">
<toggleGroup>
<ToggleGroup fx:id="fileType" />
</toggleGroup></RadioButton>
<RadioButton fx:id="pngRadioButton" layoutX="110.0" layoutY="41.0" mnemonicParsing="false" onMouseClicked="#changeRadioButtonSetValue" prefHeight="36.0" prefWidth="51.0" text="PNG" textAlignment="CENTER" toggleGroup="$fileType" />
<RadioButton fx:id="gifRadioButton" layoutX="110.0" layoutY="77.0" mnemonicParsing="false" onMouseClicked="#changeRadioButtonSetValue" prefHeight="36.0" prefWidth="51.0" selected="true" text="GIF" textAlignment="CENTER" toggleGroup="$fileType" />
<RadioButton fx:id="jpgRadioButton" layoutX="28.0" layoutY="77.0" mnemonicParsing="false" onMouseClicked="#changeRadioButtonSetValue" prefHeight="36.0" prefWidth="51.0" text="JPG" textAlignment="CENTER" toggleGroup="$fileType" />
</children>
</Group>
<Label alignment="CENTER" layoutX="1.0" layoutY="14.0" prefHeight="29.0" prefWidth="184.0" text="Default File Type for Save" textAlignment="CENTER" textOverrun="LEADING_WORD_ELLIPSIS">
<font>
<Font name="System Bold" size="14.0" />
</font>
</Label>
</children>
</Group>
<Separator layoutY="114.0" prefHeight="12.0" prefWidth="200.0" />
<Group>
<children>
<Label alignment="CENTER" layoutX="16.0" layoutY="129.0" prefHeight="17.0" prefWidth="169.0" text="Zoom">
<font>
<Font name="System Bold" size="14.0" />
</font>
</Label>
<Group layoutY="13.0">
<children>
<Label alignment="CENTER" layoutX="9.0" layoutY="137.0" prefHeight="27.0" prefWidth="84.0" text="Edited Image">
<font>
<Font name="System Bold" size="12.0" />
</font>
</Label>
<Slider fx:id="zoomEditedImageSlider" layoutX="4.0" layoutY="167.0" majorTickUnit="10.0" minorTickCount="1" onMouseClicked="#changeZoomLevel" prefHeight="33.0" prefWidth="193.0" snapToTicks="true">
<effect>
<DropShadow spread="0.16" />
</effect>
</Slider>
<Label fx:id="zoomEditedImagePercentage" alignment="CENTER" layoutX="110.0" layoutY="138.0" prefHeight="25.0" prefWidth="85.0" text="0">
<font>
<Font name="System Bold" size="14.0" />
</font>
</Label>
</children>
</Group>
<Group layoutY="13.0">
<children>
<Slider fx:id="zoomOriginalImageSlider" layoutX="4.0" layoutY="229.0" onScroll="#changeZoomLevel" prefHeight="33.0" prefWidth="193.0">
<effect>
<DropShadow spread="0.16" />
</effect>
</Slider>
<Label alignment="CENTER" layoutX="9.0" layoutY="202.0" prefHeight="27.0" prefWidth="84.0" text="Original Image">
<font>
<Font name="System Bold" size="12.0" />
</font>
</Label>
<Label fx:id="zoomOriginalImagePercentage" alignment="CENTER" layoutX="107.0" layoutY="204.0" prefHeight="25.0" prefWidth="85.0" text="0">
<font>
<Font name="System Bold" size="14.0" />
</font>
</Label>
</children>
</Group>
</children>
</Group>
<Separator layoutY="271.0" prefHeight="12.0" prefWidth="200.0" />
<Group>
<children>
<Label alignment="CENTER" layoutX="8.0" layoutY="283.0" prefHeight="29.0" prefWidth="184.0" text="Show/Hide Image Views" textAlignment="CENTER" textOverrun="LEADING_WORD_ELLIPSIS">
<font>
<Font name="System Bold" size="14.0" />
</font>
</Label>
<Group>
<children>
<Label alignment="CENTER" layoutY="312.0" prefHeight="27.0" prefWidth="92.0" text="Original Image">
<font>
<Font name="System Bold" size="12.0" />
</font>
</Label>
<ToggleButton fx:id="toggleOriginalImageVisibilty" layoutX="100.0" layoutY="313.0" mnemonicParsing="false" onMouseClicked="#toggleOrignialImageVisibility" selected="true" text="Show/Hide" />
</children>
</Group>
<Group>
<children>
<Label alignment="CENTER" layoutY="349.0" prefHeight="27.0" prefWidth="92.0" text="Edited Image">
<font>
<Font name="System Bold" size="12.0" />
</font>
</Label>
<ToggleButton fx:id="toggleEditedImageVisibility" layoutX="100.0" layoutY="350.0" mnemonicParsing="false" onMouseClicked="#toggleEditedImageVisibility" text="Show/Hide" />
</children>
</Group>
</children>
</Group>
</children>
</AnchorPane>
</content>
</TitledPane>
</panes>
<cursor>
<Cursor fx:constant="HAND" />
</cursor>
</Accordion>
<SplitPane dividerPositions="0.5" layoutX="202.0" prefHeight="583.0" prefWidth="698.0">
<items>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="578.0" prefWidth="345.0">
<children>
<ImageView fx:id="editedImage" blendMode="SRC_ATOP" fitHeight="561.0" fitWidth="322.0" layoutX="12.0" layoutY="13.0" mouseTransparent="true" pickOnBounds="true">
<image>
<Image url="@no%20takebacks.jpg" />
</image></ImageView>
</children>
</AnchorPane>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0">
<children>
<ImageView fx:id="originalImage" blendMode="SRC_ATOP" fitHeight="561.0" fitWidth="322.0" layoutX="12.0" layoutY="13.0" mouseTransparent="true" pickOnBounds="true">
<image>
<Image url="@no%20know%20answer.jpg" />
</image></ImageView>
</children>
</AnchorPane>
</items>
</SplitPane>
</children>
</Pane>

这是我的代码:

import java.io.IOException;

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
import javafx.fxml.FXML;
import javafx.scene.control.Accordion;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Label;
import javafx.scene.control.RadioButton;
import javafx.scene.control.Slider;
import javafx.scene.control.TextField;
import javafx.scene.control.ToggleButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;


public class MainClass extends Application {

@FXML
private Accordion theMainMenu;

@FXML
private TextField imageLinkBox;

@FXML
private Label pixelsPerWidth;

@FXML
private Slider pixelsPerWidthSlider;

@FXML
private RadioButton gifRadioButton;

@FXML
private RadioButton jpgRadioButton;

@FXML
private Slider zoomEditedImageSlider;

@FXML
private ImageView editedImage;

@FXML
private ToggleButton toggleOriginalImageVisibilty;

@FXML
private ImageView loadedImagePreview;

@FXML
private Label zoomOriginalImagePercentage;

@FXML
private Slider pixelsPerLengthSlider;

@FXML
private RadioButton pngRadioButton;

@FXML
private Button downloadImageButton;

@FXML
private Button replaceBothImages;

@FXML
private Label zoomEditedImagePercentage;

@FXML
private Button uploadImageButton;

@FXML
private Label pixelsPerLength;

@FXML
private ToggleButton toggleEditedImageVisibility;

@FXML
private CheckBox replaceEditedImage;

@FXML
private RadioButton jpegRadioButton;

@FXML
private Slider zoomOriginalImageSlider;

@FXML
private ImageView originalImage;

@FXML
private ToggleGroup fileType;


public static void main(String[] args) {
launch(args);
}

@Override
public void start(Stage primaryStage) {
try {
Pane root = FXMLLoader.load(getClass().getClassLoader().getResource(".\\edu\\westga\\PictureDrawer\\main\\test.fxml"));
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
} catch (IOException e) {
e.printStackTrace();
return;
}
}

@FXML
public void useMenu(ActionEvent event) {
System.out.println("hello8");
}

@FXML
public void replaceImages(ActionEvent event) {
System.out.println("hello7");
}

@FXML
public void toggleSelection(ActionEvent event) {
System.out.println("hello6");
}

@FXML
public void changePixelPerSquare(ActionEvent event) {
System.out.println("hello5");
}

@FXML
public void changeRadioButtonSetValue(ActionEvent event) {
System.out.println("hello4");
}

@FXML
public void changeZoomLevel(ActionEvent event) {
System.out.println("hello3");
if (event.getSource() == this.zoomEditedImageSlider) {
this.setValueOfLabel();
}
}

private void setValueOfLabel() {
this.zoomEditedImageSlider.valueChangingProperty().addListener(new ChangeListener<Boolean>() {
@Override
public void changed(
ObservableValue<? extends Boolean> observableValue,
Boolean wasChanging,
Boolean changing) {
String valueString = zoomEditedImageSlider.getValue() + "%";

if (wasChanging) {
zoomEditedImagePercentage.setText(valueString);
}
}
});
}

@FXML
public void toggleOrignialImageVisibility(ActionEvent event) {
System.out.println("hello2");
}

@FXML
public void toggleEditedImageVisibility(ActionEvent event) {
System.out.println("hello1");
}
}

最佳答案

问题

在 FXML 中,您使用 onMouseClicked="#nameOfTheMethod",在 Controller 中,您的方法如下

@FXML 
public void nameOfTheMethod(ActionEvent event) {
//Logic
}

每当调用该方法时,它都会传递 MouseEvent,而您的方法需要 ActionEvent,因此 java.lang.IllegalArgumentException:参数类型不匹配

解决方案

  1. 您可以将 FXML 内 ButtonsonMouseClicked 更改为 onAction
  2. 对于其他控件,请更改 Controller 内的方法声明。将参数从 ActionEvent 更改为 MouseEvent

注意将添加到 zoomEditedImageSlider 的changeListener移动到setValueOfLabel()内部以初始化 Controller 的方法。向控件添加监听器应在初始化内部完成。

关于java - 所有事件均不适用于 JavaFX 的 Eclipse,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25954066/

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