我希望在组合框中添加两个或多个圆圈作为选项。
Circle c1 = new Circle(10, 8, 5);
Circle c2 = new Circle(24, 8, 5);圆圈应使用CSS样式单独着色。
c1.setStyle("-fx-fill: red;");
c2.setStyle("-fx-fill: green;");或者直接使用Java语法
c1.setFill(Color.RED);
c2.setFill(Color.GREEN);我的问题是,setGraphic()方法将单个Node对象作为参数,要形成这个Node对象,需要一个布尔操作(如Shape.union(c1, c2) )。但这消除了以前应用的造型,所以我把红色和绿色的Circle改为黑色。

下面是最小的例子。所有代码都可以放在一个.java文件中。
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Shape;
import javafx.stage.Stage;
import javafx.util.Callback;
public class Test extends Application  {
   public static ObservableList<String> colours = FXCollections.observableArrayList("1", "2");
   @Override
   public void start(Stage primaryStage) throws Exception {
       ComboBox comboBox = new ComboBox();
       comboBox.setItems(colours);
       comboBox.setCellFactory(new gui.combos.ColourCellFactory());
       comboBox.setButtonCell(new gui.combos.ColourCell());
       HBox hbox = new HBox(comboBox);
       Scene scene = new Scene(hbox, 200, 120);
       primaryStage.setScene(scene);
       primaryStage.show();
   }
   public static void main(String[] args) {
       Application.launch(args);
   }
}class ColourCell extends ListCell<String> {
   @Override
   public void updateItem(String item, boolean empty) {
       super.updateItem(item, empty);
       if (empty) {
           setText(null);
           setGraphic(null);
       }
       else {
           setText(item);
           Shape shapes = this.getShapes(item);
           setGraphic(shapes);
       }
   }
    public Shape getShapes(String string)  {
        Shape shapes = null;
        switch (string) {
            case "1":
                Circle c = new Circle(10, 8,5); c.setStyle("-fx-fill: red;");
                shapes = c;
                break;
            case "2":
                Circle c1 = new Circle(10, 8,5); c1.setFill(Color.RED); 
                Circle c2 = new Circle(24, 8,5); c2.setFill(Color.GREEN);
                shapes = Shape.union(c1, c2);
                break;
            default:
                shapes = null;
        }
        return shapes;
    }
}class ColourCellFactory implements Callback<ListView<String>, ListCell<String>> {
   @Override
   public ListCell<String> call(ListView<String> listview) {
       return new gui.combos.ColourCell();
   }
}任何关于如何克服这种不便的想法都将不胜感激。谢谢。
发布于 2022-01-05 22:22:52
一个形状的结合将创建一个单一的形状。组成联合形状的元素不能独立地进行样式设计。
正如James_D在注释中所建议的,不要合并形状,而是使用父容器(如Group或HBox )。
下面的示例演示如何对图形使用HBox,该图形允许由布局窗格管理图形内容,而不需要手动布局。

import javafx.application.Application;
import javafx.collections.*;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.*;
import javafx.stage.Stage;
import java.util.List;
public class ShapeCombo extends Application {
    public static ObservableList<String> colours = FXCollections.observableArrayList(
            "1", "2"
    );
    @Override
    public void start(Stage stage) throws Exception {
        ComboBox<String> comboBox = new ComboBox<>(colours);
        comboBox.setCellFactory(listView -> new ColourCell());
        comboBox.setButtonCell(new ColourCell());
        Scene scene = new Scene(new HBox(comboBox), 200, 120);
        stage.setScene(scene);
        stage.show();
    }
    public static void main(String[] args) {
        launch(args);
    }
}
class ColourCell extends ListCell<String> {
    private HBox graphicContent = new HBox(6);
    public ColourCell() {
        graphicContent.setPadding(new Insets(2, 3, 2, 3));
    }
    @Override
    public void updateItem(String item, boolean empty) {
        super.updateItem(item, empty);
        if (item == null || empty) {
            setText(null);
            setGraphic(null);
        } else {
            setText(item);
            graphicContent.getChildren().setAll(
                    getShapes(item)
            );
            setGraphic(graphicContent);
        }
    }
    public List<Shape> getShapes(String string) {
        return switch (string) {
            case "1" -> List.of(
                    new Circle(5, Color.RED)
            );
            case "2" -> List.of(
                    new Circle(5, Color.RED),
                    new Circle(5, Color.GREEN)
            );
            default -> List.of();
        };
    }
}https://stackoverflow.com/questions/70599534
复制相似问题