Code:
import java.awt.*;
import javax.swing.*;
public class test extends JPanel {
private static final long serialVersionUID = 1L;
public test() {
this.setLayout(new GridBagLayout());
addComponentInLayout(getWelcomePanel(),
0, 0, 1, 1,
0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0,0,0,0));
addComponentInLayout(getSplitPanel(),
0, 1, 1, 1,
1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0,0,0,0));
}
private JPanel getWelcomePanel() {
JPanel welcomePanel = new JPanel();
welcomePanel.setLayout(new FlowLayout());
JLabel welcomeLabel = new JLabel("Willkommen ...");
welcomeLabel.setFont(new java.awt.Font("MS Sans Serif", Font.BOLD, 11));
welcomePanel.add(welcomeLabel);
return welcomePanel;
}
private JPanel getSplitPanel() {
JPanel splitPane = new JPanel();
JPanel sub1 = new JPanel();
sub1.setBackground(Color.red);
JPanel sub2 = new JPanel();
sub2.setBackground(Color.BLACK);
splitPane.setLayout(new GridBagLayout());
addComponentInContainer(splitPane, sub1,
0, 0, 1, 1,
0.75, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0,0,0,0));
addComponentInContainer(splitPane, sub2,
1, 0, 1, 1,
0.25, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0,0,0,0));
addComponentInContainer(sub1, new JLabel("bli "),
0, 0, 1, 1,
0.75, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0,0,0,0));
addComponentInContainer(sub1, new JLabel("bla"),
0, 1, 1, 1,
0.75, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0,0,0,0));
addComponentInContainer(sub1, new JLabel("blub"),
0, 2, 1, 1,
0.75, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0,0,0,0));
addComponentInContainer(sub1, new JLabel("blä"),
0, 3, 1, 1,
0.75, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0,0,0,0));
addComponentInContainer(sub1, new JLabel("blü"),
0, 4, 1, 1,
0.75, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0,0,0,0));
addComponentInContainer(sub2, new JLabel("sche"),
0, 0, 1, 1,
0.75, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0,0,0,0));
addComponentInContainer(sub2, new JLabel("Schri"),
0, 1, 1, 1,
0.75, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0,0,0,0));
return splitPane;
}
//Fügt eine Componenten in das GridbagLayout ein
public void addComponentInLayout(Component aComp, int gridX, int gridY, int gridWidth, int gridHeight, double weightX,
double weightY, int anchor, int fill, Insets insets) {
GridBagConstraints theConstraints = new GridBagConstraints(gridX, gridY, gridWidth, gridHeight, weightX,
weightY, anchor, fill, insets, 0, 0);
add(aComp, theConstraints);
}
// Fügt eine Componenten in ein z.B. Panel ein.
public void addComponentInContainer(JComponent aPanel,Component aComp, int gridX, int gridY, int gridWidth, int gridHeight, double weightX,
double weightY, int anchor, int fill, Insets insets) {
GridBagConstraints theConstraints = new GridBagConstraints(gridX, gridY, gridWidth, gridHeight, weightX,
weightY, anchor, fill, insets, 0, 0);
aPanel.add(aComp, theConstraints);
}
}
wie muss ich die absoluten angaben machen? ich habs zuerst mit Gridbagconstraint.relative versucht bei der x,y, width und height angabe aber das hat irgendwie auch nicht funktioniert
ich mein die ausgabe ist da aber er setzt die dinge nicht untereinander. den schwarzen balken seh ich auch erst dann wenn ich das fenster über meinen ganzen bildschirm aufziehe in der breite..
und was ist eigentlich dieses insets (nebenfrage)