0

I am trying to decorate a JLabel but the proprieties i set don't work .

Decorator Class :

public class Decorator extends JComponent{

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

PlayLabel class :

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));
                            }

    });
}


}

I have a JPanel in a jFrame where i add PlayLabel like this :

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

These proprieties don't work as intended for the PlayLabel i created :

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));

What am I doing wrong ?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Dubioz
  • 25
  • 3
  • Side note: don't you want `PlayLabel` to take a `JLabel` in its constructor? I don't see what's gained by being flexible and accepting a `JComponent`. – Duncan Jones May 13 '13 at 14:06
  • Have you tested that those properties work "as intended" without using decoration? I.e. apply them manually to a normal `JLabel`? – Duncan Jones May 13 '13 at 14:08
  • i tested the proprieties without decorator and they work on a normal jLabel – Dubioz May 13 '13 at 14:16
  • 1
    For better help sooner, post an [SSCCE](http://sscce.org/). – Andrew Thompson May 13 '13 at 14:24
  • `add("Center",c);` - that is not the way to specify a constraint when add a component to a panel. Read the Container API to find the appropriate method. Also, don't hardcode values. Every layout manager provides static variables for the allowed constraints. – camickr May 13 '13 at 15:18

1 Answers1

2

You are adding c to BorderLayout and you set these properties for thisComp.