java 如何在 JLabel 上设置填充

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33172555/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 21:17:40  来源:igfitidea点击:

How to set padding at JLabel

javaswingjpaneljlabel

提问by bircastri

I want to display a Multiline JLabel into JPanel. So I have this code but I'm not able to show the multiline JLabel.

我想在 JPanel 中显示多行 JLabel。所以我有这个代码,但我无法显示多行 JLabel。

public class NotificationFrame extends JFrame{
    public NotificationFrame(){
        JPanel panelBody = new JPanel();
        panelBody.setBackground(Color.white);
        GridBagConstraints GBC2 = new GridBagConstraints();
        Container CR2 = new Container();
        GridBagLayout GBL2 = new GridBagLayout();
        CR2.setLayout(GBL2);     
        panelBody.add(CR2);

        GBC2 = new GridBagConstraints();
        CR2.add(labelTesto);
        GBC2.gridx=0;
        GBC2.gridy=0;
        GBC2.insets.left = 10;
        GBC2.insets.top=0;
        GBL2.setConstraints(labelTesto,GBC2);
        panelBody.setLayout(new FlowLayout(FlowLayout.CENTER)); 


        add(panelBody,BorderLayout.CENTER);
    }
}

If I change the last line of code in

如果我更改最后一行代码

add(labelTest,BorderLayout.CENTER);

I can show that I want. But it is not correct because I want to set a padding on JLabel

我可以表现出我想要的。但这不正确,因为我想在 JLabel 上设置填充

EDIT

编辑

I have use this code now:

我现在已经使用了这个代码:

JPanel panelBody = new JPanel();
panelBody.setBackground(Color.white);
SpringLayout layout = new SpringLayout();
panelBody.setLayout(layout);
panelBody.add(labelTesto);
layout.putConstraint(SpringLayout.NORTH, labelTesto, 15, SpringLayout.NORTH, panelBody);
add(panelBody,BorderLayout.CENTER);

This is the layout:

这是布局:

enter image description here

在此处输入图片说明

This is the all test that I should see: "Il 31 Dicembre scadrà l'assistenza, ricorda di rinnovare l'assistenza per ricevere sempre assistenza ed aggiornamenti."

这是我应该看到的所有测试:“Il 31 Dicembre scadrà l'assistenza, ricorda di rinnovare l'assistenza per Ricevere semperassistenza ed aggiornamenti。”

回答by tec

Multiline it's not padding. For achieve multiline text in JLabel or JButton you need to use HTML.

多行它不是填充。要在 JLabel 或 JButton 中实现多行文本,您需要使用 HTML。

To wrap text (or multiline):

换行文本(或多行):

btn.setText("<html><p>Il 31 Dicembre scadrà l'assistenza, ricorda di rinnovare l'assistenza per ricevere sempre assistenza ed aggiornamenti</p></html>");
label.setText("<html>Date:<br></html>"+getDateFromSomeWhere);

To set padding:

设置填充:

    label.setBorder(new EmptyBorder(0,10,0,0));//top,left,bottom,right