【问题标题】:button has to perform slightly different action depending on placement按钮必须根据位置执行略有不同的操作
【发布时间】:2017-05-17 11:58:15
【问题描述】:

情况:我正在为一个使用 java 和 JavaFX 的学校项目从头开始编写一个简单的笔记程序。我正在使用标签对笔记进行分组。我可以将标签添加到笔记中,如果它是创建的新标签,它也会添加到我的标签云中。

每个标签都有一个标有 X 的按钮。我现在需要使按钮工作,但根据标签的位置,我需要它来做以下两件事之一: 1)如果用户希望从笔记中删除标签,我需要从显示该特定笔记的标签的标签栏(这是一个 TilePane)中删除该标签并将其从笔记中删除。 2) 如果用户希望完全删除标签,则用户单击标签云(即 FlowPane)中标签的 X,然后从标签云和所有注释中删除该标签。

问题:据我了解,我需要为同一个按钮执行两个不同的操作,但我不知道如何实现。

想法:我曾想过制作两种不同类型的标签,每种标签都有自己的 FXML 文件,但我不确定。

问题:如何为同一个按钮执行两个不同的操作,以及如何执行以调用正确的操作?

这是该程序目前的样子的链接:

【问题讨论】:

  • 我看到 2 个不同的 x 按钮。一个在顶部(“云”),一个在左侧。为每个人分配自己的行动。
  • 要知道按钮的位置,您所要做的就是确保持有按钮的父级具有 fx:id。然后,当您单击按钮时,请查看父母是谁。切换父级的操作。
  • 这些按钮并不相同。例如。标签栏中的“私人”标签按钮(必然)是与标签云中的“私人”标签按钮不同的按钮。所以每个都有一个事件处理程序来执行不同的操作。问题出在哪里?
  • 一篇关于标签的好帖子here

标签: java javafx


【解决方案1】:

这是一个您可以玩的应用程序。我一开始就去了here。这个应用程序创建了两组标签,如果一个被删除,则删除所有具有相同 id 的标签。

主要:

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

/**
 *
 * @author blj0011
 */
public class JavaFXApplication102 extends Application
{

    @Override
    public void start(Stage stage) throws Exception
    {
        Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));

        Scene scene = new Scene(root);

        stage.setScene(scene);
        stage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {
        launch(args);
    }        
}

控制器:

import java.io.File;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.ResourceBundle;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.event.EventType;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;

/**
 *
 * @author blj0011
 */
public class FXMLDocumentController implements Initializable
{

    @FXML
    private VBox vbMain;

    @FXML 
    private FlowPane fpMain;

    String[] tagType = {"Chicken", "Soup", "Fall", "Winter", "Happy"};
    Random random = new Random();




    @FXML
    private void handleButtonAction(ActionEvent event)
    {
        int control = random.nextInt(5);

        HBox tag1 = new HBox();
        tag1.setId(tagType[control]);
        tag1.setStyle("-fx-padding:4;" +
            "        -fx-border-width: 2;" +
            "        -fx-border-color: black;" +
            "        -fx-border-radius: 4;" +
            "        -fx-background-color: f1f1f1;" +
            "        -fx-border-insets: 5;");
        tag1.setSpacing(5);
        tag1.setPrefHeight(20);
        tag1.setMaxWidth(75);

        tag1.getChildren().add(new Label(tagType[control]));

        Label closingX1 = new Label("x");
        tag1.getChildren().add(closingX1);         
        closingX1.setOnMouseClicked((MouseEvent event1) -> {
            System.out.println("Parent: " + ((Label) event1.getSource()).getParent().getParent());      
            for(Node child : fpMain.getChildren())
            {
                if(child.getId().equals(tag1.getId()))
                {
                    Platform.runLater(()->fpMain.getChildren().remove(child));
                }
            }

            for(Node child : vbMain.getChildren())
            {
                if(child.getId().equals(tag1.getId()))
                {
                    Platform.runLater(()->vbMain.getChildren().remove(child));
                }
            }            
        });


        vbMain.getChildren().add(tag1);



        HBox tag2 = new HBox(); 
        tag2.setId(tagType[control]);
        tag2.setStyle("-fx-padding:4;" +
            "        -fx-border-width: 2;" +
            "        -fx-border-color: black;" +
            "        -fx-border-radius: 4;" +
            "        -fx-background-color: f1f1f1;" +
            "        -fx-border-insets: 5;");
        tag2.setSpacing(5);
        tag2.setPrefHeight(20);
        tag2.setMaxWidth(75);        
        tag2.getChildren().add(new Label(tagType[control]));

        Label closingX2 = new Label("x");
        tag2.getChildren().add(closingX2);          
        closingX2.setOnMouseClicked((MouseEvent event1) -> {
            System.out.println("Parent: " + ((Label) event1.getSource()).getParent().getParent());   
            for(Node child : vbMain.getChildren())
            {
                if(child.getId().equals(tag2.getId()))
                {
                    Platform.runLater(()->vbMain.getChildren().remove(child));
                }
            }

            for(Node child : fpMain.getChildren())
            {
                if(child.getId().equals(tag2.getId()))
                {
                    Platform.runLater(()->fpMain.getChildren().remove(child));
                }
            }  
        });

        fpMain.getChildren().add(tag2);        
    }



    @Override
    public void initialize(URL url, ResourceBundle rb)
    {    

    }    

}

FXML:

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.FlowPane?>
<?import javafx.scene.layout.VBox?>

<AnchorPane id="AnchorPane" prefHeight="623.0" prefWidth="820.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="javafxapplication102.FXMLDocumentController">
    <children>
        <Button fx:id="button" layoutX="372.0" layoutY="569.0" onAction="#handleButtonAction" text="add button" />
        <Label fx:id="label" layoutX="126" layoutY="120" minHeight="16" minWidth="69" />
      <VBox fx:id="vbMain" prefHeight="549.0" prefWidth="387.0" />
      <FlowPane fx:id="fpMain" layoutX="371.0" layoutY="18.0" prefHeight="505.0" prefWidth="434.0" />
    </children>
</AnchorPane>

在下面的图片中,我随机添加了三个标签,然后删除了鸡标签。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-10-07
    • 2017-06-11
    • 2014-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-11
    • 2015-08-04
    相关资源
    最近更新 更多