swing设置一个有颜色的JLabel

1、swing设置一个有颜色的JLabel

2、代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public class ColorLabel {
    public static void main(String[] args) {
        JFrame jFrame = new JFrame();
        jFrame.setTitle("");
        jFrame.setSize(600, 300);
        Container cPane = jFrame.getContentPane();
        JLabel blueLabel = new JLabel("A");
        blueLabel.setOpaque(true);
        //blueLabel.setPreferredSize(new Dimension(60, 20));
        blueLabel.setBackground(new Color(193,210,240));
        JLabel greenLabel = new JLabel("B");
        greenLabel.setBackground(Color.GREEN);
        greenLabel.setOpaque(true);
        //greenLabel.setPreferredSize(new Dimension(60, 20));
        JLabel brownLabel = new JLabel("C");
        brownLabel.setBackground(new Color(255,192,0));
        brownLabel.setOpaque(true);
        //brownLabel.setPreferredSize(new Dimension(60, 40));
        JPanel labelPanel = new JPanel();
        FlowLayout flowLayout = new FlowLayout(FlowLayout.LEFT);
        labelPanel.setLayout(flowLayout);
        labelPanel.add(blueLabel);
        labelPanel.add(greenLabel);
        labelPanel.add(brownLabel);
        cPane.add(labelPanel);
        jFrame.setVisible(true);
    }
}

3、效果

 

注意:JLabel只有设置setOpaque为true,颜色效果才会生效

 

posted @   信铁寒胜  阅读(306)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
点击右上角即可分享
微信分享提示