简体   繁体   English

JLabel 不显示

[英]JLabel not displaying

I am trying to add a linebreak in a JLabel.我正在尝试在 JLabel 中添加换行符。 '\n' does not work and I have found that using HTML should be a solution, but whenever I try anything with HTML it causes my JFrame to disappear or not be visible anymore. '\n' 不起作用,我发现使用 HTML 应该是一个解决方案,但是每当我尝试使用 HTML 进行任何操作时,它都会导致我的 JFrame 消失或不再可见。 I am using NetBeans 8.2 and Java JDK 1.8.0_251.我正在使用 NetBeans 8.2 和 Java JDK 1.8.0_251。 Is there any solution to this?有什么解决办法吗? Thank you谢谢

 WindowMain = new JFrame("Spasitel");
   WindowMain.setSize(960, 720);
   WindowMain.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   WindowMain.getContentPane().setBackground(Color.BLACK);
   //WindowMain.getContentPane().setFont(Courier New);
   WindowMain.setLayout(null);
   WindowMain.setResizable(false);
   WindowMain.setVisible(true);
   Toolkit toolkit = Toolkit.getDefaultToolkit();
   Dimension size = toolkit.getScreenSize();
   WindowMain.setLocation(size.width/2 - WindowMain.getWidth()/2, size.height/2 - WindowMain.getHeight()/2);

   JPanel TitleFrame = new JPanel();
   TitleFrame.setBounds(50,50, 860, 200);
   TitleFrame.setBackground(Color.WHITE);
   JLabel TitleText = new JLabel ("<html>Hello World!<br/>blahblahblah</html>", SwingConstants.CENTER);
   //TitleText.setFont("Courier New", Font.PLAIN, 18);
   TitleFrame.add(TitleText);
   TitleFrame.setForeground(Color.GREEN);
   TitleFrame.show();
   TitleFrame.setVisible(true);
   WindowMain.add(TitleFrame);

With HTML带 HTML

Without HTML不带 HTML

I created the following GUI.我创建了以下 GUI。

JLabel 示例

I used a JFrame , a JPanel , and a JLabel .我使用了JFrameJPanelJLabel I called the SwingUtilities invokeLater method to start the Swing GUI on the Event Dispatch Thread,我调用SwingUtilities invokeLater方法在事件调度线程上启动 Swing GUI,

I used Swing layout managers.我使用了 Swing 布局管理器。

Here's the complete runnable code.这是完整的可运行代码。

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class JLabelExample implements Runnable {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new JLabelExample());
    }

    @Override
    public void run() {
        JFrame frame = new JFrame("JLabel Example");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        frame.add(createMainPanel(), BorderLayout.CENTER);
        
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }
    
    private JPanel createMainPanel() {
        JPanel panel = new JPanel(new FlowLayout());
        panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        
        String s = "<html>This is a test of the emergency broadcast system.  ";
        s += "<br />This is only a test.";
        
        JLabel label = new JLabel(s);
        panel.add(label);
        
        return panel;
    }

}

You cannot use html tags with a null layout, try setting a layout.您不能将 html 标签与 null 布局一起使用,请尝试设置布局。

Eg:例如:

WindowMain.setLayout(new GridBagLayout());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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