GridBagLayout einfaches Beispiel geht nicht?

Status
Nicht offen für weitere Antworten.

planetenkiller

Aktives Mitglied
Hallo

Ich habe angefangen mich in das GridBagLayout einzuarbeiten. Ich wollte ein Einfaches Beispiel machen. Im moment sieht es so aus:
Code:
-----------------------
|   1    |   2  |    3|       
|---------------------|
|   x    |        4   |
|---------------------|
|          5          |
----------------------
Überall wo es eine Zahl steht, ist ein JButton. Da wo ein x steht ist nichts(Freiraum)
Ich will aber, das die Buttons 1 + 2 einer sind. Dass heisst, Button 2 soll weg, und Button 1 zwei Breit werden.
So:
Code:
-----------------------
|         1     |    3|       
|---------------------|
|   x    |        4   |
|---------------------|
|          5          |
----------------------
Doch wenn ich Button 2 Lösche und Button 1 zwei Breit mache, nutzt der Button 4 auch immer die Ganze Breite(Wie Button 5)?
Was mache ich Falsch?

Momentaner Code:
Code:
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;

/**
 *
 * @author Roland Bär
 */
public class SimpleGridBagLayout 
{
    private JFrame mainFrame;
    
    private JButton button1;
    private JButton button2;
    private JButton button3;
    private JButton button4;
    private JButton button5;
    
    public SimpleGridBagLayout()
    {
        mainFrame = new JFrame("GridBagLayout Beispiel");
        button1 = new JButton("1");
        button2 = new JButton("2");
        button3 = new JButton("3");
        button4 = new JButton("4");
        button5 = new JButton("5");   
    }
    
    public void paint()
    {
        mainFrame.setLayout(new GridBagLayout());
        GridBagConstraints gbc;
        
        /***** Button 1 *****/
        gbc = new GridBagConstraints();
        gbc.fill = GridBagConstraints.BOTH;
        gbc.gridx = 0;
        gbc.gridy = 0;
        //gbc.gridwidth = 2;
        gbc.weightx = 1.0;
        gbc.weighty = 1.0;
        mainFrame.add(button1, gbc);
        /***** Button 1 *****/
        /***** Button 2 *****/
        gbc = new GridBagConstraints();
        gbc.fill = GridBagConstraints.BOTH;
        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.weightx = 1.0;
        gbc.weighty = 1.0;
        mainFrame.add(button2, gbc);
        /***** Button 2 *****/
        /***** Button 3 *****/
        gbc = new GridBagConstraints();
        gbc.fill = GridBagConstraints.BOTH;
        gbc.gridx = 2;
        gbc.gridy = 0;
        gbc.weightx = 1.0;
        gbc.weighty = 1.0;
        mainFrame.add(button3, gbc);
        /***** Button 3 *****/

        /***** Button 4 *****/
        gbc = new GridBagConstraints();
        gbc.fill = GridBagConstraints.BOTH;
        gbc.gridx = 1;
        gbc.gridy = 1;
        gbc.gridwidth = 2;
        gbc.weightx = 1.0;
        gbc.weighty = 1.0;
        mainFrame.add(button4, gbc);
        /***** Button 4 *****/
        
        /***** Button 5 *****/
        gbc = new GridBagConstraints();
        gbc.fill = GridBagConstraints.BOTH;
        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.gridwidth = 3;
        gbc.weightx = 1.0;
        gbc.weighty = 1.0;
        mainFrame.add(button5, gbc);
        /***** Button 5 *****/
        
        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mainFrame.setSize(300, 150);
        mainFrame.setVisible(true);
    }
}
 

André Uhres

Top Contributor
Code:
package layout;
/*
 * GridBagDemo.java
 */

import java.awt.*;
import javax.swing.*;

public class GridBagDemo extends JFrame {

    private JButton jButton1;
    private JButton jButton3;
    private JButton jButton4;
    private JButton jButton5;
    private JLabel jLabel1;
    private JLabel jLabel2;
    private JLabel jLabel3;
    private JPanel jPanel1;

    public GridBagDemo() {
        GridBagConstraints gridBagConstraints;
        jPanel1 = new JPanel();
        jButton1 = new JButton();
        jButton3 = new JButton();
        jButton4 = new JButton();
        jButton5 = new JButton();
        jLabel1 = new JLabel();
        jLabel2 = new JLabel();
        jLabel3 = new JLabel();
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        jPanel1.setLayout(new GridBagLayout());
        jButton1.setText("jButton1");
        gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 2.0;
        gridBagConstraints.weighty = 1.0;
        jPanel1.add(jButton1, gridBagConstraints);
        jButton3.setText("jButton3");
        gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        jPanel1.add(jButton3, gridBagConstraints);
        jButton4.setText("jButton4");
        gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 2.0;
        gridBagConstraints.weighty = 1.0;
        jPanel1.add(jButton4, gridBagConstraints);
        jButton5.setText("jButton5");
        gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 3.0;
        gridBagConstraints.weighty = 1.0;
        jPanel1.add(jButton5, gridBagConstraints);
        
        //Diese folgenden 3 JLabels dienen lediglich als "Schablone"
        //um festzulegen, dass wir drei Spalten haben:
        jLabel1.setPreferredSize(new Dimension(100, 0));
        gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        jPanel1.add(jLabel1, gridBagConstraints);
        jLabel2.setPreferredSize(new Dimension(100, 0));
        gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        jPanel1.add(jLabel2, gridBagConstraints);
        jLabel3.setPreferredSize(new Dimension(100, 0));
        gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        jPanel1.add(jLabel3, gridBagConstraints);
        
        
        getContentPane().add(jPanel1, BorderLayout.NORTH);
        setSize(400, 300);
        setLocationRelativeTo(null);
    }

    public static void main(String args[]) {
        new GridBagDemo().setVisible(true);
    }
}
 

planetenkiller

Aktives Mitglied
aha, ok. Nur durch die Drei JLabels werden es 3 Spalten. Geht das nicht auch ohne die drei JLabels? Bei einem Layout Manager, bei dem Komplexere Layouts möglich sind, erwarte ich, dass er selbst merkt, dass es 3 Spalten werden sollen.
 

André Uhres

Top Contributor
planetenkiller hat gesagt.:
aha, ok. Nur durch die Drei JLabels werden es 3 Spalten. Geht das nicht auch ohne die drei JLabels? Bei einem Layout Manager, bei dem Komplexere Layouts möglich sind, erwarte ich, dass er selbst merkt, dass es 3 Spalten werden sollen.
Deine Anordnung der Buttons ist schon recht ungewöhnlich.
Die brauchen dann halt ne kleine Orientierungshilfe :wink:
 

André Uhres

Top Contributor
Du gehst zwar von drei Spalten aus, aber von der Optik her sind das ja nur zwei Spalten,
denn in keiner Zeile sind mehr als zwei Spalten.
Darüber hinaus haben die beiden ersten Zeilen auch noch unterschiedliche Spaltenbreiten
und die zweite Zeile hat nur eine Komponente aber das sollen zwei Spalten sein.
Für den relativ einfachen GridBagLayout ist das schon ziemlich feste Kost.
Man muss halt im Sinn behalten, dass ein GridBagLayout kein Freedesign ist :wink:
 
Status
Nicht offen für weitere Antworten.
Ähnliche Java Themen
  Titel Forum Antworten Datum
S GridBagLayout Felder formatieren AWT, Swing, JavaFX & SWT 1
S GridBagLayout - Probleme mit Bilderanzeige AWT, Swing, JavaFX & SWT 3
C GridbagLayout verstehen lernen AWT, Swing, JavaFX & SWT 1
H GridBagLayout macht mich wahnsinnig :-( AWT, Swing, JavaFX & SWT 5
BabySuna darstellungsprobleme mit JTabbedPane und GridBagLayout AWT, Swing, JavaFX & SWT 8
CptK Positionieren von Elementen in GridBagLayout AWT, Swing, JavaFX & SWT 4
A Probleme mit gridheight (GridBagLayout) AWT, Swing, JavaFX & SWT 6
Mario1409 AWT GridBagLayout AWT, Swing, JavaFX & SWT 5
J LayoutManager GridBagLayout, probleme mit Anordnung von Objekten AWT, Swing, JavaFX & SWT 6
DaCrazyJavaExpert Swing Komponenten in GridBagLayout werden Falsch angeordnet AWT, Swing, JavaFX & SWT 1
T LayoutManager Anordnen der Elemente im GridBagLayout AWT, Swing, JavaFX & SWT 11
K GridBagLayout mit reponsive Design AWT, Swing, JavaFX & SWT 2
K GridBagLayout verändert die größe? AWT, Swing, JavaFX & SWT 1
D Swing Größe einer JComboBox im GridBagLayout aufgrund der maximalen Länge der enthaltenen Daten AWT, Swing, JavaFX & SWT 7
B LayoutManager GridBagLayout und JScrollPane AWT, Swing, JavaFX & SWT 5
T GridBagLayout Anfängerprobleme AWT, Swing, JavaFX & SWT 3
Sin137 LayoutManager GridBagLayout Probleme AWT, Swing, JavaFX & SWT 6
L GridBagLayout Anordnung AWT, Swing, JavaFX & SWT 3
M Gridbaglayout Spaltenbreite AWT, Swing, JavaFX & SWT 3
M LayoutManager GridBagLayout passt seine größe nicht an AWT, Swing, JavaFX & SWT 3
V GridBagLayout AWT, Swing, JavaFX & SWT 4
N LayoutManager GridBagLayout - Grundlagen AWT, Swing, JavaFX & SWT 6
Neumi5694 Swing Gridbaglayout - automatische Anpassung verhindern AWT, Swing, JavaFX & SWT 1
P AWT Problem mit Platzierung (GridBagLayout) AWT, Swing, JavaFX & SWT 2
F Breite beim GridBagLayout festlegen AWT, Swing, JavaFX & SWT 2
M Swing GridBagLayout Komponentengröße festsetzen AWT, Swing, JavaFX & SWT 1
J GridBagLayout mit Hilfe einer For-Schleife befüllen AWT, Swing, JavaFX & SWT 1
W GridBagLayout Größe geben AWT, Swing, JavaFX & SWT 1
HarleyDavidson Swing Seltsames Verhalten GridBagLayout AWT, Swing, JavaFX & SWT 11
W GridBagLayout mit fester Zellgrösse AWT, Swing, JavaFX & SWT 2
N Swing GridBagLayout: Ein Pixel Versatz AWT, Swing, JavaFX & SWT 2
B Swing Gridbaglayout unterschiedliche Zeilenhöhe AWT, Swing, JavaFX & SWT 6
H LayoutManager GridBagLayout AWT, Swing, JavaFX & SWT 1
N GridBagLayout - was fehlt? AWT, Swing, JavaFX & SWT 8
S Swing rowHeight und rowWeight im GridBagLayout AWT, Swing, JavaFX & SWT 1
N Swing GUI mit GridBagLayout AWT, Swing, JavaFX & SWT 4
A jpanel mit gridbaglayout auf hintergrundbild AWT, Swing, JavaFX & SWT 7
S GridBagLayout-Frage AWT, Swing, JavaFX & SWT 1
G GridBagLayout AWT, Swing, JavaFX & SWT 6
S GridBagLayout "links-rechts-layouten" AWT, Swing, JavaFX & SWT 7
T LayoutManager GridBagLayout / erwartetes Raster fehlt AWT, Swing, JavaFX & SWT 3
X Gridbaglayout gridx + gridy auslesen? AWT, Swing, JavaFX & SWT 7
H GridBagLayout macht Probleme... AWT, Swing, JavaFX & SWT 4
N GridBagLayout - Zeitplan AWT, Swing, JavaFX & SWT 13
N Swing GridbagLayout AWT, Swing, JavaFX & SWT 4
S Swing gridbaglayout AWT, Swing, JavaFX & SWT 8
G GridBagLayout Problem AWT, Swing, JavaFX & SWT 4
Java-Insel LayoutManager Ein GridBagLayout-Objekt für mehrere Panels? AWT, Swing, JavaFX & SWT 2
X LayoutManager gridBagLayout wird nicht richtig Dargestellt AWT, Swing, JavaFX & SWT 5
das-mo Probleme mit GridBagLayout AWT, Swing, JavaFX & SWT 6
T LayoutManager GridBagLayout - zwei jTable mit unterschiedlicher Höhe AWT, Swing, JavaFX & SWT 2
N LayoutManager GridBagLayout schummeln erlaubt ? AWT, Swing, JavaFX & SWT 2
D GridBagLayout AWT, Swing, JavaFX & SWT 9
A Swing GridBagLayout - constraints.anchor scheint nicht korrekt zu funktionieren? AWT, Swing, JavaFX & SWT 7
J Swing Terminkalender Wochenansicht mit Gridbaglayout oder JTable AWT, Swing, JavaFX & SWT 16
C LayoutManager GridBagLayout - Anfängerfrage AWT, Swing, JavaFX & SWT 5
Asamandra LayoutManager GridBagLayout - Komponenten (mit fill?) vergrößern aber Proportionen dabei erhalten? AWT, Swing, JavaFX & SWT 3
R GridBagLayout in GridBagLayout AWT, Swing, JavaFX & SWT 2
H Positionierungsprobleme beim GridBagLayout AWT, Swing, JavaFX & SWT 16
Furtano AWT GridBagLayout macht mir Sorgen AWT, Swing, JavaFX & SWT 3
A GridbagLayout positionierungsproblem AWT, Swing, JavaFX & SWT 4
earlgrey_tea GridBagLayout Componenten proportional vergößern AWT, Swing, JavaFX & SWT 12
D JTable im GridBagLayout -> gridwidth AWT, Swing, JavaFX & SWT 6
T GridBagLayout Problem AWT, Swing, JavaFX & SWT 3
D Probleme mit GridBagLayout AWT, Swing, JavaFX & SWT 8
J Swing GridBagLayout: Links-nach-rechts Orientierung statt zentriert AWT, Swing, JavaFX & SWT 12
R Größe eines Labels bei GridBagLayout festlegen AWT, Swing, JavaFX & SWT 9
B GridBagLayout Problem AWT, Swing, JavaFX & SWT 3
M LayoutManager GridBagLayout AWT, Swing, JavaFX & SWT 11
E LayoutManager GridBagLayout in BorderLayout - Abstand bei Resizing AWT, Swing, JavaFX & SWT 2
Y LayoutManager Keine vollständige Darstellung der Tabelle mit GridBagLayout AWT, Swing, JavaFX & SWT 3
L LayoutManager GridBagLayout leere Zeilen AWT, Swing, JavaFX & SWT 4
H LayoutManager Layout mit GridBagLayout machbar? AWT, Swing, JavaFX & SWT 6
N GridBagLayout Problem AWT, Swing, JavaFX & SWT 6
C Swing JTable "zerstört" GridBagLayout AWT, Swing, JavaFX & SWT 9
N LayoutManager GridBagLayout Größe fixieren AWT, Swing, JavaFX & SWT 3
M GridBagLayout AWT, Swing, JavaFX & SWT 7
V Swing Gridbaglayout Leeres Fenster AWT, Swing, JavaFX & SWT 2
R LayoutManager GridBagLayout Fragen AWT, Swing, JavaFX & SWT 10
P LayoutManager Verständnis-Frage GridBagLayout AWT, Swing, JavaFX & SWT 7
M LayoutManager Einige Fragen zum GridBagLayout AWT, Swing, JavaFX & SWT 13
N GridBagLayout AWT, Swing, JavaFX & SWT 11
D Swing Problem mit Gridbaglayout bzw. Größenanpassung JPanels AWT, Swing, JavaFX & SWT 7
Y Swing GridbagLayout JTextfield zu klein AWT, Swing, JavaFX & SWT 5
L LayoutManager GridBagLayout spielt verrückt AWT, Swing, JavaFX & SWT 9
T LayoutManager GridBagLayout an Fenstergröße anpassen AWT, Swing, JavaFX & SWT 2
J Java GUI mit GridBagLayout AWT, Swing, JavaFX & SWT 3
Y LayoutManager Problem mit Gridbaglayout AWT, Swing, JavaFX & SWT 8
hdi LayoutManager GridBagLayout AWT, Swing, JavaFX & SWT 9
W GridBagLayout Falsche Größenanpassung AWT, Swing, JavaFX & SWT 6
R Swing Button-Größe in JPanel mit GridBagLayout nicht änderbar AWT, Swing, JavaFX & SWT 3
E Problem mit meiner GUI/GridbagLayout AWT, Swing, JavaFX & SWT 2
M LayoutManager GradientPaint auf GridBagLayout AWT, Swing, JavaFX & SWT 5
D LayoutManager GridBagLayout, Änderung zur Laufzeit AWT, Swing, JavaFX & SWT 4
G LayoutManager per Button GridBagLayout + Inhalt ändern AWT, Swing, JavaFX & SWT 2
M GridBagLayout zeilenweise füllen AWT, Swing, JavaFX & SWT 5
B Anzeigefehler in GridBagLayout durch paintComponent(Graphics g) AWT, Swing, JavaFX & SWT 3
hdi Swing Problem mit GridBagLayout AWT, Swing, JavaFX & SWT 2
E LayoutManager GridBagLayout kurz vorm Wahnsinn! AWT, Swing, JavaFX & SWT 22
P Swing GridBagLayout bleibt nicht so wie es ist :( AWT, Swing, JavaFX & SWT 8

Ähnliche Java Themen

Neue Themen


Oben