如何改变秋千标签的差距 [英] How to change gap in swing label

查看:97
本文介绍了如何改变秋千标签的差距的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一些标签:

leftLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
leftLabel.setFont(new Font(FONT, Font.PLAIN, 280));
leftLabel.setBorder(BorderFactory.createTitledBorder("aaa"));
leftLabel.setText("0");

如下所示:

正如您所看到的那样,上下差距很大。我怎么能减少呢?

As you can see there are big gap up and down. How I can reduce it ?

推荐答案

你需要调整边框插图,

import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Insets;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
import javax.swing.border.TitledBorder;

public final class TitledBorderDemo {
    private static void createAndShowGUI(){
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new FlowLayout());
        frame.add(new TitledLabel(String.valueOf(0)));
        frame.pack();
        frame.setVisible(true);
    }

    private static class TitledLabel extends JLabel{
        private static final long serialVersionUID = 1L;
        private static final String TITLE = "aaa";

        TitledLabel(String text){
            super(text);
            setAlignmentX(Component.CENTER_ALIGNMENT);
            setFont(new Font("Arial", Font.PLAIN, 280));
            setBorder(new TitledBorder(TITLE){
                private static final long serialVersionUID = 1L;

                @Override
                public Insets getBorderInsets(Component c, Insets insets){
                    // arbitrary insets for top and bottom.
                    return new Insets(insets.top - 45, insets.left, insets.bottom - 55, insets.right);
            }});

        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable(){
            @Override
            public void run() {
                createAndShowGUI();             
            }
        });
    }
}

希望这能让你开始朝着正确的方向前进!

Hopefully this gets you started in the right direction!

这篇关于如何改变秋千标签的差距的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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