我正在编写一个图形编辑器,它由JLabel
网格构建,如此:
+-------+-------+-------+-------+-------+-------+-------
| Label | Label | Label | Label | Label | Label | Label ...
+-------+-------+-------+-------+-------+-------+-------
| Label | Label | Label | Label | Label | Label | Label ...
+-------+-------+-------+-------+-------+-------+-------
| Label | Label | Label | Label | Label | Label | Label ...
+-------+-------+-------+-------+-------+-------+-------
and so on...
所有标签都有共同的属性(例如字体大小,首选大小等)。
我想知道的是,是否有办法一次性更改所有标签的公共属性而不迭代所有标签?
我想要实现的功能之一是网格,通过在右边和底边的每个JLabel
上放置一个遮罩边框来生成。例如,如果要关闭网格,我会删除每个标签的边框。
这是我目前生成它们的代码......
public class StationPanel extends JPanel {
private final InfraGridModel model;
public StationPanel(InfraGridModel model) {
super(new GridLayout(40,0));
this.model = model;
this.initComponents();
}
public void initComponents() {
for(int i = 0; i < 800; i++) {
JLabel label = new JLabel("" + i);
label.setOpaque(true);
label.setBackground(model.getTileColour());
label.setBorder(model.getTileBorder());
this.add(label);
}
}
}
因此模型是:
public class InfraGridModel {
private Integer tileSize;
private Border tileBorder;
private Boolean showGrid;
private Color tileHighlight;
private Color tileColour;
private ArrayList<StationInfrastructure> infrastructure;
private Integer scale;
private Integer orientation;
private JLabel[][] grid;
// List of constants for determining which end of the panel is the up direction
public static final Integer RENDER_TOPUP = 0;
public static final Integer RENDER_BOTTOMUP = 1;
public static final Integer RENDER_LEFTUP = 2;
public static final Integer RENDER_RIGHTUP = 3;
public InfraGridModel() {
this.tileSize = 16;
this.showGrid = true;
this.tileBorder = BorderFactory.createMatteBorder(0, 0, 1, 1, Color.BLACK);
this.tileHighlight = Color.YELLOW;
this.scale = 2;
this.orientation = RENDER_TOPUP;
}
//A long list of getters and setters...
}
我一直在搜索,找不到答案,或弄清楚如何去做。