, JPanels, BorderLayout. , .
, GridBagLayout , . , . , .


import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.GridBagLayout;
import javax.swing.JLabel;
import java.awt.GridBagConstraints;
import java.awt.Color;
import java.awt.Insets;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class GridBagLayout_Example extends JFrame {
private JPanel contentPane;
private JLabel lblVariableXVariableY;
private JButton btnVariableXFixedY;
private JLabel lblFixedXVariableY;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
GridBagLayout_Example frame = new GridBagLayout_Example();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public GridBagLayout_Example() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
this.contentPane = new JPanel();
this.contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(this.contentPane);
GridBagLayout gbl_contentPane = new GridBagLayout();
gbl_contentPane.columnWidths = new int[]{0, 0, 0};
gbl_contentPane.rowHeights = new int[]{0, 0, 0};
gbl_contentPane.columnWeights = new double[]{0.0, 1.0, Double.MIN_VALUE};
gbl_contentPane.rowWeights = new double[]{1.0, 0.0, Double.MIN_VALUE};
this.contentPane.setLayout(gbl_contentPane);
this.lblFixedXVariableY = new JLabel("Fixed x, Variable y");
this.lblFixedXVariableY.setOpaque(true);
this.lblFixedXVariableY.setBackground(new Color(233, 150, 122));
GridBagConstraints gbc_lblFixedXVariableY = new GridBagConstraints();
gbc_lblFixedXVariableY.fill = GridBagConstraints.VERTICAL;
gbc_lblFixedXVariableY.gridheight = 2;
gbc_lblFixedXVariableY.insets = new Insets(0, 0, 5, 5);
gbc_lblFixedXVariableY.gridx = 0;
gbc_lblFixedXVariableY.gridy = 0;
this.contentPane.add(this.lblFixedXVariableY, gbc_lblFixedXVariableY);
this.lblVariableXVariableY = new JLabel("Variable x & y");
this.lblVariableXVariableY.setBackground(new Color(147, 112, 219));
this.lblVariableXVariableY.setOpaque(true);
GridBagConstraints gbc_lblVariableXVariableY = new GridBagConstraints();
gbc_lblVariableXVariableY.fill = GridBagConstraints.BOTH;
gbc_lblVariableXVariableY.insets = new Insets(0, 0, 5, 0);
gbc_lblVariableXVariableY.gridx = 1;
gbc_lblVariableXVariableY.gridy = 0;
this.contentPane.add(this.lblVariableXVariableY, gbc_lblVariableXVariableY);
this.btnVariableXFixedY = new JButton("Variable x, Fixed y");
this.btnVariableXFixedY.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
}
});
this.btnVariableXFixedY.setOpaque(true);
GridBagConstraints gbc_btnVariableXFixedY = new GridBagConstraints();
gbc_btnVariableXFixedY.fill = GridBagConstraints.HORIZONTAL;
gbc_btnVariableXFixedY.gridx = 1;
gbc_btnVariableXFixedY.gridy = 1;
this.contentPane.add(this.btnVariableXFixedY, gbc_btnVariableXFixedY);
}
}