我希望添加兩個或更多圓圈作為組合框的選項。
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();
}
}
任何想法如何克服這種不便將不勝感激。謝謝你。
uj5u.com熱心網友回復:
形狀的聯合將創建一個形狀。組成聯合形狀的元素不能獨立設定樣式。
正如 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://www.uj5u.com/net/406824.html
標籤:
