Center panel when resizing a window

I would like to keep the panel that I created using the absolute layout in the center of my window, even when the window is resized (if possible). I met a couple of sentences here and [here] [2], but not cubes! Below is a sample code, any ideas or suggestions? I have no problem centering one component, such as JLable, but I want to center the panel with many components!

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.BorderFactory;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import javax.swing.border.Border;
import javax.swing.JLabel;


public class TestPanel extends JFrame {

    private JLabel lblSetupTitle;
    private Border compoundBorder, outlineColorBorder, outlineBorder;
    private JTextArea txtrManageData;
    private JPanel childPanel;

    public TestPanel() 
    {
        setBackground(Color.white);
        outlineColorBorder = BorderFactory.createLineBorder(Color.gray);
        outlineBorder = BorderFactory.createEmptyBorder(20, 20, 20, 20);
        compoundBorder = BorderFactory.createCompoundBorder(outlineColorBorder, outlineBorder);

        lblSetupTitle = new JLabel("Setup");
        lblSetupTitle.setBounds(443, 288, 44, 23);
        txtrManageData = new JTextArea("Text Area Text");
        txtrManageData.setBounds(393, 322, 142, 61);

        childPanel = new JPanel();
        childPanel.setLocation(89, 38);
        childPanel.setSize(921, 452);
        childPanel.setBorder(compoundBorder);

        setupGUIElements();
        setupPanel();
    }

    private void setupGUIElements()
    {
        txtrManageData.setBackground(null);
        txtrManageData.setLineWrap(true);
        txtrManageData.setWrapStyleWord(true);
    }

    private void setupPanel()
    {
        getContentPane().setLayout(new GridBagLayout()); // set layout of parent panel to GridBagLayout
        childPanel.setLayout(null); // set layout of child panel to AbsoluteLayout
        childPanel.add(lblSetupTitle);
        childPanel.add(txtrManageData);

        getContentPane().add(childPanel, new GridBagConstraints());

        this.setSize(1020, 500);
    }

    public static void main(String[] args) {

        SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                TestPanel ex = new TestPanel();
                ex.setVisible(true);
            }
        });
    }
}

EDIT: any tips, links, guidelines for creating something like this

example gui i would like to create

+1
source share
2 answers

I would lay out the layouts.

enter image description here

enter image description here

import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;

public class ThreeButtonTextFieldCombo {

    private JPanel ui = null;

    ThreeButtonTextFieldCombo() {
        initUI();
    }

    public final void initUI() {
        if (ui!=null) return;
        ui = new JPanel(new GridBagLayout());
        ui.setBorder(new TitledBorder("Parent Panel"));

        JPanel controls = new JPanel(new GridLayout(1,0,10,10));
        ui.add(controls);
        controls.setBackground(Color.RED);
        controls.setBorder(new TitledBorder("Child Panel"));

        for (int ii=1; ii<4; ii++) {
            addLabelAndField(controls, "String " + ii);
        }
    } 

    public JComponent getUI() {
        return ui;
    }

    private void addLabelAndField(JPanel panel, String text) {
        JPanel controls = new JPanel(new BorderLayout(3, 3));
        controls.setBorder(new EmptyBorder(20,20,20,20));
        JLabel l = new JLabel(text);
        controls.add(l, BorderLayout.PAGE_START);

        JTextArea ta = new JTextArea(text, 2, 8);
        controls.add(new JScrollPane(ta));

        panel.add(controls);
    }

    public static void main(String[] args) {
        Runnable r = new Runnable() {

            @Override
            public void run() {
                JFrame f = new JFrame("Three Button/Text Field Combo");
                f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                f.setLocationByPlatform(true);
                ThreeButtonTextFieldCombo tbtfc = 
                        new ThreeButtonTextFieldCombo();
                f.setContentPane(tbtfc.getUI());
                f.pack();
                f.setMinimumSize(f.getSize());
                f.setVisible(true);
            }
        };
        SwingUtilities.invokeLater(r);
    }
}
+2
source

, GridBagConstraints. , .

getContentPane().add(childPanel, new GridBagConstraints());

:

getContentPane().add(childPanel);

, , , setBounds (..).

, , . GridBagLayout, . Bounds (..) .

, .

:

labell1.setBounds(443, 288, 44, 23);

:

labell2.setBounds(443 + someXDisplacement, 288, 44, 23);

.. :

labell3.setBounds(443 + (someXDisplacement x 2), 288, 44, 23);

.

0

All Articles