GridBagLayout Überlegung

Status
Nicht offen für weitere Antworten.

8ull23y3

Bekanntes Mitglied
gbl_thinking.png


So, das da oben hatte ich mir überlegt damit ich mal langsam das GridBagLayout in meinen Kopp krich.

Soweit so gut.

Der Code sieht folgendermaßen aus.

Code:
import java.awt.*;

import javax.swing.*;

public class GBL extends JFrame {
  private GridBagLayout gbl;
  public GBL() {
    this.setDefaultCloseOperation(3);
    this.setSize(400,300);
    gbl = new GridBagLayout();
    this.setLayout(gbl);
    
    addComponent(this, gbl, new newmenubar(),   0,0,5,2,10.0,100.0);
    addComponent(this, gbl, new newtoolbar(),   0,2,5,2,10.0,100.0);
    addComponent(this, gbl, new newtextarea(),  0,4,4,9,10.0,100.0);
    addComponent(this, gbl, new newpanel(),     4,4,1,9,10.0,100.0);
    addComponent(this, gbl, new newtextfield(), 0,13,5,2,10.0,100.0);

    this.setVisible(true);
  }
  private void addComponent(Container container,
                            GridBagLayout gbl,
                            Component component,
                            int x, int y,
                            int width, int height,
                            double weightx, double weighty)
  {
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.fill = gbc.BOTH;
    gbc.gridx = x; gbc.gridy = y;
    gbc.gridwidth = width; gbc.gridheight = height;
    gbc.weightx = weightx; gbc.weighty = weighty;
    gbl.setConstraints(component, gbc);
    container.add(component);
  }
  private class newmenubar extends JMenuBar {
    private newmenubar() {
      JMenu menu = new JMenu("Datei");
      JMenuItem menuitem = new JMenuItem("Beenden");
      menu.add(menuitem);
      this.add(menu);
    }
  }
  private class newtoolbar extends JToolBar {
    private newtoolbar() {
    
    }
  }
  private class newtextarea extends JTextArea {
    private newtextarea() {

    }
  }
  private class newpanel extends JPanel {
    private newpanel() {
    
    }
  }
  private class newtextfield extends JTextField {
    private newtextfield() {

    }
  }
  public static void main(String[] args) {
    new GBL();
  }
}

Ist nur Testweise zum verstehn. ;)

Mein Problem dass das im Endeffekt nicht so aussieht wie ich es gern hätte sondern genauso aussieht wie beim GridLayout. Was hab ich falsch gemacht?
 
B

Beni

Gast
Die relative Breite/Höhe wird über "weightx" und "weighty" gesteuert, nicht über "gridwidth/height". Also musst du bei denen zwei unterschiedliche Werte eintragen.

Und: wenn du die Variable "fill" für einzelne Components nicht auf BOTH, sondern z.B. auf HORIZONTAL stellst, dann wird deine Oberfläche auch schon weniger wie GridLayout aussehen.
 

8ull23y3

Bekanntes Mitglied
Code:
import java.awt.*;

import javax.swing.*;

public class GBL extends JFrame {
  private GridBagLayout gbl;
  public GBL() {
    this.setDefaultCloseOperation(3);
    this.setSize(400,300);
    gbl = new GridBagLayout();
    this.setLayout(gbl);

    addComponent(this, gbl, new JButton(),   0,0,5,2,10.0,10.0);
    addComponent(this, gbl, new JButton(),   0,2,5,2,10.0,10.0);
    addComponent(this, gbl, new JButton(),  0,4,4,9,10.0,10.0);
    addComponent(this, gbl, new JButton(),     4,4,1,9,10.0,10.0);
    addComponent(this, gbl, new JButton(), 0,13,5,2,10.0,10.0);

    this.setVisible(true);
  }
  private void addComponent(Container container,
                            GridBagLayout gbl,
                            Component component,
                            int x, int y,
                            int width, int height,
                            double weightx, double weighty)
  {
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.fill = gbc.BOTH;
    gbc.gridx = x; gbc.gridy = y;
    gbc.gridwidth = width; gbc.gridheight = height;
    gbc.weightx = weightx; gbc.weighty = weighty;
    gbl.setConstraints(component, gbc);
    container.add(component);
  }
  public static void main(String[] args) {
    new GBL();
  }
}

Die AUfteilung ist ja schon richtig aber das ist alles naja weiss nicht wie ichs sagen soll, nicht das was ich will
 

8ull23y3

Bekanntes Mitglied
Code:
import java.awt.*;

import javax.swing.*;

public class GBL extends JFrame {
  JButton button;
  public GBL() {
    this.setDefaultCloseOperation(3);
    this.setSize(400,300);
    this.setLayout(new GridBagLayout());

    GridBagConstraints gbc;

    JTextArea jtextarea = new JTextArea();
    gbc = makegbc(0,0,2,1);
    gbc.weightx = 1.0;
    gbc.weighty = 1.0;
    gbc.fill = GridBagConstraints.BOTH;
    this.add(new JScrollPane(jtextarea), gbc);
    
    JPanel jpanel = new JPanel();
    jpanel.setBackground(Color.DARK_GRAY);
    gbc = makegbc(3,0,2,1);
    gbc.weightx = 0.5;
    gbc.weighty = 0.5;
    gbc.fill = GridBagConstraints.BOTH;
    this.add(jpanel, gbc);

    JTextField jtextfield = new JTextField("\"Hier werden Statusmeldeungen ausgegeben\"");
    jtextfield.setEditable(false);
    gbc = makegbc(0,3,3,1);
    gbc.fill = GridBagConstraints.HORIZONTAL;
    this.add(jtextfield, gbc);

    this.setVisible(true);
  }
  public GridBagConstraints makegbc(int x, int y, int width, int height) {
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.gridx = x;
    gbc.gridy = y;
    gbc.gridwidth = width;
    gbc.gridheight = height;
    return gbc;
  }
  public static void main(String[] args) {
    new GBL();
  }
}

So, ich hab jetzt was gefunden womit ich mich abfinden kann und so langsam dämmert es mir wofür was zuständig ist. Mein Frage ist nu wieso wird mein TextFeld nicht bis zum Ende meines Frames gezogen?

Ansonsten bin ich schon recht zufrieden.

THX
 
B

Beni

Gast
So besser? Die beiden Componenten oben hatte insgesammt eine Breite von 4, das Textfeld nur 3.

Ach, und wenn du jemals das Gefühl hast, du hättest die richtigen Einstellungen gemacht, sie werden aber nicht umgesetzt: das GBL hat erwiesenermassen Bugs... (die werden wegen der Rückwärtskompatibilität nicht entfernt, sonst würden Progis die aufgrund eines Fehlers funktionieren, nicht mehr laufen :roll:)

Code:
public class GBL extends JFrame {
	  JButton button;
	  public GBL() {
	    this.setDefaultCloseOperation(3);
	    this.setSize(400,300);
	    this.setLayout(new GridBagLayout());

	    GridBagConstraints gbc;

	    JTextArea jtextarea = new JTextArea();
	    gbc = makegbc(0,0,1,1);
	    gbc.weightx = 1.0;
	    gbc.weighty = 1.0;
	    gbc.fill = GridBagConstraints.BOTH;
	    this.add(new JScrollPane(jtextarea), gbc);
	   
	    JPanel jpanel = new JPanel();
	    jpanel.setBackground(Color.DARK_GRAY);
	    gbc = makegbc(1,0,1,1);
	    gbc.weightx = 0.5;
	    gbc.weighty = 0.5;
	    gbc.fill = GridBagConstraints.BOTH;
	    this.add(jpanel, gbc);

	    JTextField jtextfield = new JTextField("\"Hier werden Statusmeldeungen ausgegeben\"");
	    jtextfield.setEditable(false);
	    gbc = makegbc(0,1,2,1);
	    gbc.fill = GridBagConstraints.HORIZONTAL;
	    this.add(jtextfield, gbc);

	    this.setVisible(true);
	  }
	  public GridBagConstraints makegbc(int x, int y, int width, int height) {
	    GridBagConstraints gbc = new GridBagConstraints();
	    gbc.gridx = x;
	    gbc.gridy = y;
	    gbc.gridwidth = width;
	    gbc.gridheight = height;
	    return gbc;
	  }
	  public static void main(String[] args) {
	    new GBL();
	  }
	}
 
Status
Nicht offen für weitere Antworten.

Ähnliche Java Themen

Neue Themen


Oben