简体   繁体   中英

How to adjust buttons' text to buttons' size - Java's Swing

I'm trying to create a simple GUI with a grid containing numbered buttons. Problem is, the buttons' text will not adjust itself to the buttons' size.

let me first show you the code, so you'll 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);
    }
    
}

The buttons are too small for the text to fit in, so instead of numbers, three dots will appear on them. ( ... ) Only when I expand the window (and thus expanding the buttons' dimension accordingly), the numbers appear on the buttons properly. Is there a way to make the text adjust itself to the buttons' dimension so it'll always be visible?

It indeed is possible to give a bit more space to the text within the buttons. The extra empty space around the text is controlled by the margins and that can be changed:

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

If you want to make the buttons square by default, you can override their 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() (which you should be using, as per Andrew Thompson's answer).

Edit: Changed the preferred size to take in account the button's normal preferred dimensions. (Credits to A. Thompson again).

Make the change seen below.

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

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

The trick here is not to change the buttons at all, but to get the GUI to adjust to the size of the buttons. That is achieved by calling pack() which..

Causes this Window to be sized to fit the preferred size and layouts of its subcomponents. ..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM