有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Java中的用户界面复合组件

我正在为一个uni项目(炉石)制作一个战斗卡游戏

船上的“仆从”牌有生命值和攻击值,它们在不断变化,我正在尝试制作一个复合组件,它将是一个中心ImageIcon,在左下角和右下角有两个“正方形”,表示牌的当前生命值和攻击值,在左上角有一个,表示其成本,所有这些都是StringProperty

我真的不知道如何处理这个问题,也许一个复合组件甚至不是必需的

这是炉石卡外观的一个示例:

enter image description here


共 (1) 个答案

  1. # 1 楼答案

    使用堆叠窗格。把图片放在底部,然后在顶部放一个锚烷。在三个角中的每个角中放置一个文本,并将每个角的textProperty()绑定到其中一个StringProperties。大概是这样的:

    public class BattleCard extends Application {
    
    private static Label label;
    
    public static void main(String[] args) {
        launch(args);
    }
    
    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("Hello World!");
        label = new Label();
        label.setText("Waiting...");
        StackPane root = new StackPane();
        root.getChildren().add(new CardPane());
        primaryStage.setScene(new Scene(root, 350, 420));
        primaryStage.show();
    }
    
    public class CardPane extends StackPane {
        public CardPane() {
            ImageView cardImage = new ImageView(new Image("http://i.stack.imgur.com/1ljQH.png"));
            AnchorPane anchorPane = new AnchorPane();
            getChildren().addAll(cardImage, anchorPane);
            Text costText = new Text("Cost");
            Text healthText = new Text("Health");
            Text attackText = new Text("Attack");
            AnchorPane.setTopAnchor(costText, 0d);
            AnchorPane.setLeftAnchor(costText, 0d);
            AnchorPane.setBottomAnchor(healthText, 0d);
            AnchorPane.setLeftAnchor(healthText, 0d);
            AnchorPane.setBottomAnchor(attackText, 0d);
            AnchorPane.setRightAnchor(attackText, 0d);
            anchorPane.getChildren().addAll(costText, healthText, attackText);
        }
    }
    

    }