Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change gap in swing label

Tags:

java

swing

jlabel

I create some label:

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

which look like this: enter image description here

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

like image 662
hudi Avatar asked Apr 14 '12 21:04

hudi


People also ask

How to remove fixed gap in NetBeans?

Double-click the gap at the bottom of the last button. The Edit Layout Space dialog box is displayed. In the Edit Layout Space dialog box, select the Resizable option and click OK.

How do you adjust the size of a swing frame?

Using setSize() you can give the size of frame you want but if you use pack() , it will automatically change the size of the frames according to the size of components in it.

How do you put a border on a swing?

To put a border around a JComponent , you use its setBorder method. You can use the BorderFactory class to create most of the borders that Swing provides. If you need a reference to a border — say, because you want to use it in multiple components — you can save it in a variable of type Border .


2 Answers

You need to tweak the border insets,

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();             
            }
        });
    }
}

enter image description here

Hopefully this gets you started in the right direction!

like image 79
user1329572 Avatar answered Sep 29 '22 18:09

user1329572


the problem is probably within the properties of the newly created border. These borders have insets. That is the only thing I can think of to influence the gaps. Try to call a method on the border that changes these insets to [1,1,1,1].

like image 40
MarioDS Avatar answered Sep 29 '22 18:09

MarioDS