+------------------------------+
| |
| |
| label: textfeld |
| |
| liste |
| |
| |
| |
+------------------------------+
GridBagConstraints tmp = new GridBagConstraints();
tmp.anchor = GridBagConstraints.NORTHEAST;
add(Component comp, Object constraints);
+- frame ----------------------+
| |
| |
| +- gridbaglayout --+ |
| | label: textfeld | |
| | | |
| | liste | |
| +------------------+ |
| |
| |
+------------------------------+
JFrame frame = new JFrame("HelloWorldSwing");
frame.getContentPane().setLayout(new FlowLayout(FlowLayout.LEFT));
((FlowLayout)frame.getLayout()).setAlignment(FlowLayout.LEFT);
Öhm, ne, das stimmt nicht. Das GridBagLayout fängt oben links an. Es unterteilt den Container in kleine Zellen, wobei oben link 0,0 ist. Die Anzahl der Spalten wird dynamisch bestimmt. Aber bevor ich mir die Finger blutig tippe:ich glaub, auch wenn man als standard ein gridbaglayout nimmt, fängt alles in der mitte an, ich mein das gridbaglayout selbst fängt in der mitte vom frame an und nicht oben links
Semerzo hat gesagt.:Öhm, ne, das stimmt nicht. Das GridBagLayout fängt oben links an.
Roar hat gesagt.:ja, vorausgesetzt du gibst die richtigen werte bei den gbc an. mal schnell layout zusammentippen is da nich mit gbl. das ding wird mir immer unsympathischer :x
package coding;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;
public class SwingStarter {
private JLabel label;
public SwingStarter() {
//Make sure we have nice window decorations.
JFrame.setDefaultLookAndFeelDecorated(false);
//Create and set up the window.
JFrame frame = new JFrame("Hello GridLayout!");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new GridBagLayout());
GridBagConstraints tmp = new GridBagConstraints();
//Bestimmt die Orientierung innerhalb der Zelle.
tmp.anchor = GridBagConstraints.NORTHWEST;
// Wenn _alle_ dieser Werte auf 0 sind, default, wird der übrige
// Platz rings um den GBL verteilt. Es sieht dann so aus,
// als wäre alles zentriert. Sollte eine Tasche, GridagLayoutConstraint (GBL),
// vom Default abweichen, nimmt sie sich allen Platz den sie bekommen kann.
// Die weight[x | y] gibt den prozentualen Wert an, den sich ein GBL schnappen
// darf. Wenn alle ausser einer dabei default wären, schnappt sie sich alles.
tmp.weightx = 0.1;
tmp.weighty = 0.1;
label = new JLabel("0,0");
// Damit ihr mir glaubt, dass die nicht ihre Tasche 'fillen'
Border border = new LineBorder(new Color(0,0,0));
label.setBorder(border);
tmp.gridx = 0;
tmp.gridy = 0;
frame.getContentPane().add(label, tmp);
label = new JLabel("1,0");
tmp.gridx = 1;
tmp.gridy = 0;
frame.getContentPane().add(label, tmp);
label = new JLabel("0,1");
tmp.gridx = 0;
tmp.gridy = 1;
frame.getContentPane().add(label, tmp);
label = new JLabel("1,1");
tmp.gridx = 1;
tmp.gridy = 1;
frame.getContentPane().add(label, tmp);
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
new SwingStarter();
}
}