简体   繁体   English

如何知道在JavaFX中单击了哪个按钮

[英]How to know which button is bieng clicked in javafx

How to know which button has envoked the function. 如何知道哪个按钮激活了该功能。 I have read other answers on stackoverflow like this one . 我已经阅读了关于stackoverflow 这样的 其他答案 I tried creating a new button and giving it a value of event.getSource() but it is not working 我尝试创建一个新按钮并给它一个event.getSource()的值,但是它不起作用

    @FXML
    Button v1;
    @FXML
    Button v2;
    @FXML
    Button v3;
    @FXML
    Button v4;
    @FXML
    Button v5;
    @FXML
    Button v6;


public void printButton(ActionEvent event){

            Button sourceButton = (Button) event.getSource();

            if(sourceButton == v1){
                System.out.print("v1");
            }

            else if(sourceButton == v2){
                System.out.print("v2");
            }

            else if(sourceButton == v3){
                System.out.print("v3");
            }

            else if(sourceButton == v4){
                System.out.print("v4");
            }

            else if(sourceButton == v5){
                System.out.print("v5");
            }

            else if(sourceButton == v6){
                System.out.print("v6");
            }
        }

I have created the button in fxml and it calls the same function printButton(); 我已经在fxml中创建了按钮,它调用了相同的函数printButton();。

This answer is using java 8 update 211 for testing. 这个答案是使用Java 8更新211进行测试。

The comments are suggesting that changing == to .equals() was the solution to this. 评论表明将==更改为.equals()是解决此问题的方法。 However, Button does not override .equals() , so both of those ways are doing effectively the same thing. 但是,Button不会覆盖.equals() ,因此这两种方式实际上都是在做同一件事。

Running up the sample application below to test resulted in all of the 3 buttons working as expected. 运行下面的示例应用程序进行测试,结果这3个按钮均按预期工作。 Therefore, there may have been something incorrect in the FXML file with OP's code, which (as I write this) has not been shown from OP. 因此,带有OP的代码的FXML文件中可能存在一些不正确的信息(在我撰写本文时),而OP中并未显示。

In the example below, note that the fxml file: 在下面的示例中,请注意fxml文件:

  • Specifies the controller with fx:controller="sample.Controller" fx:controller="sample.Controller"指定控制器
  • Contains 3 buttons with their ids matching exactly to the ones declared in Controller 包含3个按钮,其ID与Controller中声明的按钮完全匹配
  • On each button, includes onAction="#printButton" , and the name in quotes matches the method name in Controller onAction="#printButton" . 在每个按钮上,包括onAction="#printButton" ,并且引号中的名称与Controller onAction="#printButton"中的方法名称匹配。

Please note all of these are within the same package. 请注意,所有这些都在同一包装内。

Main.java: Main.java:

package sample;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{
        Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
        primaryStage.setTitle("Hello World");
        primaryStage.setScene(new Scene(root, 300, 275));
        primaryStage.show();
    }


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

Controller.java Controller.java

package sample;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;

public class Controller {

    @FXML
    Button v1;
    @FXML
    Button v2;
    @FXML
    Button v3;

    public void printButton(ActionEvent event){

        Button sourceButton = (Button) event.getSource();

        if(sourceButton.equals(v1)){
            System.out.print("v1");
        }

        else if(sourceButton == v2){
            System.out.print("v2");
        }

        else if(sourceButton == v3){
            System.out.print("v3");
        }

    }
}

sample.fxml sample.fxml

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

<?import javafx.geometry.Insets?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<VBox alignment="CENTER" spacing="10.0" xmlns="http://javafx.com/javafx/10.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
    <padding>
        <Insets bottom="20.0" left="20.0" right="20.0" top="20.0"/>
    </padding>
    <HBox alignment="CENTER" spacing="10.0">
        <Button fx:id="v1" mnemonicParsing="false" onAction="#printButton" text="Button 1"/>
        <Button fx:id="v2" mnemonicParsing="false" onAction="#printButton" text="Button 2"/>
        <Button fx:id="v3" mnemonicParsing="false" onAction="#printButton" text="Button 3"/>
    </HBox>
    <Label text="Source:"/>
    <Label fx:id="lblSource"/>
</VBox>

make your life easy how about using isPressed() function ? 使您的生活变得轻松如何使用isPressed()函数?

                 if( v1.isPressed() )  {
                 name2 = n1.getText(); 

               System.out.println("   V1  got called ");
                 }  

v1.isPressed(); means check whether v1 has been clicked or not it return true or false 表示检查v1是否被单击,返回true或false

i'm not sure about also v1.isfire(); 我也不确定v1.isfire(); I think this one can make auto click 我认为这可以使自动点击

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM