Probleme mit dem GridBagLayout

schnuffie

Erfahrenes Mitglied
Hallo Leute,

nachdem ich nun schon Verschiedenes probiert habe, weiß ich leider nicht mehr weiter.
Mein Layout soll sich der Fenstergröße proportional anpassen, daher ein Layout-Manager.
Obere Zeile 1. Viertel Beschreibung, Rest: Eingabefeld, genauso auch die zweite Zeile, das klappt auch. Der schwierige Teil ist jetzt: Im ersten Viertel soll oben "Text", darunter "Zeichenanzahl" und darunter ein Eingabefeld sein. Der restliche Platz soll komplett mit einem Textarea gefüllt sein (siehe auch Quellcode). Darunter kommt noch ein Button, das klappt auch. Leider bekomme ich den Teil mit dem Textarea nicht hin. Was mache ich falsch?
:confused:

Code:
  public void init() {
	GridBagLayout gb = new GridBagLayout();
	GridBagConstraints gbc = new GridBagConstraints();
	gbc.fill = GridBagConstraints.BOTH;
	gbc.weightx = 1.0;
	gbc.weighty = 1.0;
	gbc.gridwidth = 1;
	this.add(new Label("int:"), gb, gbc);
	gbc.weightx = 3.0;
	gbc.weighty = 1.0;
	gbc.gridwidth = GridBagConstraints.REMAINDER;
	this.tfInt = new TextField();
	this.add(tfInt, gb, gbc);
	gbc.weightx = 1.0;
	gbc.weighty = 1.0;
	gbc.gridwidth = 1;
	this.add(new Label("long:"), gb, gbc);
	gbc.weightx = 3.0;
	gbc.weighty = 1.0;
	gbc.gridwidth = GridBagConstraints.REMAINDER;
	this.tfLong = new TextField();
	this.add(tfLong, gb, gbc);
	gbc.weightx = 1.0;
	gbc.weighty = 1.0;
	gbc.gridwidth = 1;
	this.add(new Label("Text:"), gb, gbc);
	gbc.weightx = 3.0;
	gbc.weighty = 3.0;
	gbc.gridheight = 3;
	gbc.gridwidth = GridBagConstraints.REMAINDER;
	this.taText = new TextArea();
	this.add(taText, gb, gbc);
	gbc.weightx = 1.0;
	gbc.weighty = 1.0;
	gbc.gridwidth  = GridBagConstraints.REMAINDER;
	gbc.gridheight = 1;
	this.add(new Label("Zeichenanzahl:"), gb, gbc);
	gbc.weightx = 1.0;
	gbc.weighty = 1.0;
	gbc.gridwidth = GridBagConstraints.REMAINDER;
	this.tfTextLength = new TextField();
	this.add(tfTextLength, gb, gbc);
	gbc.weightx = 4.0;
	gbc.weighty = 1.0;
	gbc.gridwidth = GridBagConstraints.REMAINDER;
	this.bRandom = new Button("Generieren");
	bRandom.addActionListener(this);
	this.add(bRandom, gb, gbc);	
	this.setLayout(gb);
  }
 
  private void add(Component c, GridBagLayout gb, GridBagConstraints gbc) {
	gb.setConstraints(c, gbc);
	this.add(c);
  }
 
Hallo Schnuffie,

ich habe deinen Code etwas verunstaltet und dein Layout so verändert wie ich es von deiner Beschreibung verstanden habe.

Code:
import java.awt.*;
import javax.swing.JApplet;

public class Test extends JApplet {
	
	TextField tfInt, tfLong, tfTextLength;
	TextArea taText;
	Button bRandom;
	
	  public void init() {
		setLayout(new GridBagLayout());
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.fill = GridBagConstraints.BOTH;
		gbc.weightx = 1;
		gbc.weighty = 1;
		gbc.gridwidth = 1;
		gbc.gridheight = 1;
		gbc.gridx = 0;
		gbc.gridy = 0;
		add(new Label("int:"), gbc);
		gbc.weightx = 3;
		gbc.gridx = 1;
		gbc.gridwidth = 3;
		tfInt = new TextField();
		add(tfInt,  gbc);
		gbc.weightx = 1;
		gbc.gridwidth = 1;
		gbc.gridx = 0;
		gbc.gridy = 1;
		add(new Label("long:"), gbc);
		gbc.weightx = 3;
		gbc.gridx = 1;
		gbc.gridwidth = 4;
		tfLong = new TextField();
		add(tfLong, gbc);
		gbc.weightx = 1;
		gbc.gridwidth = 1;
		gbc.gridx = 0;
		gbc.gridy = 2;
		add(new Label("Text:"),  gbc);
		gbc.gridy = 3;
		add(new Label("Zeichenanzahl:"), gbc);
		gbc.gridy = 4;
		tfTextLength = new TextField();
		add(tfTextLength,  gbc);
		gbc.weightx = 3;
		gbc.gridheight = 3;
		gbc.gridwidth = 3;
		gbc.gridx = 1;
		gbc.gridy = 2;
		taText = new TextArea();
		add(taText, gbc);
		gbc.weightx = 4;
		gbc.gridwidth = 4;
		gbc.gridx = 0;
		gbc.gridy = 5;
		bRandom = new Button("Generieren");
		add(bRandom,  gbc);
	  }
	
}

Wenn es doch anders aussehen sollte, könntest du ja vielleicht eine kleine Skizze uploaden?

Vg Erdal
 
flashray hat gesagt.:
Hallo Schnuffie,
Wenn es doch anders aussehen sollte, könntest du ja vielleicht eine kleine Skizze uploaden?

Vg Erdal

Das wäre in der Tat hilfreich.
Ein kleiner Tipp noch zum GBL:
Es ist oftmals sinnvoll, bei vielen Komponenten mehrere Panel wie Zeilen zu verwenden und auf diesen einige Komponenten zu platzieren. Und hin und wieder erfüllen Dummy-Panel und Dummy-Label ihren Zweck, um andere Komponenten in Ecken oder Seiten auszurichten.
 
Hallo!

Also ich wuerde nicht empfehlen unnoetig viele JPanels zu verwenden... es gibt ja Gott sei dank neben GridBagLayout noch viele andere LayoutManager... wie beispielsweise das huebsche SpringLayout ;-)

Gruss Tom
 
Thomas Darimont hat gesagt.:
Hallo!
Also ich wuerde nicht empfehlen unnoetig viele JPanels zu verwenden...

Warum nicht?

Thomas Darimont hat gesagt.:
es gibt ja Gott sei dank neben GridBagLayout noch viele andere LayoutManager... wie beispielsweise das huebsche SpringLayout ;-)

Gruss Tom

Die Beispiele im Sun Tutorial sehen aber etwas spartanisch aus. Ist das genau so mächtig wie das GBL?
 
Danke Euch allen.

Mit den x-/y-Angaben funktioniert's jetzt. :)

Code:
import java.applet.Applet;
import java.awt.*;
import java.util.Random;
 
public class RandomApplet extends Applet implements ActionListener {
  
  private TextField tfInt;
  private TextField tfLong;
  private TextArea taText;
  private TextField tfTextLength;
  private Checkbox cbOnlySpace;
  private Checkbox cbWithNumeric;
  private Button bRandom;
  private Random r;
  
  public void init() {
    GridBagLayout gb = new GridBagLayout();
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.ipadx = 5;
    gbc.ipady = 1;
    gbc.weightx = 1.0;
    gbc.weighty = 1.0;
    gbc.gridwidth = 1;
    gbc.gridheight = 1;
    gbc.gridx = 0;
    gbc.gridy = 0;
    this.add(new Label("int:"), gb, gbc);
    gbc.weightx = 3.0;
    gbc.weighty = 1.0;
    gbc.gridwidth = GridBagConstraints.REMAINDER;
    gbc.gridheight = 1;
    gbc.gridx = 1;
    gbc.gridy = 0;
    this.tfInt = new TextField(60);
    this.add(tfInt, gb, gbc);
    gbc.weightx = 1.0;
    gbc.weighty = 1.0;
    gbc.gridwidth = 1;
    gbc.gridheight = 1;
    gbc.gridx = 0;
    gbc.gridy = 1;
    this.add(new Label("long:"), gb, gbc);
    gbc.weightx = 3.0;
    gbc.weighty = 1.0;
    gbc.gridwidth = GridBagConstraints.REMAINDER;
    gbc.gridheight = 1;
    gbc.gridx = 1;
    gbc.gridy = 1;
    this.tfLong = new TextField(60);
    this.add(tfLong, gb, gbc);
    gbc.weightx = 1.0;
    gbc.weighty = 1.0;
    gbc.gridwidth = 1;
    gbc.gridheight = 1;
    gbc.gridx = 0;
    gbc.gridy = 2;
    this.add(new Label("Text:"), gb, gbc);
    this.tfTextLength = new TextField(6);
    Panel p = new Panel(new FlowLayout(FlowLayout.LEFT, 0, 0));
    p.add(tfTextLength);
    p.add(new Label("Zeichen"));
    gbc.weightx = 1.0;
    gbc.weighty = 1.0;
    gbc.gridwidth  = 1;
    gbc.gridheight = 1;
    gbc.gridx = 0;
    gbc.gridy = 3;
    this.add(p, gb, gbc);
    gbc.weightx = 1.0;
    gbc.weighty = 1.0;
    gbc.gridwidth = 1;
    gbc.gridheight = 1;
    gbc.gridx = 0;
    gbc.gridy = 4;
    this.cbWithNumeric = new Checkbox("mit Ziffern");
    this.add(cbWithNumeric, gb, gbc);
    gbc.weightx = 1.0;
    gbc.weighty = 1.0;
    gbc.gridwidth = 1;
    gbc.gridheight = 1;
    gbc.gridx = 0;
    gbc.gridy = 5;
    this.cbOnlySpace = new Checkbox("ohne Leerzeichen");
    this.add(cbOnlySpace, gb, gbc);
    gbc.weightx = 3.0;
    gbc.weighty = 1.0;
    gbc.gridwidth = GridBagConstraints.REMAINDER;
    gbc.gridheight = 4;
    gbc.gridx = 1;
    gbc.gridy = 2;
    this.taText = new TextArea("", 6, 60, TextArea.SCROLLBARS_VERTICAL_ONLY);
    this.add(taText, gb, gbc);
    gbc.fill = GridBagConstraints.NONE;
    gbc.weightx = 4.0;
    gbc.weighty = 1.0;
    gbc.gridwidth = GridBagConstraints.REMAINDER;
    gbc.gridheight = GridBagConstraints.REMAINDER;
    gbc.gridx = 0;
    gbc.gridy = 6;
    this.bRandom = new Button("Generieren");
    bRandom.addActionListener(this);
    this.add(bRandom, gb, gbc); 
    this.setLayout(gb);
  }
  
  public void start() {
    this.r = new Random();
  }
  
  private void add(Component c, GridBagLayout gb, GridBagConstraints gbc) {
    gb.setConstraints(c, gbc);
    this.add(c);
  }
  public void actionPerformed(ActionEvent ae) {
    tfInt.setText(String.valueOf(Math.abs(r.nextInt())));
    tfLong.setText(String.valueOf(Math.abs(r.nextLong())));
    try {
      int max = Integer.parseInt(tfTextLength.getText());
      StringBuffer sb = new StringBuffer();
      while (sb.length() < max) {
        sb.append((char)Math.abs(r.nextInt(Byte.MAX_VALUE)));
      }
      taText.setText(sb.toString());
    } catch (Exception e) {
      taText.setText("");
    }
  }
}
 
Moin,
interessant. So weit ich weiß, liegen weight-Werte zwischen 0 und 1. Was passiert, wenn Du

gbc.weightx = 3.0;

änderst auf

gbc.weightx = 1.0;

?
 
Hallo Snape,

Vom SunTutorial: (Siehe ab "Larger numbers...")

"Generally weights are specified with 0.0 and 1.0 as the extremes: the numbers in between are used as necessary. Larger numbers indicate that the component's row or column should get more space. For each column, the weight is related to the highest weightx specified for a component within that column, with each multicolumn component's weight being split somehow between the columns the component is in. Similarly, each row's weight is related to the highest weighty specified for a component within that row. Extra space tends to go toward the rightmost column and bottom row."

Vg Erdal
 
Beispielhaft:

Fall 1:
Spalte 1
gbc.weightx = 0.0;
Spalte 2
gbc.weightx = 1.0;
Spalte 2 bekommt den restlichen Platz vollständig zugewiesen

Fall 2:
Spalte 1
gbc.weightx = 1.0;
Spalte 2
gbc.weightx = 1.0;
Spalte 1 und 2 bekommen den gleichen Anteil vom Rest zugeteilt

Fall 3:
Spalte 1
gbc.weightx = 1.0;
Spalte 2
gbc.weightx = 3.0;
Spalte 2 bekommt verhältnismäßig 3 mal soviel wie Spalte 1 vom restlichen Platz.

Vg Erdal
 
Hallo,

Zitat von Thomas Darimont
Hallo!
Also ich wuerde nicht empfehlen unnoetig viele JPanels zu verwenden...

...
Warum nicht?
Weil man zum einen dadurch den Quellcode extremst unleserlich macht, die Swing Anwendung unnoetig aufblaeht. Um zu verstehen was ich meine muss man sich einfach nur die obigen Beispiele des GridBagLayouts ansehen...

Zitat:
Zitat von Thomas Darimont
es gibt ja Gott sei dank neben GridBagLayout noch viele andere LayoutManager... wie beispielsweise das huebsche SpringLayout ;-)

Gruss Tom


Die Beispiele im Sun Tutorial sehen aber etwas spartanisch aus. Ist das genau so mächtig wie das GBL?
Ja, IMHO kann man mit dem SpringLayout genausoviel machen wie mit dem GridBagLayout.

Gruss Tom
 
Zurück