如何使一个类扩展jLabel,使其在我的主类中显示为JLabel?

时间:2015-06-03 01:23:44

标签: java class jlabel extends

所以,基本上我是在记忆。我有一个tile类,此时只有一个数字和一个布尔值(显示它是否被翻转)。我将所有数字的副本作为Tile对象并随机化,然后将它们添加到我的布局中。

基本上,我需要它们作为JLabel出现在我的屏幕上,我不确定如何做到这一点?我应该扩展其他内容,还是在我的Tile类中做一些特别的事情?或者在其他地方我的逻辑是一个问题?这是我的Tile课程(很少)

PS。我还需要为flippy翻牌使用计时器

number

任何帮助将不胜感激!  编辑:

这是我尝试添加瓷砖的主要类

class Tile extends JLabel implements ActionListener{

boolean flipped;
int imageNum;
ImageIcon image;
JLabel card;

Tile(int num, boolean f)
{
    imageNum=num;
    flipped=f;
    card=new JLabel(""+imageNum);
}
public void flipCard()
{

}

public void actionPerformed(ActionEvent a)
{

}

:'(

1 个答案:

答案 0 :(得分:1)

 Tile(int num, boolean f){
    imageNum=num;
    flipped=f;
    card=new JLabel(""+imageNum);//1. Remove this line and card Label
    super.setText(String.valueOf(num));//ADD This line
 }

只需创建Object

Tile即可
Tile myLabel = new Tile(10,false);//this will create lable

由于您的Tile类属于JLabel的子类,因此无需在Label类中创建Tile Tile本身就是Label 。除此之外,您还没有获得JLabel的动作事件。使用MouseListener不是可点击的元素。