带HTML的JLabel没有正确设置宽度 [英] JLabel with HTML does not set width properly

查看:129
本文介绍了带HTML的JLabel没有正确设置宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



请考虑以下代码:

  import javax.swing。*; 

public class Xyzzy extends JFrame {
public static void main(String [] args){
javax.swing.SwingUtilities.invokeLater(new Runnable(){
public void run(){
Xyzzy frame = new Xyzzy();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setLayout(new BoxLayout(frame.getContentPane( ),

String s =x;
for(int i = 0; i <200; ++ i)
s + =x ;

JLabel jl = new JLabel(< html>< div style = \width:300px; \>+ s +< / div>< / html>);

frame.add(jl);

frame.setSize(600,600);
frame.setVisible(true);
}
});
}
}

我预计JLabel的宽度为300像素,但实际上它大约是390个像素。如果我将宽度规格更改为200px,则生成的标签宽度大约为260像素。

我做错了什么?

解决方案

这个html代码对JLabel来说太复杂了(仅支持HTML规范的一部分)
http://www.apl.jhu.edu/~hall/java/Swing-Tutorial/Swing-Tutorial-JLabel .html

组件的大小取决于布局。
我更喜欢MigLayout http://www.miglayout.com 简单教程
编辑:JLabel中的HTML非常过时


I am trying to set the width of a JLabel using an HTML div tag.

Consider the following code:

import javax.swing.*;

public class Xyzzy extends JFrame{
    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                Xyzzy frame = new Xyzzy();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                frame.setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.PAGE_AXIS));

                String s = "x ";
                for (int i=0; i<200; ++i)
                    s += "x ";

                JLabel jl = new JLabel("<html><div style=\"width: 300px;\">" + s + "</div></html>");

                frame.add(jl);

                frame.setSize(600, 600);
                frame.setVisible(true);
            }
        });
    }
}

I would have expected the JLabel to be 300 pixels wide, but in reality it is about 390 pixels wide. If I change the width specification to 200px, the resulting label is about 260 pixels wide.

What am I doing wrong?

解决方案

This html code is too complicated for JLabel (support only part of HTML specification) http://www.apl.jhu.edu/~hall/java/Swing-Tutorial/Swing-Tutorial-JLabel.html
Size of components depends on Layout. I prefer MigLayout http://www.miglayout.com simple tutorial

Edit: HTML in JLabel is very outdated

这篇关于带HTML的JLabel没有正确设置宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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