Witam,
ostatnio zacząłem "walkę" z menadżerem układu GridBagLayout i wszystko fajnie pięknie tylko pojawił mi się mały "problem". Jest sposób aby ustalić że jakieś elementy muszą być zawsze równe? Chodzi mi o to że napisałem coś takiego:
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;
import javax.swing.SwingConstants;
public class GridBadLayoutTest extends JFrame
{
private JScrollPane scroll;
public GridBadLayoutTest()
{
GridBagLayout layout = new GridBagLayout();
setLayout(layout);
GridBagConstraints c = new GridBagConstraints();
JTextArea pole1 = new JTextArea(25, 15);
JTextArea pole2 = new JTextArea(25, 15);
JTextArea pole3 = new JTextArea(25, 15);
JScrollPane scroll;
c.fill = GridBagConstraints.BOTH;
c.weightx = 0.5;
c.anchor = GridBagConstraints.NORTHWEST;
c.gridx = 0;
c.gridy = 0;
c.gridheight = 1;
c.gridwidth = 6;
add(new JLabel("Label"), c);
c.anchor = GridBagConstraints.NORTH;
c.gridx = 6;
c.gridy = 0;
c.gridheight = 1;
c.gridwidth = 6;
add(new JLabel("Label"), c);
c.anchor = GridBagConstraints.NORTH;
c.gridx = 12;
c.gridy = 0;
c.gridheight = 1;
c.gridwidth = 6;
add(new JLabel("Label"), c);
c.weighty = 0.5;
c.anchor = GridBagConstraints.NORTHWEST;
c.gridx = 0;
c.gridy = 1;
c.gridwidth = 6;
c.gridheight = 3;
scroll = new JScrollPane(pole1);
scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
add(scroll,c);
c.anchor = GridBagConstraints.NORTH;
c.gridx = 6;
c.gridy = 1;
c.gridwidth = 6;
c.gridheight = 3;
scroll = new JScrollPane(pole2);
scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
add(scroll,c);
c.anchor = GridBagConstraints.NORTHEAST;
c.gridx = 12;
c.gridy = 1;
c.gridwidth = 6;
c.gridheight = 3;
scroll = new JScrollPane(pole3);
scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
add(scroll,c);
c.anchor = GridBagConstraints.NORTHWEST;
c.weighty = 0.0;
c.gridx = 5;
c.gridy = 4;
c.gridheight = 1;
c.gridwidth = 1;
add(new JLabel("Label ",SwingConstants.RIGHT), c);
c.gridx = 6;
c.gridy = 4;
add(new JTextField(), c);
c.gridx = 5;
c.gridy = 5;
add(new JLabel("Label",SwingConstants.RIGHT), c);
c.gridx = 6;
c.gridy = 5;
add(new JComboBox<String>(), c);
c.gridx = 5;
c.gridy = 6;
add(new JLabel("Label",SwingConstants.RIGHT), c);
c.gridx = 6;
c.gridy = 6;
add(new JTextField(), c);
c.gridx = 7;
c.gridy = 6;
add(new JTextField(), c);
c.gridx = 8;
c.gridy = 6;
add(new JTextField(), c);
c.gridx = 12;
c.gridy = 6;
add(new JLabel("Godz:",SwingConstants.RIGHT), c);
c.gridx = 13;
c.gridy = 6;
add(new JTextField(), c);
c.gridx = 14;
c.gridy = 6;
add(new JLabel("Min:",SwingConstants.RIGHT), c);
c.gridx = 15;
c.gridy = 6;
add(new JTextField(), c);
c.gridx = 16;
c.gridy = 6;
add(new JLabel("Sek:",SwingConstants.RIGHT), c);
c.gridx = 17;
c.gridy = 6;
add(new JTextField(), c);
pack();
}
}
i zależy mi aby w czasie zmieniania rozmiaru okienka pola (pole1,** pole2**, pole3) były zawsze sobie równe. Jest to wykonalne?