0

我正在尝试装饰一个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));
                            }

    });
}


}

我有一个JPaneljFrame添加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));

我究竟做错了什么 ?

4

1 回答 1

2

您将 c 添加到 BorderLayout 并为 thisComp 设置这些属性。

于 2013-05-13T15:02:13.263 回答