java-swing-JLabel(色彩标签)

7 篇文章 0 订阅

java-swing-JLabel(色彩标签):

package com.etc.jlabel;

import javax.swing.*;

/**
 * JLabel 色彩标签演练
 */
public class SwingDemo {

    private static void createGIU() {

//        MyFrame frame = new MyFrame("color ");

        // 使用优化后的 MyFrame2 测试
        MyFrameYouHua frame = new MyFrameYouHua("color");

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.setSize(400, 300);

        frame.setVisible(true);
    }

    public static void main(String[] args) {

        //  new 一个接口就是new一个实现这个接口的对象,创建了一个匿名类的对象,为了实现run方法。
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                createGIU();
            }
        });
    }
}

package com.etc.jlabel;

import javax.swing.*;
import java.awt.*;

public class MyFrame extends JFrame {

    public MyFrame(String title) throws HeadlessException {
        super(title);

        Container container = getContentPane();
        container.setLayout(new FlowLayout());

        JLabel a1 = new JLabel("1");

        // 设置背景为不透明(默认透明)
        a1.setOpaque(true);

        // 设置背景的颜色
        a1.setBackground(Color.YELLOW);

        // 设置区域尺寸
        a1.setPreferredSize(new Dimension(60, 30));

        // 设置水平对齐
        a1.setHorizontalAlignment(SwingConstants.CENTER);


        JLabel a2 = new JLabel("2");
        a2.setOpaque(true);
        a2.setBackground(Color.BLUE);
        a2.setPreferredSize(new Dimension(60, 30));
        a2.setHorizontalAlignment(SwingConstants.CENTER);

        JLabel a3 = new JLabel("3");
        a3.setOpaque(true);
        a3.setBackground(Color.GREEN);
        a3.setPreferredSize(new Dimension(60, 30));
        a3.setHorizontalAlignment(SwingConstants.CENTER);


        JLabel a4 = new JLabel("4");
        a4.setOpaque(true);
        a4.setBackground(Color.RED);
        a4.setPreferredSize(new Dimension(60, 30));
        a4.setHorizontalAlignment(SwingConstants.CENTER);

        container.add(a1);
        container.add(a2);
        container.add(a3);
        container.add(a4);
    }
}

优化处理:在MyFrame 的基础上进行优化:

package com.etc.jlabel;

import javax.swing.*;
import java.awt.*;

/**
 * 在MyFrame 的基础上进行优化
 */
public class MyFrameYouHua extends JFrame {

    public MyFrameYouHua(String title) throws HeadlessException {
        super(title);

        Container container = getContentPane();
        container.setLayout(new FlowLayout());

        ColorLabel a1 = new ColorLabel("1", Color.YELLOW);
        ColorLabel a2 = new ColorLabel("2", Color.BLUE);
        ColorLabel a3 = new ColorLabel("3", Color.GREEN);
        ColorLabel a4 = new ColorLabel("4", Color.RED);


        container.add(a1);
        container.add(a2);
        container.add(a3);
        container.add(a4);

    }


    private static class ColorLabel extends JLabel {

        public ColorLabel(String text, Color bgColor) {

            super(text);

            // 设置背景为不透明(默认透明)
            setOpaque(true);

            // 设置背景的颜色
            setBackground(bgColor);

            // 设置区域尺寸
            setPreferredSize(new Dimension(180, 30));

            // 设置水平对齐(文本居中)
            setHorizontalAlignment(SwingConstants.CENTER);

        }
    }
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值