更改标签文本轮廓的粗细
change the thickness of the outline of the label text
一开始,我想为我的 Jlabel 添加轮廓,我找到了这个来源 ,它可以工作,但我无法更改轮廓粗细的大小。
我想使用相同的代码将标签文本的粗细更改为更粗(因为此代码易于实现)...但我不明白如何更改轮廓的粗细.. ..任何帮助都会被挪用
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);
}
}
输出如下:
我改变了轮廓粗细。
我修改了 OutlineLabel
class 的 paint
方法以包含厚度值。
这是修改后的完整可运行代码。
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;
}
}
}
一开始,我想为我的 Jlabel 添加轮廓,我找到了这个来源 ,它可以工作,但我无法更改轮廓粗细的大小。
我想使用相同的代码将标签文本的粗细更改为更粗(因为此代码易于实现)...但我不明白如何更改轮廓的粗细.. ..任何帮助都会被挪用
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);
}
}
输出如下:
我改变了轮廓粗细。
我修改了 OutlineLabel
class 的 paint
方法以包含厚度值。
这是修改后的完整可运行代码。
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;
}
}
}