创建自己的 JLabel
Creation own JLabel
我将 JPanel 与 GridLayout 一起使用,并将 JLabel
放入其中。一切正常。但是我想用自己的class(extends JLabel
)有问题
当我使用 JLabel
时,我有这样的渲染:
当我使用自己的 JLabel
时,我有:
这是我的 JLabel
自定义代码:
public class LabelCustom extends JLabel{
int x;
int y;
public LabelCustom(int x, int y) {
super();
this.x = x;
this.y = y;
this.setBackground(Color.white);
this.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
}
以及我如何使用它:
JPanel j = new JPanel();
j.setLayout(new GridLayout(nbCaseY, nbCaseX));
for(int i=0; i<nbCaseY; i++) {
HashMap<Integer, JLabel> ligne = new HashMap();
for(int y=0; y<nbCaseX; y++) {
LabelCustom p = new LabelCustom(i, y);
p.addMouseListener(ml);
//p.setBounds(100+ y*(hauteur), 100 + i*( hauteur), hauteur, hauteur);
p.setPreferredSize(new Dimension(hauteur, hauteur));
//p.setBounds(100+ y*( (width-200-2*hauteur)/nbCaseX), 100 + i*( (height-200)/nbCaseY), ((width-200-2*hauteur)/nbCaseX), ((height-200)/nbCaseY));
p.setTransferHandler(new TransferHandler("icon"));
p.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));
p.setOpaque(true);
p.setVisible(true);
j.add(p);
ligne.put(y, p);
}
Frame.p.getListeNiveau().get(0).ajouterLigne(ligne);
}
JLabel
已经有一个 getX
和 getY
方法来定位屏幕上的标签,你(无意中)覆盖了这个功能,现在返回不相关的信息.
It's not for x and y position. It is for an id of hashmap, i think it is useful ;)
因此,我建议不要使用 getX/Y
,而是制作一个 ID
class,它包含您需要的信息(可能会覆盖 equals
和 hashcode
方法使比较更容易)并使用它(提供 getID
和 setID
方法)
我将 JPanel 与 GridLayout 一起使用,并将 JLabel
放入其中。一切正常。但是我想用自己的class(extends JLabel
)有问题
当我使用 JLabel
时,我有这样的渲染:
当我使用自己的 JLabel
时,我有:
这是我的 JLabel
自定义代码:
public class LabelCustom extends JLabel{
int x;
int y;
public LabelCustom(int x, int y) {
super();
this.x = x;
this.y = y;
this.setBackground(Color.white);
this.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
}
以及我如何使用它:
JPanel j = new JPanel();
j.setLayout(new GridLayout(nbCaseY, nbCaseX));
for(int i=0; i<nbCaseY; i++) {
HashMap<Integer, JLabel> ligne = new HashMap();
for(int y=0; y<nbCaseX; y++) {
LabelCustom p = new LabelCustom(i, y);
p.addMouseListener(ml);
//p.setBounds(100+ y*(hauteur), 100 + i*( hauteur), hauteur, hauteur);
p.setPreferredSize(new Dimension(hauteur, hauteur));
//p.setBounds(100+ y*( (width-200-2*hauteur)/nbCaseX), 100 + i*( (height-200)/nbCaseY), ((width-200-2*hauteur)/nbCaseX), ((height-200)/nbCaseY));
p.setTransferHandler(new TransferHandler("icon"));
p.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));
p.setOpaque(true);
p.setVisible(true);
j.add(p);
ligne.put(y, p);
}
Frame.p.getListeNiveau().get(0).ajouterLigne(ligne);
}
JLabel
已经有一个 getX
和 getY
方法来定位屏幕上的标签,你(无意中)覆盖了这个功能,现在返回不相关的信息.
It's not for x and y position. It is for an id of hashmap, i think it is useful ;)
因此,我建议不要使用 getX/Y
,而是制作一个 ID
class,它包含您需要的信息(可能会覆盖 equals
和 hashcode
方法使比较更容易)并使用它(提供 getID
和 setID
方法)