Java 按钮必须根据位置执行稍微不同的操作
情况:我正在用java和JavaFX从头开始为一个学校项目编写一个简单的笔记程序。我正在使用标签对笔记进行分组。我可以将标签添加到便笺中,如果创建了新标签,它也会添加到我的标签云中 每个标签都有一个标有X的按钮。我现在需要使按钮工作,但根据标签的位置,我需要它执行以下两项操作之一: 1) 如果用户希望从便笺中删除标记,我需要从显示特定便笺标记的标记栏(这是一个TilePane)中删除标记,并将其从便笺中删除。 2) 如果用户希望完全删除标记,则用户单击标记云中标记的X(这是一个流程窗格),然后从标记云和所有注释中删除标记 问题:据我所知,我需要对同一个按钮执行两种不同的操作,但我不知道如何操作 想法:我曾想过制作两种不同类型的标签,每种标签都有自己的FXML文件,但我不确定 问题:如何为同一按钮执行两种不同的操作,以及如何执行以调用正确的操作 这里有一个到目前为止该计划的链接:Java 按钮必须根据位置执行稍微不同的操作,java,javafx,Java,Javafx,情况:我正在用java和JavaFX从头开始为一个学校项目编写一个简单的笔记程序。我正在使用标签对笔记进行分组。我可以将标签添加到便笺中,如果创建了新标签,它也会添加到我的标签云中 每个标签都有一个标有X的按钮。我现在需要使按钮工作,但根据标签的位置,我需要它执行以下两项操作之一: 1) 如果用户希望从便笺中删除标记,我需要从显示特定便笺标记的标记栏(这是一个TilePane)中删除标记,并将其从便笺中删除。 2) 如果用户希望完全删除标记,则用户单击标记云中标记的X(这是一个流程窗格),然后从
这是一个你可以玩的应用程序。我一开始就去了。此应用程序创建两组标签,如果删除一组标签,则删除具有相同id的所有标签 主要内容: 控制器: FXML:
我看到两个不同的x
按钮。一个在顶部(“云”),一个在左侧。为每个按钮分配自己的操作。要知道按钮的位置,您所要做的就是确保持有按钮的父项具有fx: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)
{
}
}
<?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>