Это код следующее:
Код: Выделить всё
/*
* GridBagLayoutDemo.java requires no other files.
*/
import java.awt.*;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class JavaSwingLayout {
final static boolean shouldFill = true;
final static boolean shouldWeightX = true;
final static boolean RIGHT_TO_LEFT = false;
public static void addComponentsToPane(Container pane) {
if (RIGHT_TO_LEFT) {
pane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
}
JLabel label;
JPanel section;
Color color;
pane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
if (shouldFill) {
// natural height, maximum width
c.fill = GridBagConstraints.VERTICAL;
}
// Turquoise
label = new JLabel();
label.setOpaque(true);
color = new Color(76, 235, 169);
label.setBackground(color);
c.fill = GridBagConstraints.VERTICAL;
c.gridx = 0;
c.gridy = 0;
c.gridheight = 4;
c.ipadx = 150;
c.ipady = 100;
c.insets = new Insets(5, 5, 5, 5);
pane.add(label, c);
// Section
section = new JPanel();
section.setLayout(new GridBagLayout());
c.fill = GridBagConstraints.NONE;
c.gridx = 0;
c.gridy = 4;
c.gridheight = 4;
c.ipadx = 0;
c.ipady = 0;
c.weighty = 0;
c.weightx = 0;
c.insets = new Insets(0, 0, 0, 0);
pane.add(section, c);
// red
label = new JLabel();
label.setOpaque(true);
color = new Color(254, 0, 0);
label.setBackground(color);
c.fill = GridBagConstraints.VERTICAL;
c.gridx = 0;
c.gridy = 0;
c.gridheight = 1;
c.ipadx = 50;
c.ipady = 33;
c.insets = new Insets(0, 0, 0, 0);
section.add(label, c);
// orange
label = new JLabel();
label.setOpaque(true);
label.setOpaque(true);
color = new Color(255, 109, 0);
label.setBackground(color);
c.fill = GridBagConstraints.BOTH;
c.gridx = 0;
c.gridy = 1;
c.gridheight = 2;
c.ipadx = 0;
c.ipady = 0;
section.add(label, c);
//magenta
label = new JLabel();
label.setOpaque(true);
label.setOpaque(true);
color = new Color(240, 0, 255);
label.setBackground(color);
c.fill = GridBagConstraints.BOTH;
c.gridx = 1;
c.gridy = 0;
c.gridheight = 3;
c.gridwidth = 2;
c.ipadx = 100;
c.ipady = 100;
section.add(label, c);
// Green
label = new JLabel();
label.setOpaque(true);
color = new Color(56, 120, 0);
label.setBackground(color);
c.fill = GridBagConstraints.VERTICAL;
c.gridx = 1;
c.gridy = 0;
c.gridheight = 2;
c.ipadx = 100;
c.ipady = 50;
c.insets = new Insets(5, 5, 5, 5);
pane.add(label, c);
// Blue
label = new JLabel();
label.setOpaque(true);
color = new Color(1, 80, 159);
label.setBackground(color);
c.fill = GridBagConstraints.VERTICAL;
c.gridx = 1;
c.gridy = 2;
c.gridheight = 5;
c.ipady = 115;
pane.add(label, c);
// Tan
label = new JLabel();
label.setOpaque(true);
color = new Color(201, 157, 97);
label.setBackground(color);
c.fill = GridBagConstraints.VERTICAL;
c.gridx = 1;
c.gridy = 7;
c.gridheight = 1;
c.ipady = 25;
pane.add(label, c);
}
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event-dispatching thread.
*/
private static void createAndShowGUI() {
// Create and set up the window.
JFrame frame = new JFrame("GridBagLayoutDemo");
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Set up the content pane.
addComponentsToPane(frame.getContentPane());
// Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
// Schedule a job for the event-dispatching thread:
// creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
[img]https://i.sstatic .net/M64W7Znp.png[/img]
Но я хочу, чтобы раздел, вызываемый JPanel, со своим собственным GridBagLayout, не растягивал пространство между JLabels по вертикали таким образом, я хочу, чтобы он занимал то же пространство, что и бирюзовый JLabel над ним.
Перед добавлением JLabels в раздел JPanel он выглядит так:

Но после их добавления искажается пространство, которое должно занимать, причем настолько, насколько Я пытаюсь, но не могу это исправить.
Подробнее здесь: https://stackoverflow.com/questions/790 ... other-jpan