简体   繁体   中英

How to work with JLabel?

在此处输入图片说明

Hi i am trying to make java desktop application where i am using JLabel I wrote some test on JLabel I want to set text from the top and I am using multiple line in JLabel I want to set different different color for every line.

Here is my code:

JLabel label = new JLabel("<html>Case &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Item&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; CaseNum<br>Party1<br>Party2</html>");

How can I achieve this?

You can try using the html tables for new lines as below,

import java.awt.Dimension;

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

/**
 * @author JayaPrasad
 * 
 */
public class SwingHtml {

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        JLabel label = new JLabel(
                "<html>Case &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Item&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; CaseNum<table><tr><font color=blue>Party1</font></tr><tr><font color=red>Party2</font></tr></table></html>");
        frame.add(label);
        frame.setSize(new Dimension(250, 130));
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

Output:

在此处输入图片说明

为此,请使用JTable ,其呈现方式与Nimbus PLAF中的呈现方式相同。

You can do something like this:

 <html><p><font color="green">line 1</font></p><br /><p><font color="red">line2</font></p></html>

if you want to change the line 1 text colour and add a new line 2 with a new colour

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM