user3429531
user3429531

Reputation: 355

Gui problems when enlarged

I have a question on my GUI. after I ran my codes. my GUI will display like the picture shown below

not enlarged

But when I enlarged it to full screen. it will become like this. enlarged

How do I keep in the center and just like the size in picture one only when enlarged to full screen?

import java.awt.BorderLayout;
import java.awt.GridLayout;    
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class GuiTest {    
    JFrame frame = new JFrame("Gui Test");
    JPanel panel = new JPanel(new GridLayout(4,0));
    JPanel panelTwo = new JPanel(new BorderLayout());
    JLabel labelOne = new JLabel("Text One:");
    JLabel labelTwo = new JLabel("Text Two:");
    JTextField textFieldOne = new JTextField(15);  
    JTextField textFieldTwo = new JTextField(15);
    JTextField textFieldThree = new JTextField(15);

    public GuiTest() {
        panel.add(labelOne);
        panel.add(textFieldOne);
        panel.add(labelTwo);
        panel.add(textFieldTwo);
        panelTwo.add(panel, BorderLayout.CENTER);
        frame.add(panelTwo);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.pack();
        frame.getPreferredSize(); 
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        //frame.setResizable(false);
    }

    public static void main(String[]args) { 
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new GuiTest();
            }
        });
    }

}   

Upvotes: 1

Views: 151

Answers (1)

Jan Bodnar
Jan Bodnar

Reputation: 11657

Yet another GridLayout confusion!

It is stretching its children and there is nothing that we can do about it. GridLayout is a very simple manager with very limited practical usage. Skip it and skip BorderLayout as well. The only thing that you need to know about BorderLayout is that it is the default manager of the frame's content pane.

Layout management is a complex thing -- there not shortcuts. One has to put some effort to learn it. The good news is that there are some very capable layout managers. I recommend the following two:

  • MigLayout
  • GroupLayout

JGoodies' FormLayout is a good option too. These managers are doing it properly. Study them carefully and choose your favourite.

I provide two solutions, one with MigLayout, one with GroupLayout.

MigLayout solution

MigLayout is a third-party layout manager for which we need to dowload a single jar file.

package com.zetcode;

import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import net.miginfocom.swing.MigLayout;

public class MigLayoutEx extends JFrame {

    public MigLayoutEx() {

        initUI();
    }

    private void initUI() {

        JPanel pnl = new JPanel(new MigLayout("ins dialog, center, "
                + "center, wrap"));

        pnl.add(new JLabel("Text one"));
        pnl.add(new JTextField(15));
        pnl.add(new JLabel("Text two"));
        pnl.add(new JTextField(15));

        add(pnl);

        pack();

        setTitle("MigLayout example");
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }    


    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                MigLayoutEx ex = new MigLayoutEx();
                ex.setVisible(true);
            }
        });       
    }
}

MigLayout solution

GroupLayout solution

GroupLayout is a built-in layout manager.

package com.zetcode;

import java.awt.Container;
import java.awt.EventQueue;
import javax.swing.GroupLayout;
import static javax.swing.GroupLayout.Alignment.LEADING;
import static javax.swing.GroupLayout.DEFAULT_SIZE;
import static javax.swing.GroupLayout.PREFERRED_SIZE;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class GroupLayoutEx extends JFrame {

    public GroupLayoutEx() {

        initUI();
    }

    private void initUI() {

        Container pane = getContentPane();
        GroupLayout gl = new GroupLayout(pane);
        pane.setLayout(gl);

        JLabel lbl1 = new JLabel("Text one");
        JLabel lbl2 = new JLabel("Text two");

        JTextField field1 = new JTextField(15);
        JTextField field2 = new JTextField(15);

        gl.setAutoCreateGaps(true);
        gl.setAutoCreateContainerGaps(true);

        gl.setHorizontalGroup(gl.createSequentialGroup()
                .addGap(5, 50, Short.MAX_VALUE)
                .addGroup(gl.createParallelGroup(LEADING)
                .addComponent(lbl1)
                .addComponent(field1, DEFAULT_SIZE, DEFAULT_SIZE, 
                        PREFERRED_SIZE)
                .addComponent(lbl2)
                .addComponent(field2, DEFAULT_SIZE, DEFAULT_SIZE, 
                        PREFERRED_SIZE))
                .addGap(5, 50, Short.MAX_VALUE)
        );

        gl.setVerticalGroup(gl.createSequentialGroup()
                .addGap(5, 50, Short.MAX_VALUE)
                .addGroup(gl.createSequentialGroup()
                        .addComponent(lbl1)
                        .addComponent(field1, DEFAULT_SIZE, DEFAULT_SIZE,
                                PREFERRED_SIZE)
                        .addComponent(lbl2)
                        .addComponent(field2, DEFAULT_SIZE, DEFAULT_SIZE,
                                PREFERRED_SIZE))
                .addGap(5, 50, Short.MAX_VALUE)
        );

        pack();

        setTitle("GroupLayout example");
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                GroupLayoutEx ex = new GroupLayoutEx();
                ex.setVisible(true);
            }
        });
    }
}

Upvotes: 2

Related Questions