用Decorator图案装饰JLabel

时间:2013-05-13 13:56:44

标签: java swing decorator jlabel

我正在尝试装饰 JLabel ,但我设定的礼节不起作用。

装饰师类:

public class Decorator extends JComponent{

    public Decorator(JComponent c){
        setLayout(new BorderLayout());
        add("Center",c);}
}

PlayLabel课程:

public class PlayLabel extends Decorator{
JComponent thisComp;
    public PlayLabel(JComponent c){
        super(c);

        thisComp=this;
        LineBorder line = new LineBorder(new Color(36,77,240), 2, true);
        thisComp.setAlignmentX(Component.CENTER_ALIGNMENT);
        thisComp.setBackground(new Color(36,77,240));
        thisComp.setMaximumSize(new Dimension(150,75));
        thisComp.setForeground(Color.WHITE);
        thisComp.setOpaque(true);
        thisComp.setFont(new Font("Times New Roman",Font.BOLD,35));
        //thisComp.setHorizontalAlignment(SwingConstants.CENTER);
        thisComp.setBorder(line);
        c.addMouseListener(new MouseListener() {

            @Override
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                thisComp.setBackground(new Color(96,125,244));

            }

            @Override
            public void mouseExited(java.awt.event.MouseEvent evt) {
                thisComp.setBackground(new Color(36,77,240));
                            }

    });
}


}

我在JPanel中有一个jFrame,我在这里添加PlayLabel

panel.add(new PlayLabel(new JLabel("PLAY")));

这些礼节不适用于我创建的PlayLabel:

thisComp.setAlignmentX(Component.CENTER_ALIGNMENT);
  thisComp.setBackground(new Color(36,77,240));
  thisComp.setMaximumSize(new Dimension(150,75));
  thisComp.setForeground(Color.WHITE);
  thisComp.setFont(new Font("Times New Roman",Font.BOLD,35));

我做错了什么?

1 个答案:

答案 0 :(得分:2)

您正在向BorderLayout添加c并为thisComp设置这些属性。