java label 超链接_如何在JLabel中添加超链接?

我想提供另一种解决方案。它与已经提出的类似,因为它在JLabel中使用HTML代码,并在其上注册MouseListener,但是当您将鼠标移到链接上时它也会显示HandCursor,因此外观和感觉就像大多数用户所期望的那样。如果平台不支持浏览,则不会创建可能误导用户的带蓝色下划线的HTML链接。相反,链接只是以纯文本形式呈现。这可以与@ dimo414提出的SwingLink类结合使用。public class JLabelLink extends JFrame {private static final String LABEL_TEXT = "For further information visit:";private static final String A_VALID_LINK = "http://stackoverflow.com";private static final String A_HREF = "";private static final String HREF_END = "";private static final String HTML = "";private static final String HTML_END = "";public JLabelLink() {

setTitle("HTML link via a JLabel");

setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

Container contentPane = getContentPane();

contentPane.setLayout(new FlowLayout(FlowLayout.LEFT));

JLabel label = new JLabel(LABE

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是在Java创建带圆角的JLabel的最简单代码: ``` import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.RenderingHints; import javax.swing.BorderFactory; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; public class RoundedLabel extends JLabel { private int radius; private Color backgroundColor; public RoundedLabel(String text, int radius, Color backgroundColor) { super(text); this.radius = radius; this.backgroundColor = backgroundColor; setOpaque(false); setBorder(BorderFactory.createEmptyBorder(5, 10, 5, 10)); } @Override protected void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D) g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2.setColor(backgroundColor); g2.fillRoundRect(0, 0, getWidth(), getHeight(), radius, radius); super.paintComponent(g); } public static void main(String[] args) { JFrame frame = new JFrame("Rounded Label"); JPanel panel = new JPanel(); panel.setBackground(Color.WHITE); RoundedLabel label = new RoundedLabel("Hello world!", 15, Color.YELLOW); panel.add(label); frame.add(panel); frame.pack(); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } ``` 在这个例子,我们创建了一个自定义的JLabel,名为RoundedLabel。我们可以通过调用它的构造函数来创建一个带圆角的JLabel。在构造函数,我们设置了标签的文本、圆角半径和背景颜色,并调用了setOpaque(false)和setBorder(BorderFactory.createEmptyBorder(5, 10, 5, 10))来去除默认的标签边框和背景。在paintComponent方法,我们使用Graphics2D类来绘制一个带圆角的矩形作为标签的背景,并调用了super.paintComponent(g)来绘制标签的文本。 最后,我们在main方法创建了一个JFrame和一个JPanel,并将RoundedLabel添加到JPanel。我们也可以通过调用label.setBackground(Color.RED)来更改标签的背景颜色。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值