我创建了这个程序,当你点击"创建"按钮另外两个ButtonBar(称为newNode)被创建..在这个按钮栏内有一个按钮和一个圆圈。当全部创建时,我想在我点击另一个按钮时更改这两个圆圈的颜色。我编写了一些东西,但我可以改变最后一圈的颜色。
public class Controller implements Initializable{
@FXML
private Button btnHaveFun;
@FXML
private ButtonBar ReadSMSBar;
@FXML
private ButtonBar GalleryBar;
@FXML
private ButtonBar FileExplorerBar;
@FXML
private ButtonBar SpyCamBar;
@FXML
private ButtonBar TakeAPictureBar;
@FXML
private ButtonBar TakeAScreenshotBar;
@FXML
private ButtonBar RecordAVideoBar;
@FXML
private ButtonBar SpyWhatsappBar;
@FXML
private ButtonBar KeyLoggerBar;
@FXML
private ButtonBar ScreenStreamBar;
@FXML
private ButtonBar SpyMicrophoneBar;
@FXML
private ButtonBar slaveBar;
@FXML
private VBox slaveVbox;
@FXML
private Circle statusSlave;
private ButtonBar newNode = new ButtonBar();
private Circle c= new Circle();
@Override
public void initialize(URL location, ResourceBundle resources) {
// TODO Auto-generated method stub
}
public void creat(String s){
newNode = new ButtonBar();
c= new Circle();
c.setRadius(11);
c.setStrokeWidth(1);
c.setStroke(Paint.valueOf("#ffffff"));
c.setFill(Paint.valueOf("#15ff00"));
c.setId(s);
newNode.getButtons().addAll(new Button("Mavero"),c);
slaveVbox.getChildren().addAll(newNode) ;
}
@FXML
public void newComp(){
creat("id1");
creat("id2");
}
@FXML
public void change(){
c.setFill(Color.BLACK);
}
}
答案 0 :(得分:3)
您可以使用Node.lookup()
使用css选择器获取Circle
(或Node.lookupAll
获取多个节点):
void change(String oldId, String newId) {
change(oldId, Color.RED);
change(newId, Color.GREEN);
}
void change(String id, Color color){
Circle circle = (Circle) slaveVbox.lookup('#'+id);
circle.setFill(color);
}
答案 1 :(得分:0)
简单:
只需在列表中添加所有圈子 - >
List<Circle> circles = new ArrayList<>();
public void creat(String s) {
newNode = new ButtonBar();
Circle c = new Circle();
c.setRadius(11);
c.setStrokeWidth(1);
c.setStroke(Paint.valueOf("#ffffff"));
c.setFill(Paint.valueOf("#15ff00"));
c.setId(s);
circles.add(c);
newNode.getButtons().addAll(new Button("Mavero"), c);
slaveVbox.getChildren().addAll(newNode);
}
@FXML
public void newComp() {
creat("id1");
creat("id2");
}
@FXML
public void change() {
for (Circle c : circles) {
c.setFill(Color.BLACK);
}
}