【问题标题】:How to know which button is bieng clicked in javafx如何知道在javafx中单击了哪个按钮
【发布时间】:2019-05-24 20:21:47
【问题描述】:

如何知道哪个按钮触发了该功能。我在 stackoverflow like this one 上阅读了其他答案。我尝试创建一个新按钮并为其赋值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");
            }
        }

我在 fxml 中创建了按钮,它调用了相同的函数 printButton();

【问题讨论】:

  • Button 没有复制构造函数。但是:你怎么知道代码不起作用?您唯一要做的就是分配一些字段,没有输出到控制台,没有修改场景......如果没有更多信息,我们无法在这里提供帮助。 (投票结束)
  • 如果你的 fxml 正确,那么你的代码应该可以工作。请也发布你的 fxml。顺便说一句,我个人会将 Buttons 与 button.equals(otherButton) 进行比较
  • @micpog90 与 button.equals(otherButton) 比较有效。谢谢。

标签: java button javafx


【解决方案1】:

此答案使用 java 8 update 211 进行测试。

cmets 建议将== 更改为.equals() 是解决此问题的方法。但是,Button 不会覆盖 .equals(),因此这两种方式都在有效地做同样的事情。

运行下面的示例应用程序进行测试,结果所有 3 个按钮都按预期工作。因此,带有 OP 代码的 FXML 文件中可能存在一些不正确的东西,(在我写这篇文章时)OP 没有显示出来。

在下面的示例中,请注意 fxml 文件:

  • fx:controller="sample.Controller"指定控制器
  • 包含 3 个按钮,其 id 与 Controller 中声明的完全匹配
  • 在每个按钮上,包括 onAction="#printButton" ,引号中的名称与控制器 onAction="#printButton" 中的方法名称匹配。

请注意,所有这些都在同一个包中。

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

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

<?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>

【讨论】:

  • 是的,现在将“.equals()”更改为“==”正在工作。也许是别的什么问题。
【解决方案2】:

让您的生活更轻松,使用isPressed() 函数怎么样?

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

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

v1.isPressed(); 表示检查v1是否被点击它返回true或false

我也不确定v1.isfire();我认为这个可以自动点击

【讨论】:

  • 我应该在哪里检查它。我无法在 printButton() 函数中检查它。
猜你喜欢
  • 2017-06-04
  • 2021-05-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-25
相关资源
最近更新 更多