我试图在按下按钮后从VBox中删除其ID已知的特定标签。
我将使用鼠标选择标签,并将其ID存储在称为markedNumbers的ArrayList中。当我按下deleteButton时,我试图遍历此ArrayList并删除这些标签并清除数组。 我试图使用VBox.getChildren()。remowe(label的ID)来实现这一点,但是它似乎不起作用。
下面是与此问题有任何联系的代码:
控制器:
package sample;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.VBox;
import java.util.ArrayList;
public class Controller {
@FXML
private TextField numberField;
@FXML
private VBox VBoxId;
@FXML
private Label averageLabel;
private ArrayList<Double> numbersList = new ArrayList<>();
private String numberFieldContent;
numberClass numberObject = new numberClass();
private ArrayList<Integer> markedNumbers = new ArrayList<>();
@FXML
public void onNewNumberButton(){
averageLabel.setVisible(false);
numberFieldContent = numberField.getText();
numberObject.addNumber(numberFieldContent);
if(numberObject.isDataCorrect()){
numberField.clear();
Label numberLabel = new LabelClass(numberFieldContent,String.valueOf(numberObject.arraySize())).createLabel(EventHandler);
VBoxId.getChildren().add(numberLabel);
System.out.println("numberLabel id: " + numberLabel.getId());
}else {
numberField.clear();
}
numberObject.setDataCorrect(false);
}
javafx.event.EventHandler<MouseEvent> EventHandler = new javafx.event.EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
marked(event.getSource());
}
};
@FXML
public void onEnterPressed (KeyEvent key){
if(key.getCode().equals(KeyCode.ENTER)){
onNewNumberButton();
}
}
public void marked(Object object){
Label label = (Label) object;
LabelClass labelClass = new LabelClass(label.getText(),label.getId());
labelClass.markLabel(label);
markedNumbers.add(Integer.parseInt(label.getId()));
System.out.print("markedNumber list contains now: ");
for(int i =0; i<markedNumbers.size();i++){
System.out.print(markedNumbers.get(i) + ", ");
}
}
public void onCalculateButton(){
if(numberObject.arraySize()>0) {
averageLabel.setVisible(true);
averageLabel.setText("Average = " + numberObject.calculate());
}
}
public void onDeleteButton(){
for(int i =0;i<markedNumbers.size();i++){
VBoxId.getChildren().remove(markedNumbers.get(i));
System.out.println("Label with id: " + markedNumbers.get(i) + " got removed.");
}
markedNumbers.clear();
}
}
LabelClass:
package sample;
import javafx.event.EventHandler;
import javafx.scene.control.Label;
import javafx.scene.paint.Paint;
public class LabelClass {
private double content;
private String index;
public LabelClass(String content, String index) {
this.content = Double.parseDouble(content);
this.index = index;
}
public Label createLabel (EventHandler eventHandler){
Label numberLabel = new Label(String.valueOf(content));
numberLabel.setId(index);
numberLabel.addEventFilter(javafx.scene.input.MouseEvent.MOUSE_CLICKED, eventHandler);
return numberLabel;
}
public void markLabel(Label label){
label.setTextFill(Paint.valueOf("RED"));
System.out.println("Label that turned red id: " + label.getId());
}
public void delete(int index){
Label label = new Label();
label.setId(String.valueOf(index));
}
}
答案 0 :(得分:0)
似乎您正在尝试通过ID删除子项,但方法VBoxId.getChildren()。remove希望从VBoxId.getChildren函数返回的列表中获得索引。 我建议您将Marked对象保存在Set中,而不要保存其ID。
private Set<Node> markedNumbers = new HashSet<>();
...
public void marked(Object object){
Label label = (Label) object;
markedNumbers.add(label);
}
...
public void onDeleteButton(){
for(Node node :markedNumbers){
VBoxId.getChildren().remove(node);
}
markedNumbers.clear();
}