【问题标题】:Compound component in JavaJava中的复合组件
【发布时间】:2016-08-10 12:44:16
【问题描述】:
我正在为一个 uni 项目(炉石)制作战斗卡牌游戏
船上的“小兵”牌有生命值和攻击力,而且这些都在不断变化,我正在尝试制作一个复合组件,它是一个中心ImageIcon,左下角和右下角有两个“正方形”,代表卡片当前的生命值和攻击力,左上角的一个代表它的消耗,所有这些都是StringProperty
我真的不知道如何解决这个问题,也许甚至不需要复合组件
这是炉石卡的外观示例:
【问题讨论】:
标签:
java
user-interface
javafx-8
【解决方案1】:
使用 StackPane。将图像放在底部,然后将 AnchorPane 放在顶部。在三个角中的每一个中放置一个文本,并将每个角的 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);
}
}
}