mohammad saadeh
mohammad saadeh

Reputation: 35

How to create layout with Swing?

I want to design like frame using four panel, taking into account zoom in and out the screen...

Like this design:

JPanel testPanel = new JPanel();
testPanel.setBorder(BorderFactory.createLineBorder(Color.black));
frame.add(testPanel, BorderLayout.NORTH);

JPanel testPanel1 = new JPanel();
testPanel1.setBorder(BorderFactory.createLineBorder(Color.black));
frame.add(testPanel1, BorderLayout.NORTH);

JPanel testPane2 = new JPanel();
testPane2.setBorder(BorderFactory.createLineBorder(Color.black));
frame.add(testPane2, BorderLayout.EAST);

JPanel testPane3 = new JPanel();
testPane3.setBorder(BorderFactory.createLineBorder(Color.black));
frame.add(testPane3, BorderLayout.CENTER);

Upvotes: 0

Views: 465

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347204

You could...

Use a GridBagLayout...

Example

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new GridBagLayout());

            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.weightx = 1;
            gbc.fill = GridBagConstraints.BOTH;
            add(makePanel("panel 2"), gbc);
            gbc.gridy++;
            add(makePanel("panel 3"), gbc);
            gbc.gridy++;
            gbc.weighty = 0.66;
            gbc.weightx = 0.66;
            add(makePanel("panel 3"), gbc);

            gbc.gridx++;
            gbc.gridy = 0;
            gbc.gridheight = gbc.REMAINDER;
            add(makePanel("panel 1"), gbc);
        }

        protected JPanel makePanel(String name) {
            JPanel panel = new JPanel(new GridBagLayout());
            panel.add(new JLabel(name));
            panel.setBorder(new CompoundBorder(new LineBorder(Color.DARK_GRAY), new EmptyBorder(10, 10, 10, 10)));
            return panel;
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(500, 400);
        }



    }

}

You could...

Also make use of compound layouts, this is useful in situations where the rules are complex and/or only effect certain components.

Which you might use will depend on what you are trying to achieve, at the end of the day, there is not hard and fast rule, you need to need look at the underlying requirements and consider which approaches are the best - don't forget, you can combine different approaches to build different solutions

Don't forget to have a look at Laying Out Components Within a Container for more details

Upvotes: 2

Related Questions