GridBagLayout HILFEEE

Status
Nicht offen für weitere Antworten.

Tobias_H

Mitglied
Topic is selbsterklärend :roll:

Ich möchte diveres Komponenten über das GridBagLayout in meiner Pane anordnen, aber aus irgendwelchen Gründen wird alles was ich hinzufügen in die Mitte gesetzt.

mit .GridBagConstrain.NORTH o.ä. kann ich die Komponenten auch nich dazu bewegen sich nördlich zu orientieren.

Als Koordinaten hab ich beim ersten Komponenten gridx=0; und gridy = 0;

Der Komponent sollte doch jetzt eigenlch in der oberen linke Ecke sitzen, oder? Tut er aber nicht, er landet in der Mitte.

Wenn mir jemand einen rat geben kann wie ich das GridBagLayout zähmen kann wäre ich dankbar, es kostet mich wirklich reichtlich nerven.

Danke im Vorraus

Tobias
 
G

Guest

Gast
Füge in die untere rechte Ecke eine Box hinzu und lass das Ding den
ganzen ungenützten Platz einnehmen.

z.B. (### ist die Spalte und Zeile unten rechts):
Code:
container.add(Box.createGlue(), new GridBagConstraints(###,###, 1, 1, 1.0, 1.0, GridBagConstraints.NORTHWEST, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
 

Tobias_H

Mitglied
mic_checker hat gesagt.:
Poste doch bitte den kompletten Code damit man genau sieht wie du die Constraints setzt etc.

Zum Sourcecode: Ich will einen Mini-Browser implementieren. in der oberen Zeile soll eine Zeile sein, das JTextField, wo man seine URL eingibt.

Darunter eine Reihe mit Buttons, unter dieser Reihe dann das eigentlich Fenster das den seiten inhalt darstellt(die JEditorPane).

Das ist mein Code:

Code:
public Fenster(URL url) {
		frame = new JFrame();
		textfeld = new JTextField();
		editor = new JEditorPane();
		pane = new Container();
		menuBar = new MenuBar();	
		startseite = new JButton("Startseite");
				
		
		JMenuBar mb = menuBar.erzeugeMenuBar();
		frame.setJMenuBar(mb);
		
		scroll = new JScrollPane(editor);
		frame.add(scroll);
			
		pane = frame.getContentPane();
		GridBagConstraints c = new GridBagConstraints();
		pane.setLayout(new GridBagLayout());
		
		c.gridx = 0;
		c.gridy = 0;
		c.gridwidth = 1;
		c.gridheight = 1;
		c.weightx = 0;
		c.weighty = 0;
		c.fill = GridBagConstraints.HORIZONTAL;
		c.anchor = GridBagConstraints.NORTH;
		
		pane.add(textfeld, c);
		
		c.gridx = 0;
		c.gridy = 1;
		c.gridwidth = 1;
		c.gridheight = 1;
		c.weightx = 0;
		c.weighty = 0;
		c.fill = GridBagConstraints.NONE;
		c.anchor = GridBagConstraints.SOUTH;
		
		pane.add(editor, c);
		
		
		JMenuBar mb = menuBar.erzeugeMenuBar();
		frame.setJMenuBar(mb);
		
		scroll = new JScrollPane(editor);
		frame.add(scroll);
				
		try {
			editor.setEditorKit(kit);
			editor.setContentType("text/html");
			editor.setPage(url);
			editor.setEditable(false);
			System.out.println(url);
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		
		editor.addHyperlinkListener(new HyperLinkLauscher());
		textfeld.addKeyListener(new TastaturListener(textfeld));
		System.out.println(textfeld.getText());
				
		
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setBounds(500,500,500,500);
		frame.setLocation(200,200);
		frame.setVisible(true);

@Gast
Kann ich es nicht so einrichten das es gar nicht erst ungenutzten Platz gibt?

DAs BorderLayout hat leider seine Grenzen erreicht, ich kann damit zwar Textfeld und Pane darstellen, aber zusätliche Buttons kriege ich damit nicht unter gebracht.
 

semi

Top Contributor
Du musst beim Layout zulassen, dass die Komponenten, die den ganzen Platz
einnehmen sollen, vergrößert werden.

c.weightx = 1.0f;
c.weighty = 1.0f;

Am besten verwende etwas andere Notation für die Constraints, ist übersichtlicher.
Hier ein Beispiel
Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class BrowserWindow extends JFrame {
  private JButton buttonA = new JButton("A");
  private JButton buttonB = new JButton("B");
  private JButton buttonC = new JButton("C");
  private JTextField textField = new JTextField();
  private JTextPane textPane = new JTextPane();

  public BrowserWindow() {
    super("Noch ein Browser");
    initUI();
  }

  private void initUI() {
    Insets buttonMargin = new Insets(2, 2, 2, 2);
    Dimension buttonSize = new Dimension(25, 25);
    buttonA.setMargin(buttonMargin);
    buttonA.setPreferredSize(buttonSize);
    buttonB.setMargin(buttonMargin);
    buttonB.setPreferredSize(buttonSize);
    buttonC.setMargin(buttonMargin);
    buttonC.setPreferredSize(buttonSize);
    JScrollPane scrollPane = new JScrollPane();
    scrollPane.getViewport().add(textPane, null);

    Container c = this.getContentPane();
    c.setLayout(new GridBagLayout());

    c.add(buttonA,    new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
    c.add(buttonB,    new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0, GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
    c.add(buttonC,    new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0, GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
    c.add(textField,  new GridBagConstraints(0, 1, 4, 1, 0.0, 0.0, GridBagConstraints.NORTHWEST, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
    c.add(scrollPane, new GridBagConstraints(0, 3, 4, 1, 1.0, 1.0, GridBagConstraints.NORTHWEST, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
  }
  
  public static void main(String argv[]) {
    try {
      final BrowserWindow w = new BrowserWindow();
      w.addWindowListener(
        new WindowAdapter() {
          public void windowClosing(WindowEvent e) {
            w.setVisible(false);
            System.exit(0);
          }
        }
      );
      w.setSize(1024, 768);
      w.setLocationRelativeTo(null);
      w.setVisible(true);
    }
    catch(Exception e) {
      e.printStackTrace();
    }
  }
}
 

Tobias_H

Mitglied
Danke, hab versucht deine Ratschlage bei mir ein zu bauen, aber mit doch eher weniger Erfolg.

Das hier ist mein jetziger Code der Hauptklasse, hab mal die ganzen Listener entfernt, man kann ihn jetzt so comilieren.

Das Resultat was man bekommt ist ein komischer zusammen geknüddelter haufen von irgendwas. Zieht man den Rahme was größer popt die JEditorPane auf einmal auf, zieht man den rahmen noch größer verschwindet sie wieder.

Ich verstehe nicht, was ich so grundlegend an meinem Code falsch hab.....
Code:
public class Fenster {
	JFrame frame;
	JTextField textfeld;
	Container pane;
	JScrollPane scroll;
	JEditorPane editor;
	HTMLEditorKit kit;
	JButton startseite;
	
		
	public Fenster(URL url) {
		frame = new JFrame();
		textfeld = new JTextField();
		editor = new JEditorPane();
		pane = new Container();
		startseite = new JButton("startseite");
		
		Insets buttonMargin = new Insets(2, 2, 2, 2);
	    Dimension buttonSize = new Dimension(25, 25);
	    startseite.setMargin(buttonMargin);
	    startseite.setPreferredSize(buttonSize); 
		
					
		pane = frame.getContentPane();
		pane.setLayout(new GridBagLayout());
		
		pane.add(textfeld,new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.NORTHWEST, GridBagConstraints.NONE,       new Insets(0, 0, 0, 0), 0, 0)); 
		pane.add(startseite,  new GridBagConstraints(0, 1, 4, 1, 0.0, 0.0, GridBagConstraints.NORTHWEST, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
		pane.add(editor,    new GridBagConstraints(0, 3, 4, 1, 1.0, 1.0, GridBagConstraints.NORTHWEST, GridBagConstraints.BOTH,       new Insets(0, 0, 0, 0), 0, 0));
		
		
		
		scroll = new JScrollPane(editor);
		frame.add(scroll);
				
		try {
			editor.setEditorKit(kit);
			editor.setContentType("text/html");
			editor.setPage(url);
			editor.setEditable(false);
			System.out.println(url);
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		
				
		
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setBounds(500,500,500,500);
		frame.setLocation(200,200);
		frame.setVisible(true);
		
	}
	
	
	public static void main(String[] args) {
		try {
			new Fenster(new URL("http://www.google.de"));
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
	}
}
 

semi

Top Contributor
Hehe, lustige Effekte, was? :bae:

Das Problem war, dass Du das ScrollPane nicht in das ContentPane eingefügt hast.
Ansonsten noch die Textbreite etc.
Ich würde Dir vorschlagen anfangs immer ein Skizze auf Papier zu machen,
dann ziehst Du horizontale und vertikale Linien und schon weisst Du wo was
reinkommt und wieviele Spalten/Zeilen es jeweils in Anspruch nimmt.

Schau Dir das hier an (nur der Konstruktor, sonst nix geändert.)
Code:
   public Fenster(URL url) { 
      Insets buttonMargin = new Insets(2, 2, 2, 2); 
      Dimension buttonSize = new Dimension(25, 25); 

      startseite = new JButton("startseite"); 
      startseite.setMargin(buttonMargin); 
      startseite.setPreferredSize(buttonSize); 
      
      frame = new JFrame(); 
      textfeld = new JTextField(); 
      editor = new JEditorPane(); 
      scroll = new JScrollPane(editor);

      pane = frame.getContentPane(); 
      pane.setLayout(new GridBagLayout()); 
       
      pane.add(textfeld,new GridBagConstraints(0, 0, 1, 1, 1.0, 0.0, GridBagConstraints.NORTHWEST, GridBagConstraints.HORIZONTAL,       new Insets(0, 0, 0, 0), 0, 0)); 
      pane.add(startseite,  new GridBagConstraints(0, 1, 1, 1, 1.0, 0.0, GridBagConstraints.NORTHWEST, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0)); 
      pane.add(scroll,    new GridBagConstraints(0, 3, 1, 1, 1.0, 1.0, GridBagConstraints.NORTHWEST, GridBagConstraints.BOTH,       new Insets(0, 0, 0, 0), 0, 0)); 
             
      try { 
        editor.setEditable(false); 
        editor.setContentType("text/html"); 
        editor.setPage(url); 
        System.out.println(url); 
      } catch (IOException e1) { 
         e1.printStackTrace(); 
      } 
       
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
      frame.setBounds(500,500,500,500); 
      frame.setLocation(200,200); 
      frame.setVisible(true); 
   }
 
G

Guest

Gast
Dankeeeeee, es funktioniert. :toll:

Nur damit ich das richtig verstehe:

Du gibts der JEditorPane keine Position im Layout, sonder weist der Scrollpane die JEditorPane zu und gibts dieser dann eine Position in der Pane, dadurch wird dann die JEditorPane samt ScrollPane korrekt dargestellt?!

Code:
....
JScrollPane scroll;
....
scroll = new JScrollPane(editor);
...

und

pane.add(scroll,    new GridBagConstraints(0, 4, 2, 1, 1.0, 1.0, GridBagConstraints.NORTHWEST, GridBagConstraints.BOTH,       new Insets(0, 0, 0, 0), 0, 0));
 

semi

Top Contributor
So ist es.
Wenn Du eine Komponente in einen Container wie ScrollPane steckst, dann musst Du
das ScrollPane hinzufügen und nicht die Komponente, die darin enthalten ist.
Ansonsten gilt noch, dass keine Komponente zwei Parent-Komponenten haben darf.

So jetzt haben wir es. Oder? :wink:
 
Status
Nicht offen für weitere Antworten.
Ähnliche Java Themen
  Titel Forum Antworten Datum
O Swing GridBagLayout in Größe anpassen AWT, Swing, JavaFX & SWT 6
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

Ähnliche Java Themen

Neue Themen


Oben