如何在运行时突出显示JLabel的一部分? [英] how to highlight part of a JLabel at runtime?

查看:180
本文介绍了如何在运行时突出显示JLabel的一部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在JPanel中有一个JLabel.我将Netbeans IDE用于我的Java程序的GUI(免费设计,没有布局管理器).

I have a JLabel inside a JPanel. I'm using Netbeans IDE for the GUI of my java program (free design, and no Layout Manager).

在运行时,我需要突出显示我的JLabel的一部分.可以按以下说明突出显示JLabel的一部分:如何突出显示JLabel的一部分JLabel?

At runtime I need to highlight a part of my JLabel. Highlighting a part of JLabel can be done as said here : How to highlight part of a JLabel?

我不知道如何在运行时执行此操作,我的意思是我已经有一个JLabel,现在如何为此目的覆盖它的paintComponent()方法?

I don't know how to do that in run time, I mean I already have a JLabel, now how can I override it's paintComponent() method for that purpose ?

推荐答案

以下是Tikhon Jelvis在您提到的帖子中给出的答案的说明性示例,您所需要做的就是添加一些字段(在本示例中为startend)表示要突出显示的区域,并使用一种方法(在本示例中为highlightRegion)来设置以下字段:

Here is an illustrative example of the answer given by Tikhon Jelvis in the post you mentioned, all you need is to add some fields (in this example start and end) to indicate the regions to be highlighted, and use a method (highlightRegion in this example) to set these fields:

import java.awt.Color;
import java.awt.FontMetrics;
import java.awt.Graphics;

import javax.swing.JFrame;
import javax.swing.JLabel;

public class Main
{
    public static void main(String[] argv)
    {
        JFrame frame = new JFrame("Highlighting");
        frame.setSize(300, 100);

        Label label = new Label();
        label.setText("Money does not buy happiness");
        label.setVerticalAlignment(JLabel.TOP);
        label.highlightRegion(3, 15);

        frame.add(label);
        frame.setVisible(true);
    }

    static class Label extends JLabel
    {
        private static final long serialVersionUID = 1L;
        private int start;
        private int end;

        @Override
        public void paint(Graphics g)
        {
            FontMetrics fontMetrics = g.getFontMetrics();

            String startString = getText().substring(0, start);
            String text = getText().substring(start, end);

            int startX = fontMetrics.stringWidth(startString);
            int startY = 0;

            int length = fontMetrics.stringWidth(text);
            int height = fontMetrics.getHeight();

            g.setColor(new Color(0x33, 0x66, 0xFF, 0x66));
            g.fillRect(startX, startY, length, height);

            super.paint(g);
        }

        public void highlightRegion(int start, int end)
        {
            this.start = start;
            this.end = end;
        }
    }

}

请注意,使用label.setVerticalAlignment(JLabel.TOP);可以简化操作.如果不使用它,则需要进行额外的计算以确定要突出显示的区域的确切垂直位置.

Notice the use of label.setVerticalAlignment(JLabel.TOP); to simplify things. If you don't use it then you need to do extra computations to determine the exact vertical location of the region to be highlighted.

这篇关于如何在运行时突出显示JLabel的一部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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