2017-05-17 11 views
0

状況:私は、JavaとJavaFXを使用して学校プロジェクトのための簡単なメモ作成プログラムを最初から作成しています。私はグループのノートにタグを使用しています。ノートにタグを追加することができます。作成された新しいタグであれば、タグクラウドにも追加されます。ボタンはプレースメントによって若干異なる操作を実行する必要があります

各タグにはXが付いたボタンがあります。ボタンを機能させる必要がありますが、タグの配置に応じて2つのうちのいずれかを実行する必要があります。 1)ユーザーが特定のノートのタグが表示されているタグバー(これはTilePane)からタグを削除し、それをノートから削除する必要があります。 2)ユーザがタグを完全に削除したい場合、ユーザはタグクラウド(FlowPane)内のタグのXをクリックし、タグクラウドおよびすべてのノートからタグを除去する。

問題:私が理解する限り、同じボタンに対して2つの異なるアクションを行う必要があり、どのようにその作業を行うかわかりません。

アイデア:私は独自のFXMLファイルで2種類のタグを作成することを考えましたが、わかりません。

質問:同じボタンに対して2つの異なるアクションを作成するにはどうしたらよいのですか?

は、ここでプログラムは、これまでのように見えるものへのリンクです:ここで enter image description here

+1

私は2つの異なる 'X'ボタンを参照してください。 1つは上部(「雲」)に、もう1つは左側にあります。独自のアクションをそれぞれ割り当てます。 – user1803551

+0

ボタンがどこにあるか知るには、ボタンを保持する親にfx:idがあることを確認するだけです。次にボタンをクリックすると、親が誰であるかがわかります。親にアクションを切り替えます。 – Sedrick

+1

これらは同じボタンではありません。例えば。タグ・バーの「プライベート」タグ・ボタンは、タグ・クラウドの「プライベート」タグ・ボタンとは(必然的に)異なるボタンである。したがって、それぞれには異なるアクションを実行するイベントハンドラがあります。問題はどこだ? –

答えて

0

はあなたと遊ぶことができるアプリです。私は最初にhereに行きました。このアプリは2つのタグセットを作成し、同じ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> 

以下の写真では、3つのタグをランダムに追加して、チキンタグを削除しました。

enter image description hereenter image description here

関連する問題