按钮必须根据位置执行稍微不同的操作

时间:2017-05-17 11:58:16

标签: java javafx

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

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

问题:据我所知,我需要为同一个按钮做两个不同的操作,我不知道如何做到这一点。

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

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

这是指向目前该计划的内容的链接: enter image description here

1 个答案:

答案 0 :(得分:0)

这是一款可以玩的应用。我开始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>

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

enter image description here enter image description here