How to resize button text – swing in Java

I'm new here and have taken my first step in Java

For learning purposes, I'm trying to create a simple GUI using a grid containing numbered buttons The problem is that the text of the button does not adjust according to the size of the button

Let me show you the code first so you can see what I mean:

import javax.swing.*;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


public class SimpleGUI implements ActionListener {

    private JFrame frame;
    private JPanel grid;
    private JButton[] buttons;

    public SimpleGUI() {
        frame=new JFrame("Buttons grid");
        frame.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE);


        grid=new JPanel();
        buttons=new JButton[100];


        for (int i=0; i<buttons.length; i++) {
            buttons[i]=new JButton(Integer.toString(i+1));
            buttons[i].setActionCommand(Integer.toString(i+1));
            grid.add(buttons[i]);
            buttons[i].addActionListener(this);
        }

        frame.getContentPane().add(grid);
    }

    public static void main(String[] args) {
        SimpleGUI gui=new SimpleGUI();
        gui.go();
    }


    public void go() {
        grid.setLayout(new GridLayout(10,10));

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


    public void actionPerformed(ActionEvent e) {
        String buttonNum=e.getActionCommand();
        System.out.println("You've pressed "+buttonNum);
    }

}

I can't upload pictures, so you must run this code to view the results The problem is that the button is too small to hold text, so instead of numbers, three dots appear (...) only when I expand the window (thereby expanding the size of the button accordingly) will the number display correctly on the button Is there a way for the text to resize the button itself so that it is always visible?

Thank you in advance and forgive my bad language (English is my second language)

Solution

It does provide more space for the text in the button The extra white space around the text is controlled by margins and can be changed:

button.setMargin(new Insets(0,0));

If you want the button to be square by default, you can override its preferred size:

buttons[i] = new JButton(Integer.toString(i + 1)) {
    @Override
    public Dimension getPreferredSize() {
        Dimension naturalSize = super.getPreferredSize();
        int sideLength = Math.max(naturalSize.width,naturalSize.height);
        return new Dimension(sideLength,sideLength);
    }
};

Then the initial layout will be square, when using pack () (according to Andrew Thompson's answer, you should use it)

Edit: the preferred size has been changed to take into account the normal preferred size of the button (write to A. Thompson again)

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>