Java Swing: how can I resize a single row in a Grid layout and allow it to be dynamically resized only horizontally and not vertically?

Hi everyone, I have a JFrame in which there are three components.

  • Menu
  • Tab bar
  • panel with several buttons

My initial goal was to create a design such as: enter image description here

when the user resizes the application, everything will be resized. So I thought, maybe if I used a simple grid layout, my problem would be solved, so I decided to do the following:

  • Some panels will be embedded in the tabbed panels, and these panels will follow the layout of the grid.

  • The panel below will follow the grid pattern.

  • JFrame will also follow the layout of the grid.

Results:

enter image description here

, , , , :

enter image description here

, . ( , , ), , , , , . ? gridbaglayout, , . , , :

enter image description here

, , , :

enter image description here

.

+5
2

BorderLayout . BorderLayout.CENTER BorderLayout.SOUTH.

+10

JFrame .

. BorderLayout ( , JFrame GridLayout)

  • BorderLayout.SOUTH
  • - ( BorderLayout.CENTER)

Swing LayoutManager , , , BorderLayout.

:

import java.awt.BorderLayout;
import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.SwingUtilities;

public class TestLayout {

    protected void initUI() {
        final JFrame frame = new JFrame(TestLayout.class.getSimpleName());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JMenuBar menuBar = new JMenuBar();
        JMenu fileMenu = new JMenu("File");
        JMenuItem newItem = new JMenuItem("New...");
        JMenuItem open = new JMenuItem("Open...");
        fileMenu.add(newItem);
        fileMenu.add(open);
        menuBar.add(fileMenu);
        JTabbedPane tabs = new JTabbedPane();
        tabs.addTab("Tab 1", new JPanel());
        tabs.addTab("Tab 2", new JPanel());
        tabs.addTab("Tab 3", new JPanel());
        JPanel buttonPanel = new JPanel(new GridLayout());
        buttonPanel.add(new JButton("Button-1"));
        buttonPanel.add(new JButton("Button-2"));
        buttonPanel.add(new JButton("Button-3"));
        frame.add(tabs);
        frame.add(buttonPanel, BorderLayout.SOUTH);
        frame.setJMenuBar(menuBar);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new TestLayout().initUI();
            }
        });
    }
}
+7

All Articles