G
Guest
Gast
Hallo zusammen,
ich habe mal zwei Dinge mit GBL ausprobiert. Eine Sache macht mir aber noch Sorgen/Probleme.
Erste Variante:
hierbei verhält sich das JLabel l1 so, dass es sich über seine ganze Zellenbreite ausdehnt und das TextField
ganz nach links verschiebt. So weit so gut, das ist mir einleuchtend.
Aber in diesem Code:
macht das JLabel label1 nichts. Erst, wenn ich das JTextField tf1 auf weightx/y = 1.0; und
GridBagConstraints.BOTH setze, dann verhält sich das JTextField tf1 wie sich eigentlich das
JLabel label1 verhalten sollte, über beide Richtungen.
Es ist so, als ob JLabel label1 überhaupt nicht auf weightx/y = 1.0 und GridBagConstraints.BOTH
reagieren würde, obwohl doch die Methode "addComponentLabel(....) zuerst aufgerufen wird.
Kann mir jemand sagen warum das so ist und ob es überhaupt sinnvoll ist über Methoden
die Membervariablen von GridBagConstraints zu initialisieren.
Meiner Meinung nach, wäre es schon sinnvoll, da nur immer eine Methode für z.B. fünf
Button oder 10 JTextFields geschireben werden müsste.
Danke, chris
ich habe mal zwei Dinge mit GBL ausprobiert. Eine Sache macht mir aber noch Sorgen/Probleme.
Erste Variante:
Code:
....
l1 = new JLabel("Name:");
l1.setPreferredSize(new Dimension(150,25));
l1.setHorizontalAlignment(JLabel.CENTER);
l1.setBackground(Color.red);
l1.setOpaque(true);
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.weightx = 1.0;
gbc.weighty = 1.0;
gbc.fill = GridBagConstraints.BOTH;
gb.setConstraints(l1, gbc);
panel1.add(l1);
tf1 = new JTextField();
tf1.setPreferredSize(new Dimension(120,25));
GridBagConstraints gbcTextField = new GridBagConstraints();
gbcTextField.gridx = 1;
gbcTextField.gridy = 0;
gbcTextField.gridwidth = 1;
gbcTextField.gridheight = 1;
gbcTextField.fill = GridBagConstraints.NONE;
gb.setConstraints(tf1, gbcTextField);
panel1.add(tf1);
ganz nach links verschiebt. So weit so gut, das ist mir einleuchtend.
Aber in diesem Code:
Code:
....
static void addComponentLabel(JPanel p, JLabel l, int x, int y, int width, int height,
int padx, int pady, double wx, double wy)
{
GridBagLayout gb = new GridBagLayout();
GridBagConstraints gbcLabel = new GridBagConstraints();
p.setLayout(gb);
gbcLabel.gridx = x;
gbcLabel.gridy = y;
gbcLabel.gridwidth = width;
gbcLabel.gridheight = height;
gbcLabel.ipadx = padx;
gbcLabel.ipady = pady;
gbcLabel.weightx = wx;
gbcLabel.weighty = wy;
gbcLabel.fill = GridBagConstraints.BOTH;//for example: GridBagConstraints.BOTH;
gb.setConstraints(l, gbcLabel);
p.add(l);//p --> Panel, component --> anythink Component (for example: JButton)
}//Ende der Methode
static void addComponentTextField(JPanel p, JTextField f, int x, int y, int width, int height,
int padx, int pady, double wx, double wy)
{
GridBagLayout gb = new GridBagLayout();
GridBagConstraints gbc = new GridBagConstraints();
p.setLayout(gb);
gbc.gridx = x;
gbc.gridy = y;
gbc.gridwidth = width;
gbc.gridheight = height;
gbc.ipadx = padx;
gbc.ipady = pady;
gbc.weightx = wx;
gbc.weighty = wy;
gbc.fill = GridBagConstraints.NONE;//for example: GridBagConstraints.BOTH;
gb.setConstraints(f, gbc);
p.add(f);//p --> Panel, component --> anythink Component (for example: JButton)
}
label1 = new JLabel("Name:");
label1.setHorizontalAlignment(JLabel.LEFT);
label1.setPreferredSize(new Dimension(100, 27));
label1.setOpaque(true);
label1.setBackground(Color.red);//
GridBagLayout1.addComponentLabel(panel1, label1, 0, 0, 1, 1, 0, 0, 1.0, 1.0);
tf1 = new JTextField();
tf1.setPreferredSize(new Dimension(150, 25));
GridBagLayout1.addComponentTextField(panel1, tf1, 1, 0, 1, 1, 0, 0, 0.0, 0.0);
macht das JLabel label1 nichts. Erst, wenn ich das JTextField tf1 auf weightx/y = 1.0; und
GridBagConstraints.BOTH setze, dann verhält sich das JTextField tf1 wie sich eigentlich das
JLabel label1 verhalten sollte, über beide Richtungen.
Es ist so, als ob JLabel label1 überhaupt nicht auf weightx/y = 1.0 und GridBagConstraints.BOTH
reagieren würde, obwohl doch die Methode "addComponentLabel(....) zuerst aufgerufen wird.
Kann mir jemand sagen warum das so ist und ob es überhaupt sinnvoll ist über Methoden
die Membervariablen von GridBagConstraints zu initialisieren.
Meiner Meinung nach, wäre es schon sinnvoll, da nur immer eine Methode für z.B. fünf
Button oder 10 JTextFields geschireben werden müsste.
Danke, chris