gpt4 book ai didi

单击按钮文本时 JavaFX 按钮出现错误

转载 作者:行者123 更新时间:2023-12-01 22:56:18 26 4
gpt4 key购买 nike

我有一个 4 个按钮,当我单击按钮的文本时,会出现错误,但是当我单击没有文本的按钮部分时,它会起作用。

这是事件代码:

@FXML
public void starClick(MouseEvent event){
event.consume();
try{
Button btn = (Button)event.getTarget();
}catch(Exception e){
e.printStackTrace();
}
//UIActions.setStarClick(Integer.parseInt(btn.getText()));
}

以下是其中一个按钮的 XML:

<Button fx:id="star1" disable="false" focusTraversable="false" minHeight="0.0" minWidth="0.0" mnemonicParsing="false" onMouseClicked="#starClick" onMouseEntered="#starHover" onMouseExited="#starExit" prefHeight="20.0" prefWidth="25.0" styleClass="button-star" text="1" textFill="TRANSPARENT" visible="true">
<stylesheets>
<URL value="@../css/button.css" />
</stylesheets>
</Button>

这是单击时的输出:

java.lang.ClassCastException: com.sun.javafx.scene.control.skin.LabeledText cannot be cast to javafx.scene.control.Button
at phantom.DocumentController.starClick(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1763)
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1651)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:204)
at javafx.scene.Scene$ClickGenerator.postProcess(Scene.java:3456)
at javafx.scene.Scene$ClickGenerator.access$8300(Scene.java:3387)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3755)
at javafx.scene.Scene$MouseHandler.access$1800(Scene.java:3471)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1695)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2486)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:314)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:243)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:345)
at com.sun.glass.ui.View.handleMouseEvent(View.java:526)
at com.sun.glass.ui.View.notifyMouse(View.java:898)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.access$300(WinApplication.java:39)
at com.sun.glass.ui.win.WinApplication$4$1.run(WinApplication.java:112)
at java.lang.Thread.run(Thread.java:745)

最佳答案

我不确定确切的原因,但可能是因为 Button 上的 Label 也调用了相同的方法而导致错误。您尝试过使用

onAction="#starClick"

而不是

onMouseClicked="#starClick"

关于单击按钮文本时 JavaFX 按钮出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23998044/

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