简体   繁体   中英

JLabel disappears when using html

I have a JLabel that is going to have a lot of text in it so I need to add new lines to it. I found out that in order to do this you need to use html in your JLabel and <br> for a new line. Well, as soon as I add HTML to my JLabel it disappears!!

Here is a picture of the JLabel with the following code:

instructionLabel = new JLabel("lol");
instructionLabel.setBounds(25,10,500,100);
add(instructionLabel);

http://prntscr.com/9p1cme

Now, when I change the code, as soon as I add html, it disappears:

instructionLabel = new JLabel("<html>lol</html>");
instructionLabel.setBounds(25,10,500,100);
add(instructionLabel);

http://prntscr.com/9p1d4u

The program gives no errors & I am willing to provide more code if necessary.

Avoid using null layouts, pixel perfect layouts are an illusion within modern ui design. There are too many factors which affect the individual size of components, none of which you can control. Swing was designed to work with layout managers at the core, discarding these will lead to no end of issues and problems that you will spend more and more time trying to rectify

HtmlLabel

import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class LabelExample {

    public static void main(String[] args) {
        new LabelExample();
    }

    public LabelExample() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            add(new JLabel("Look ma, no html"));
            add(new JLabel("<html>Look ma, html</html>"));
        }

    }

}

See Laying Out Components Within a Container for more details

Based on this requirement...

需求

I did a simple GridBagLayout and produced this...

得分

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.LineBorder;

public class LabelExample {

    public static void main(String[] args) {
        new LabelExample();
    }

    public LabelExample() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new GridBagLayout());
            JLabel points = new JLabel("<html><b>Points: 0</b></html>");
            JLabel highscore = new JLabel("<html><b>Highscore: 0</b></html>");
            JLabel score = new JLabel("97");
            score.setFont(score.getFont().deriveFont(Font.BOLD, 96));

            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.weightx = 1;
            add(points, gbc);
            gbc.gridx = 2;
            add(highscore, gbc);

            gbc.weightx = 0;
            gbc.weighty = 1;
            gbc.gridx = 1;
            gbc.anchor = GridBagConstraints.CENTER;
            gbc.gridy++;
            gbc.ipadx = 50;
            gbc.ipady = 50;
            score.setHorizontalAlignment(JLabel.CENTER);
            add(score, gbc);

            JButton high = new JButton("High");
            JButton low = new JButton("Low");

            gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 2;
            gbc.fill = GridBagConstraints.HORIZONTAL;
            add(high, gbc);
            gbc.gridx = 2;
            add(low, gbc);
        }


    }

}

This is just a rough start to provide a basic example of the idea

// You can try below code to solve your problem. 
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.Font;

public class Tutorial extends JFrame {

    public static void main(String[] args) {
     new Tutorial();
    }

    public Tutorial() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        getContentPane().setLayout(null);

        JLabel instructionLabel = new JLabel("<html><p>lol <br> Working</p></html>");
        instructionLabel.setFont(new Font("Tahoma", Font.BOLD, 12));
        instructionLabel.setBounds(25,10,500,100);
        getContentPane().add(instructionLabel);
        setVisible(true);
    }
}

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