【问题标题】:Putting an object on a stage when mouse clicked单击鼠标时将对象放在舞台上
【发布时间】:2014-03-27 02:24:38
【问题描述】:

包javafxapplication36;

/**
 * Copyright (c) 2008, 2012 Oracle and/or its affiliates.
 * All rights reserved. Use is subject to license terms.
 */
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.beans.InvalidationListener;
import javafx.beans.Observable;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.VPos;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.control.Button;
import javafx.scene.effect.DropShadow;
import javafx.scene.effect.Lighting;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Text;
import javafx.scene.text.TextAlignment;
import javafx.util.Duration;

/**
 * A sample that demonstrates how to add or remove a change listener on a node
 * (for example, a Rectangle node) for some property (for example,
 * Rectangle.hover). Once you add a listener, the text field  shows the hover
 * property change.
 *
 * @see javafx.beans.value.ChangeListener
 * @see javafx.beans.InvalidationListener
 * @see javafx.beans.value.ObservableValue
 */
public class ChangeListenerSample extends Application {

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

    private void init(Stage primaryStage) {
        final Group root = new Group();
        primaryStage.setResizable(false);
        Scene scene = new Scene(root, 400,80);
        primaryStage.setScene(scene);

        //rect.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>()
        scene.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
        @Override public void handle(MouseEvent event) {
              Circle circle = new Circle(event.getSceneX(), event.getSceneY(),30);
              circle.setFill(Color.YELLOW);
              root.getChildren().add(circle);
        }
     });
    //root.getChildren().add(circle);
    primaryStage.setScene(new Scene(root, 300, 250));
    primaryStage.show();
}
}

我试图在按下鼠标时在舞台上放一个圆圈,但我无法做到。我尝试在开始时设置root,但它没有发生。 感谢您的帮助!

【问题讨论】:

    标签: javafx stage scene


    【解决方案1】:

    我只对您的代码进行了一些编辑,它似乎运行良好!

    注意:您不需要在场景中再次设置根,因为您已经在开始时这样做了!

    将场景设置为舞台也是如此!

    import javafx.application.Application;
    import javafx.event.EventHandler;
    import javafx.scene.Group;
    import javafx.scene.Scene;
    import javafx.scene.input.MouseEvent;
    import javafx.scene.paint.Color;
    import javafx.scene.shape.Circle;
    import javafx.stage.Stage;
    
    /**
     * A sample that demonstrates how to add or remove a change listener on a node
     * (for example, a Rectangle node) for some property (for example,
     * Rectangle.hover). Once you add a listener, the text field  shows the hover
     * property change.
     *
     * @see javafx.beans.value.ChangeListener
     * @see javafx.beans.InvalidationListener
     * @see javafx.beans.value.ObservableValue
     */
    public class ChangeListenerSample extends Application {
    
        public static void main(String[] args) {
        launch(args);
    }
    
        @Override
        public void start(Stage primaryStage) throws Exception {
            final Group root = new Group();
            primaryStage.setResizable(false);
            Scene scene = new Scene(root, 400,80);
            primaryStage.setScene(scene);
    
            //rect.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>()
            scene.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
            @Override public void handle(MouseEvent event) {
                  Circle circle = new Circle(event.getSceneX(), event.getSceneY(),30);
                  circle.setFill(Color.YELLOW);
                  root.getChildren().add(circle);
            }
            });
            //root.getChildren().add(circle);
            primaryStage.show();
    
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-06-01
      • 1970-01-01
      • 2014-07-01
      • 2014-06-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多