0

I'm new here. I wrote a simple program in Java and for the GUI I'm using GridBagLayout. The problem is that it doesn't paginate well until I touch the border of the window to resize the Frame. Here are some pictures of what I mean:

The main window before I resize the frame

The main window after I resize the frame

A part of my code:

public class TW2Tempi
{

    public static void main(String[] args)
    {
        MainFrame mf = new MainFrame();
    }

}

class MainFrame extends JFrame
{
    GridBagConstraints gbc = new GridBagConstraints();
    //other variables .....

    public MainFrame()
    {
        super("Calcola i tempi dei tuoi attacchi");
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new GridBagLayout());
        setPreferredSize(new Dimension(500, 300));
        setMinimumSize(new Dimension(500, 300));
        //setResizable(false); //I would also like to add this but then I wouldn't be able to "resize" and see everything (second picture)

        //row 1 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
        //where gbc.gridy = 0

        nobile = new JLabel("Orario di arrivo del nobile", SwingConstants.RIGHT);//right align
        gbc.fill = GridBagConstraints.BOTH;
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.gridwidth = 1;
        gbc.insets = new Insets(7,10,0,0);
        gbc.fill = GridBagConstraints.BOTH;
        add(nobile, gbc);
        ....

Do I have to initialize GridBagConstraints in a different way?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
mastodilu
  • 119
  • 1
  • 9
  • 1) `setVisible(true);` should be last, immediately after `pack()`.. (I guessed this was the problem from reading the opening lines of the question - it is a common problem.) 2) In future, please post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 3) `setPreferredSize(new Dimension(500, 300));` Don't guess this kind of stuff. See [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?](http://stackoverflow.com/q/7229226/418556) (Yes.) – Andrew Thompson Sep 27 '17 at 09:03
  • it worked, thanks! – mastodilu Sep 27 '17 at 09:39

0 Answers0