JavaFX鼠标事件抛出异常

时间:2015-10-29 15:37:38

标签: exception button javafx onclick

我有这个代码,它使用JavaFX控件,代码“fi.setOnAction(e - > fi.setStyle(” - fx-background-color:#00C275;“));”应该更改按钮的颜色fi,但是不起作用(抛出异常),但有趣的是它可以在除了按钮之外的所有其他代码之后工作并且取消了setOnAction:

import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.beans.property.SimpleStringProperty;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Control;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.scene.control.*;


public class Medow extends Application {
public static void main(String arg[]) {
    launch(arg);
}

public void start(Stage primarystage) {
    primarystage.setTitle("Medow");
    primarystage.setResizable(false);

    // Label l = new Label("Hello");
    // setXY( 0, 0, l);

    Image image = new Image("/modified/user.jpg");
    ImageView iv1 = new ImageView();
    iv1.setImage(image);
    iv1.setFitWidth(93.75);
    iv1.setPreserveRatio(true);
    //iv1.setX(75);
    //iv1.setY(75);


    Circle c = new Circle(46.875, 46.875, 46.875, Color.BLUE); // Previously it was 75, 75 - after adding image the Circle Moves Down, and changing it to 93.75/2 = 46.875 fixes this.
    iv1.setClip(c);




    VBox v1 = new VBox(iv1); // Upper Left VBox - inside vb
    v1.setMinHeight(170);
    v1.setPadding(new Insets(28.125, 0, 0, 28.125));

    Label fa = new Label("Farming");
    fa.setStyle("-fx-text-fill: #969696; -fx-font-weight: bold;");
    fa.setPadding(new Insets(5, 0, 10, 10));


    //fi.setStyle("-fx-background-color: #606365; -fx-text-fill: #ffffff; -fx-font-weight: bold;");
    ImageView fiv = new ImageView(new Image("/modified/map.png"));
    fiv.setFitHeight(20);
    fiv.setPreserveRatio(true);
    Button fi = new Button("Farm", fiv); // Farm Button
    fi.setPadding(new Insets(7.5, 65, 7.5, 20)); // It would be more Efficient to use HBox inside Button
    fi.setGraphicTextGap(10);


    ImageView crv = new ImageView(new Image("/modified/crop.png"));
    crv.setFitHeight(20);
    crv.setPreserveRatio(true);
    Button cr = new Button( "Crop", crv); // Crop Button
    cr.setGraphicTextGap(10);
    cr.setPadding(new Insets(7.5, 67, 7.5, 20));

    ImageView rfv = new ImageView(new Image("/modified/rain.png"));
    rfv.setFitHeight(20);
    rfv.setPreserveRatio(true);
    Button rf = new Button("Rainfall", rfv); // Rainfall Button
    rf.setPadding(new Insets(7.5, 50, 7.5, 20));
    rf.setGraphicTextGap(10);

    ImageView mov = new ImageView(new Image("/modified/drop.png"));
    mov.setFitHeight(20);
    mov.setPreserveRatio(true);
    Button mo = new Button("Moisture", mov); // Moisture
    mo.setPadding(new Insets(7.5, 40, 7.5, 20));
    mo.setGraphicTextGap(10);

    ImageView evv = new ImageView(new Image("/modified/cal.png"));
    evv.setFitHeight(20);
    evv.setPreserveRatio(true);
    Button ev = new Button("Events", evv); //Events
    ev.setPadding(new Insets( 7.5, 55, 7.5, 20));
    ev.setGraphicTextGap(10);


    cr.setId("lp");
    rf.setId("lp");
    mo.setId("lp");
    ev.setId("lp");
    fi.setId("lp");

    changeBackgroundOnHoverUsingBinding(cr);
    changeBackgroundOnHoverUsingBinding(rf);
    changeBackgroundOnHoverUsingBinding(mo);
    changeBackgroundOnHoverUsingBinding(ev);
    changeBackgroundOnHoverUsingBinding(fi);

     /* fi.setOnMouseClicked(new EventHandler<MouseEvent>() {
        fi.setStyle("")
    }); */

    fi.setOnAction(e -> fi.setStyle("-fx-background-color: #00C275;"));

    /*
    changeBackgroundOnClickUsingBinding(fi);
    changeBackgroundOnClickUsingBinding(cr);
    changeBackgroundOnClickUsingBinding(mo);
    changeBackgroundOnClickUsingBinding(ev);
    changeBackgroundOnClickUsingBinding(rf);
     */

    VBox v2 = new VBox( fa, fi, cr, rf, mo, ev); // Lower Left VBox - inside vb
    v2.setPadding(new Insets( 0, 0, 0, 0));

    VBox vb = new VBox(v1, v2); // Left Outer Most VBox
    vb.setMaxWidth(150);
    vb.setMinWidth(150);
    vb.setStyle("-fx-background-color: #454851;");
    BorderPane bp = new BorderPane();
    bp.setLeft(vb);
    Scene sc = new Scene(bp, 800, 500);
    sc.getStylesheets().add(Medow.class.getResource("all.css").toExternalForm()); // Adding all.css
    primarystage.setScene(sc);
    primarystage.show();

}
public void setXY(double x, double y, Control o){
    o.setLayoutX(x);
    o.setLayoutY(y);
}

 /*  public void butStyle(Control... b1){
    for(Control b : b1){
    //b.setStyle("-fx-background-color: :#606365;");
    }
} */

private void changeBackgroundOnHoverUsingBinding(Node node) { // Function To Change Button Color on Hover
    node.styleProperty().bind(
            Bindings
                    .when(node.hoverProperty())
                    .then(
                            new SimpleStringProperty("-fx-background-color: #373940;")
                    )
                    .otherwise(
                            new SimpleStringProperty("-fx-background-color: #454851;")
                    )
    );
}



}

以下是投掷的例外情况:

Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: Button.style : A bound value cannot be set.
        at javafx.beans.property.StringPropertyBase.set(StringPropertyBase.java:140)
        at javafx.scene.Node$4.set(Node.java:1101)
        at javafx.scene.Node$4.set(Node.java:1095)
        at javafx.scene.Node.setStyle(Node.java:1073)
        at Medow$1.handle(Medow.java:121)
        at Medow$1.handle(Medow.java:116)
        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.EventUtil.fireEventImpl(EventUtil.java:74)
        at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
        at javafx.event.Event.fireEvent(Event.java:198)
        at javafx.scene.Node.fireEvent(Node.java:8411)
        at javafx.scene.control.Button.fire(Button.java:185)
        at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
        at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
        at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
        at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
        at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
        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.EventUtil.fireEventImpl(EventUtil.java:74)
        at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
        at javafx.event.Event.fireEvent(Event.java:198)
        at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
        at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
        at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
        at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
        at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:352)
        at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:275)
        at java.security.AccessController.doPrivileged(Native Method)
        at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$355(GlassViewEventHandler.java:388)
        at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
        at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:387)
        at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
        at com.sun.glass.ui.View.notifyMouse(View.java:937)
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: Button.style : A bound value cannot be set.
        at javafx.beans.property.StringPropertyBase.set(StringPropertyBase.java:140)
        at javafx.scene.Node$4.set(Node.java:1101)
        at javafx.scene.Node$4.set(Node.java:1095)
        at javafx.scene.Node.setStyle(Node.java:1073)
        at Medow$1.handle(Medow.java:121)
        at Medow$1.handle(Medow.java:116)
        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.EventUtil.fireEventImpl(EventUtil.java:74)
        at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
        at javafx.event.Event.fireEvent(Event.java:198)
        at javafx.scene.Node.fireEvent(Node.java:8411)
        at javafx.scene.control.Button.fire(Button.java:185)
        at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
        at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
        at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
        at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
        at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
        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.EventUtil.fireEventImpl(EventUtil.java:74)
        at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
        at javafx.event.Event.fireEvent(Event.java:198)
        at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
        at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
        at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
        at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
        at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:352)
        at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:275)
        at java.security.AccessController.doPrivileged(Native Method)
        at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$355(GlassViewEventHandler.java:388)
        at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
        at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:387)
        at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
        at com.sun.glass.ui.View.notifyMouse(View.java:937)

Process finished with exit code 0

1 个答案:

答案 0 :(得分:0)

堆栈跟踪告诉您问题:

  

Button.style:无法设置绑定值。

即。你已经使用

绑定了style属性
PdfWriter

然后你试着打电话

node.styleProperty().bind(...

要解决此问题,您需要在处理程序中设置样式属性之前取消绑定(在这种情况下,当鼠标悬停/取消悬停时,它将不再更改颜色),或者您的绑定需要适应逻辑被压了。您使用哪种方法取决于您想要的功能。