简体   繁体   English

防止带有 html 的 JLabel 断线

[英]Prevent JLabel with html from breaking line

Wondering if anyone knows how to disable line break when using html in JLabel.想知道是否有人知道在 JLabel 中使用 html 时如何禁用换行符。

Here is the code:这是代码:

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JOptionPane;

import java.awt.GridLayout;

public class Main 
{
    private static final int[] TEXT = 
    {   0x05D0, 0x05B2, 0x05DC, 
        0x05B5, 0x05D9, 0x05DB, 
        0x05B6, 0x05DD 
    };

    public static void main(String[] args) 
    {
        String text = "";
        for(int cp : TEXT)
            text += Character.toString(cp);
    
        String html = "<html>" + text + "</html>";
    
        JLabel label = new JLabel(html);
        JLabel msg = new JLabel("The text should at least go out to here.");
        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(2,1));
        panel.add(label);
        panel.add(msg);
        
        JOptionPane.showMessageDialog(null, panel);
    }
}

The bidi text breaks into two lines.比迪文本分成两行。 I am trying to write html to a JTree node so it will support multiple font families.我正在尝试将 html 写入 JTree 节点,以便它支持多种字体系列。 I can't get it to work with a JLabel.我无法让它与 JLabel 一起工作。 I'm thinking I might need to paint it in a cell renderer.我想我可能需要在单元格渲染器中绘制它。 I was hoping to get the html to work.我希望让 html 工作。 It would make things a lot easier.这会让事情变得容易很多。

Any suggestions?有什么建议?

=== Edit === When my display setting in Windows is at 125% it breaks the line; === 编辑 === 当我在 Windows 中的显示设置为 125% 时,它会断线; however, when I change my display setting in Windows to 100% it does not break the line.但是,当我将 Windows 中的显示设置更改为 100% 时,它不会断线。 Running 1920 x 1080 display.运行 1920 x 1080 显示器。 Anyone have any ideas?有人有想法么? Or, is anyone able to repeat the breaking of the line?或者,有没有人能够重复断线?

=== Edit === === 编辑 ===

Interestingly when I pass -Dsun.java2d.uiScale= with 1.0 or 2.0 it works.有趣的是,当我使用 1.0 或 2.0 传递 -Dsun.java2d.uiScale= 时,它可以工作。 When I use 3.0, 4.0, 1.25 or 1.5 or 0.8 it does not work.当我使用 3.0、4.0、1.25 或 1.5 或 0.8 时,它不起作用。

Well, I submitted a bug report.好吧,我提交了一个错误报告。 I managed to find a workaround that seems to work for all Windows Display Scale's but it is quite a hacky workaround.我设法找到了一种似乎适用于所有 Windows Display Scale 的变通方法,但它是一个相当笨拙的变通方法。 But it will be sufficient until the bug is fixed.但是在修复错误之前就足够了。

Here it is:这里是:

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JOptionPane;

import java.awt.Graphics;
import java.awt.image.BufferedImage;

import java.awt.GridLayout;

public class Main 
{
    private static final String TEXT = 
    "\u05D0\u05B2\u05DC\u05B5\u05D9\u05DB\u05B6\u05DD";

    public static void main(String[] args) 
    {
        JLabel label = new JLabel();
        Graphics g = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB).getGraphics();
        int width = g.getFontMetrics(label.getFont()).stringWidth(TEXT);
    
        String html = "<html><p style=\"width:"+width+"px;\">" + TEXT + "</p></html>";
    
        label.setText(html);
        JLabel msg = new JLabel("The text should at least go out to here.");
        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(2,1));
        panel.add(label);
        panel.add(msg);
    
        JOptionPane.showMessageDialog(null, panel);
    }
}

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

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