Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to adjust buttons' text to buttons' size - Java's Swing
    primarykey
    data
    text
    <p>I'm new here, and making my first steps in Java.</p> <p>I'm trying, for learning purposes, to create a simple GUI with a grid containing numbered buttons. Problem is, the buttons' text will not adjust itself to the buttons' size.</p> <p>let me first show you the code, so you'll see what I mean:</p> <pre><code>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&lt;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); } } </code></pre> <p>I can't upload pictures, so you'd have to run this code to see the result. The thing is, the buttons are too small for the text to fit in, so instead of numbers, three dots will appear on them. (<strong>...</strong>) 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?</p> <p>Thanks in advanced, and forgive my poor language (English is my second language).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload