1
我創建了一個HBoxes,Buttons和Labels數組。每一個「添加」按鈕被按下時, 我設置:如何從JavaFX的流程窗格中刪除節點?
hbox[count] = new HBox();
buttons[count] = new Button();
labels[count] = new Label();
(其中計數從0開始和結束於5) 我然後添加按鈕和標籤到HBox中(使得每個HBox中包含一個按鈕和一個標籤),最後將5個HBox添加到流程窗格中。 如何通過單擊HBox內的按鈕從流程窗格中刪除HBox? 這是Flow Panel中每個HBox的圖片。
/**
* FXML Controller class
*
* @author HeshamSaleh
*/
public class SecondaryViewController implements Initializable {
@FXML
private TextField searchBar;
@FXML
private Button addBtn;
@FXML
private FlowPane flowPane;
ArrayList<String> addedArtists = new ArrayList<String>();
String[] artists = {"Craig David", "Coldplay", "Eminem", "D12", "Shakira", "Radiohead", "Linkin Park", "Maroon 5", "Celine Dion", "50 Cent", "Tupac", "Snoop Dogg", "Metallica", "Backstreet Boys"};
List<String> artistNames = new ArrayList<String>(Arrays.asList(artists));
int count = 4;
HBox[] hboxArr = new HBox[5];
Button[] buttonArr = new Button[5];
Label[] labelArr = new Label[5];
int hboxCount = 0;
/**
* Initializes the controller class.
*/
@Override
public void initialize(URL url, ResourceBundle rb) {
searchBar.setFocusTraversable (false);
TextFields.bindAutoCompletion(searchBar, artistNames);
}
@FXML
private void addBtnPressed(MouseEvent event) {
String artistName = searchBar.getText();
searchBar.setText("");
if(artistNames.contains(artistName) && !addedArtists.contains(artistName) && count != -1) {
hboxArr[hboxCount] = new HBox();
buttonArr[hboxCount] = new Button();
labelArr[hboxCount] = new Label();
hboxArr[hboxCount].setAlignment(Pos.CENTER);
hboxArr[hboxCount].setSpacing(-1);
buttonArr[hboxCount].setText("X");
buttonArr[hboxCount].setAlignment(Pos.CENTER);
buttonArr[hboxCount].setStyle("-fx-background-color: TRANSPARENT; -fx-border-color: #000000;");
buttonArr[hboxCount].setFont(Font.font("Open Sans", FontWeight.BOLD, 12));
buttonArr[hboxCount].setMinWidth(20);
buttonArr[hboxCount].setMinHeight(20);
labelArr[hboxCount].setText(artistName.toUpperCase());
labelArr[hboxCount].setFont(Font.font("Proxima Nova Rg", 12));
labelArr[hboxCount].setAlignment(Pos.CENTER);
labelArr[hboxCount].setStyle("-fx-background-color: TRANSPARENT; -fx-border-color: #000000;");
labelArr[hboxCount].setMinWidth(90);
labelArr[hboxCount].setMinHeight(27);
hboxArr[hboxCount].getChildren().addAll(buttonArr[hboxCount], labelArr[hboxCount]);
flowPane.setAlignment(Pos.CENTER);
flowPane.getChildren().add(hboxArr[hboxCount]);
addedArtists.add(artistName);
count--;
hboxCount++;
}
}
}
你怎麼知道你要移除哪一個?當按下按鈕時,我需要刪除一個特定的hb –
@HeshamSaleh Akhi它刪除了按下按鈕的HBox(即父項)..請將代碼複製粘貼到您的IDE中並檢查:) – Yahya
好吧,您的代碼完全正確我需要的。你能幫我在上面的代碼中實現它嗎?非常感謝Yahya! –