JavaFX - 鼠标事件未触发

时间:2013-11-29 13:57:26

标签: java events button user-interface javafx

这是我的代码:

Button start = new Button("Start");
    start.setPrefHeight(100d);
    start.setMaxWidth(Double.MAX_VALUE);        

    start.setOnMouseClicked(new EventHandler<MouseEvent>(){

        @Override
        public void handle(MouseEvent arg0) {
            //ss.startServer();
            System.out.println("adfasddasfrwgafsdasdf");
        }

    });

eclipse上的控制台上没有打印消息!我希望在释放鼠标时触发事件。我做错了什么?

在furthet投资后,我意识到如果我在另一个节点上有另一个鼠标释放事件,它将发射更多事件!...为什么会这样? 例如,这是我正在使用的代码,当我按下开始按钮时,它会打印开始和重新加载!

 start.setOnMouseReleased(new EventHandler<MouseEvent>(){

        @Override
        public void handle(MouseEvent arg0) {
            ss.startServer();
            System.out.println("Start");
            arg0.consume();
        }


    });
 Button stop = new Button("Stop");
    stop.setPrefHeight(100d);
    stop.setMaxWidth(Double.MAX_VALUE);
    stop.setOnMouseReleased(new EventHandler<MouseEvent>(){

        @Override
        public void handle(MouseEvent event) {
            sc.stopServer();
            System.out.println("Stop");
            event.consume();
        }

    });
    Button reload = new Button("Reload");
    reload.setPrefHeight(100d);
    reload.setMaxWidth(Double.MAX_VALUE);
    start.setOnMouseReleased(new EventHandler<MouseEvent>(){

        @Override
        public void handle(MouseEvent some) {
            sc.reloadServer();
            System.out.println("Reload");
            some.consume();
        }

    });

停止按钮正常工作! 无论如何,如果另一段代码有效,那么它是(http://pastebin.com/4f55R2gN

2 个答案:

答案 0 :(得分:1)

试试这个

button.setOnMouseReleased(new EventHandler<MouseEvent>() {
   @Override
   public void handle(MouseEvent arg0) {
      System.out.println("button click and mouse released");
   }
});

以下对我来说很好。

public class FxmlSample extends Application{

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

    @Override
    public void start(Stage stage) throws IOException{
        JFrame frame = new JFrame();

        JFXPanel jfxPanel = new JFXPanel();

        Pane pane = new Pane();
        VBox vBox = new VBox();
        Button button = new Button("Click me");
        button.setMinSize(50.0, 50.0);
        button.setOnMouseReleased(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent arg0) {
                System.out.println("11111111111111");

            }
        });

        Button button_2 = new Button("Click me 2");
        button_2.setMinSize(50.0, 50.0);
        button_2.setOnMouseReleased(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent arg0) {
                System.out.println("2222222222222");

            }
        });
        vBox.setPadding(new Insets(20));
        vBox.getChildren().addAll(button, button_2);
        pane.getChildren().addAll(vBox);
        Scene scene = new Scene(pane);      
        jfxPanel.setScene(scene);
        frame.add(jfxPanel);
        frame.setVisible(true);
        frame.setMinimumSize(new Dimension(700, 700));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

答案 1 :(得分:1)

你错了你的代码。您可以在开始对象上设置MouseReleased处理程序,而不是重新加载

    Button reload = new Button("Reload");
    reload.setPrefHeight(100d);
    reload.setMaxWidth(Double.MAX_VALUE);
    start.setOnMouseReleased(new EventHandler<MouseEvent>(){