更改标签文本轮廓的粗细 [英] change the thickness of the outline of the label text

查看:34
本文介绍了更改标签文本轮廓的粗细的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一开始,我想为我的 Jlabel 添加一个大纲,我找到了这个来源

解决方案

我改变了轮廓粗细.

我修改了 OutlineLabel 类的 paint 方法以包含厚度值.

这是修改后的完整可运行代码.

import java.awt.BorderLayout;导入 java.awt.Color;导入 java.awt.Font;导入 java.awt.Graphics;导入 javax.swing.JFrame;导入 javax.swing.JLabel;导入 javax.swing.SwingUtilities;导入 javax.swing.border.Border;导入 javax.swing.border.CompoundBorder;导入 javax.swing.border.EmptyBorder;公共类 OutlineLabelGUI 实现 Runnable {公共静态无效主(字符串 [] args){SwingUtilities.invokeLater(new OutlineLabelGUI());}@覆盖公共无效运行(){JFrame w = new JFrame();w.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);OutlineLabel label = new OutlineLabel("更改"+轮廓粗细",JLabel.CENTER,3);label.setFont(new Font("Serif", Font.BOLD, 48));label.setOutlineColor(Color.black);label.setForeground(Color.white);label.setOpaque(true);w.add(label, BorderLayout.CENTER);w.pack();w.setLocationByPlatform(true);w.setVisible(true);}公共类 OutlineLabel 扩展 JLabel {private static final long serialVersionUID = 1L;私有颜色outlineColor = Color.WHITE;私人布尔 isPaintingOutline = false;私有布尔 forceTransparent = false;私有最终整数厚度;公共轮廓标签(整数厚度){极好的();this.thickness = 厚度;设置边界(厚度);}公共大纲标签(字符串文本,整数厚度){超级(文本);this.thickness = 厚度;设置边界(厚度);}公共大纲标签(字符串文本,int水平对齐,整数厚度){超级(文本,水平对齐);this.thickness = 厚度;设置边界(厚度);}私有无效 setBorder(整数厚度){Border border = getBorder();Border margin = new EmptyBorder(thickness,thickness + 3,厚度,厚度 + 3);setBorder(new CompoundBorder(border, margin));}公共颜色 getOutlineColor() {返回轮廓颜色;}公共无效setOutlineColor(颜色outlineColor){this.outlineColor = 大纲颜色;this.invalidate();}@覆盖公共颜色 getForeground() {如果(isPaintingOutline){返回轮廓颜色;} 别的 {返回 super.getForeground();}}@覆盖公共布尔 isOpaque() {如果(强制透明){返回假;} 别的 {返回 super.isOpaque();}}@覆盖公共无效油漆(图形g){字符串文本 = getText();if (text == null || text.length() == 0) {super.paint(g);返回;}//1 2 3//8 9 4//7 6 5如果(isOpaque()){super.paint(g);}forceTransparent = 真;isPaintingOutline = true;g.translate(-thickness, -thickness);super.paint(g);//1g.translate(厚度, 0);super.paint(g);//2g.translate(厚度, 0);super.paint(g);//3g.translate(0, 厚度);super.paint(g);//4g.translate(0, 厚度);super.paint(g);//5g.translate(-thickness, 0);super.paint(g);//6g.translate(-thickness, 0);super.paint(g);//7g.translate(0, -thickness);super.paint(g);//8g.translate(厚度, 0);//9isPaintingOutline = false;super.paint(g);forceTransparent = 假;}}}

In the beginning, I wanted to add an outline for my Jlabel and I found this source https://stackoverflow.com/a/23521196/14949008 and it works but I can't change the size of the outline thickness.

I wanted to change the thickness of the label text to be thicker using the same code (because this code is simple to Implement)...but I didn't understand how to change the thickness of the outline....any help would be appropriated

package Test2;

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

public class OutlineLabel extends JLabel {

    private Color outlineColor = Color.WHITE;
    private boolean isPaintingOutline = false;
    private boolean forceTransparent = false;

    public OutlineLabel() {
        super();
    }

    public OutlineLabel(String text) {
        super(text);
    }

    public OutlineLabel(String text, int horizontalAlignment) {
        super(text, horizontalAlignment);
    }

    public Color getOutlineColor() {
        return outlineColor;
    }

    public void setOutlineColor(Color outlineColor) {
        this.outlineColor = outlineColor;
        this.invalidate();
    }

    @Override
    public Color getForeground() {
        if (isPaintingOutline) {
            return outlineColor;
        } else {
            return super.getForeground();
        }
    }

    @Override
    public boolean isOpaque() {
        if (forceTransparent) {
            return false;
        } else {
            return super.isOpaque();
        }
    }

    @Override
    public void paint(Graphics g) {

        String text = getText();
        if (text == null || text.length() == 0) {
            super.paint(g);
            return;
        }

        // 1 2 3
        // 8 9 4
        // 7 6 5

        if (isOpaque())
            super.paint(g);

        forceTransparent = true;
        isPaintingOutline = true;
        g.translate(-1, -1);
        super.paint(g); // 1
        g.translate(1, 0);
        super.paint(g); // 2
        g.translate(1, 0);
        super.paint(g); // 3
        g.translate(0, 1);
        super.paint(g); // 4
        g.translate(0, 1);
        super.paint(g); // 5
        g.translate(-1, 0);
        super.paint(g); // 6
        g.translate(-1, 0);
        super.paint(g); // 7
        g.translate(0, -1);
        super.paint(g); // 8
        g.translate(1, 0); // 9
        isPaintingOutline = false;

        super.paint(g);
        forceTransparent = false;

    }

    public static void main(String[] args) {
        JFrame w = new JFrame();
        w.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        OutlineLabel label = new OutlineLabel("change the outline thickness", OutlineLabel.CENTER);
        label.setFont(new Font("Serif", Font.BOLD, 48));
        label.setOutlineColor(Color.black);
        label.setForeground(Color.white);
        label.setOpaque(true);
        w.setContentPane(new JPanel(new BorderLayout()));
        w.add(label, BorderLayout.CENTER);
        w.pack();
        w.setVisible(true);
    }
}

and here is how the output looks like :

解决方案

I changed the outline thickness.

I modified the paint method of the OutlineLabel class to include a thickness value.

Here's the modified complete runnable code.

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
import javax.swing.border.Border;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;

public class OutlineLabelGUI implements Runnable {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new OutlineLabelGUI());
    }

    @Override
    public void run() {
        JFrame w = new JFrame();
        w.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       
        OutlineLabel label = new OutlineLabel("change the "
                + "outline thickness", JLabel.CENTER, 3);
        label.setFont(new Font("Serif", Font.BOLD, 48));
        label.setOutlineColor(Color.black);
        label.setForeground(Color.white);
        label.setOpaque(true);
       
        w.add(label, BorderLayout.CENTER);
        w.pack();
        w.setLocationByPlatform(true);
        w.setVisible(true);
    }

    public class OutlineLabel extends JLabel {

        private static final long serialVersionUID = 1L;
        
        private Color outlineColor = Color.WHITE;
        
        private boolean isPaintingOutline = false;
        private boolean forceTransparent = false;
        
        private final int thickness;

        public OutlineLabel(int thickness) {
            super();
            this.thickness = thickness;
            setBorder(thickness);
        }

        public OutlineLabel(String text, int thickness) {
            super(text);
            this.thickness = thickness;
            setBorder(thickness);
        }

        public OutlineLabel(String text, int horizontalAlignment, 
                int thickness) {
            super(text, horizontalAlignment);
            this.thickness = thickness;
            setBorder(thickness);
        }
        
        private void setBorder(int thickness) {
            Border border = getBorder();
            Border margin = new EmptyBorder(thickness, thickness + 3, 
                    thickness, thickness + 3);
            setBorder(new CompoundBorder(border, margin));
        }

        public Color getOutlineColor() {
            return outlineColor;
        }

        public void setOutlineColor(Color outlineColor) {
            this.outlineColor = outlineColor;
            this.invalidate();
        }

        @Override
        public Color getForeground() {
            if (isPaintingOutline) {
                return outlineColor;
            } else {
                return super.getForeground();
            }
        }

        @Override
        public boolean isOpaque() {
            if (forceTransparent) {
                return false;
            } else {
                return super.isOpaque();
            }
        }

        @Override
        public void paint(Graphics g) {
            String text = getText();
            if (text == null || text.length() == 0) {
                super.paint(g);
                return;
            }

            // 1 2 3
            // 8 9 4
            // 7 6 5

            if (isOpaque()) {
                super.paint(g);
            }

            forceTransparent = true;
            isPaintingOutline = true;
            g.translate(-thickness, -thickness);
            super.paint(g); // 1
            g.translate(thickness, 0);
            super.paint(g); // 2
            g.translate(thickness, 0);
            super.paint(g); // 3
            g.translate(0, thickness);
            super.paint(g); // 4
            g.translate(0, thickness);
            super.paint(g); // 5
            g.translate(-thickness, 0);
            super.paint(g); // 6
            g.translate(-thickness, 0);
            super.paint(g); // 7
            g.translate(0, -thickness);
            super.paint(g); // 8
            g.translate(thickness, 0); // 9
            isPaintingOutline = false;

            super.paint(g);
            forceTransparent = false;
        }

    }

}

这篇关于更改标签文本轮廓的粗细的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆