Gridbagconstraints

Status
Nicht offen für weitere Antworten.

noisebreath

Bekanntes Mitglied
Code:
import java.awt.*;

import javax.swing.*;

public class test extends JPanel { 
 
   private static final long serialVersionUID = 1L; 
    
   public test() { 
       this.setLayout(new GridBagLayout()); 
       addComponentInLayout(getWelcomePanel(), 
             0, 0, 1, 1, 
             0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0,0,0,0)); 
       addComponentInLayout(getSplitPanel(), 
             0, 1, 1, 1, 
             1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0,0,0,0)); 
        
            
    } 
    
    
    private JPanel getWelcomePanel() { 
       JPanel welcomePanel = new JPanel(); 
       welcomePanel.setLayout(new FlowLayout()); 
        
       JLabel welcomeLabel = new JLabel("Willkommen ..."); 
       welcomeLabel.setFont(new java.awt.Font("MS Sans Serif", Font.BOLD, 11));
       welcomePanel.add(welcomeLabel); 
        
       return welcomePanel; 
   } 

   private JPanel getSplitPanel() { 
        
        JPanel splitPane = new JPanel(); 
        JPanel sub1 = new JPanel(); 
        sub1.setBackground(Color.red); 
        
        JPanel sub2 = new JPanel(); 
        sub2.setBackground(Color.BLACK); 
        splitPane.setLayout(new GridBagLayout()); 

       addComponentInContainer(splitPane, sub1, 
             0, 0, 1, 1, 
             0.75, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0,0,0,0)); 
       
       addComponentInContainer(splitPane, sub2, 
             1, 0, 1, 1, 
             0.25, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0,0,0,0)); 
        
       addComponentInContainer(sub1, new JLabel("bli "), 
       		 0, 0, 1, 1, 
       		 0.75, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0,0,0,0));
          
       addComponentInContainer(sub1, new JLabel("bla"), 
    		 0, 1, 1, 1, 
    		 0.75, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0,0,0,0));
           
       addComponentInContainer(sub1, new JLabel("blub"), 
       		 0, 2, 1, 1, 
       		 0.75, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0,0,0,0));
       
       addComponentInContainer(sub1, new JLabel("blä"), 
       		 0, 3, 1, 1, 
       		 0.75, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0,0,0,0));
       
       addComponentInContainer(sub1, new JLabel("blü"), 
       		 0, 4, 1, 1, 
       		 0.75, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0,0,0,0));
       
       addComponentInContainer(sub2, new JLabel("sche"), 
         	 0, 0, 1, 1, 
         	 0.75, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0,0,0,0));
       
       addComponentInContainer(sub2, new JLabel("Schri"), 
       		 0, 1, 1, 1, 
       		 0.75, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0,0,0,0));
        
       return splitPane; 
    } 
    
     //Fügt eine Componenten in das GridbagLayout ein 

    public void addComponentInLayout(Component aComp, int gridX, int gridY, int gridWidth, int gridHeight, double weightX, 
          double weightY, int anchor, int fill, Insets insets) { 
       GridBagConstraints theConstraints = new GridBagConstraints(gridX, gridY, gridWidth, gridHeight, weightX, 
             weightY, anchor, fill, insets, 0, 0); 

       add(aComp, theConstraints); 
    } 
     
     // Fügt eine Componenten in ein z.B. Panel ein. 
   
    public void addComponentInContainer(JComponent aPanel,Component aComp, int gridX, int gridY, int gridWidth, int gridHeight, double weightX, 
          double weightY, int anchor, int fill, Insets insets) { 
       GridBagConstraints theConstraints = new GridBagConstraints(gridX, gridY, gridWidth, gridHeight, weightX, 
             weightY, anchor, fill, insets, 0, 0); 
       aPanel.add(aComp, theConstraints); 
      
    } 
    
}

wie muss ich die absoluten angaben machen? ich habs zuerst mit Gridbagconstraint.relative versucht bei der x,y, width und height angabe aber das hat irgendwie auch nicht funktioniert :(
ich mein die ausgabe ist da aber er setzt die dinge nicht untereinander. den schwarzen balken seh ich auch erst dann wenn ich das fenster über meinen ganzen bildschirm aufziehe in der breite..

und was ist eigentlich dieses insets (nebenfrage)
 

L-ectron-X

Gesperrter Benutzer
Insets ist ein Freiraum um eine Komponente herum.
Insets hält die Attribute top, left, bottom und right, denen man z.B. über den Konstruktor ihre Werte zuweist und damit den Freiraum am jeweiligen Rand der Komponente festlegt.
 

noisebreath

Bekanntes Mitglied
japp das hab ich jetzt durch rumversuchen auch gesehen ^^ aber irgendwie krieg ich das bisher noch immer nicht hin die gescheit untereinander zu packen :-/
 

Shai

Aktives Mitglied
also ich versteh das grad irgendwie auch nicht.. im moment siehts so aus zb :

Code:
       addComponentInContainer(splitPane, sub1, 
               0, 0, 1, 3, 
               0.85, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0,0,0,0)); 
       
       addComponentInContainer(splitPane, sub2, 
               1, 0, 1, 2, 
               0.15, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0,0,0,0)); 
        
       addComponentInContainer(sub1, rb1, 
       		   0, 0, 1, 1, 
       		   0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0,0,0,0));
          
       addComponentInContainer(sub1, rb2, 
    		   0, 1, 1, 1, 
       		   0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0,0,0,0));
           
       addComponentInContainer(sub1, rb3, 
    		   0, 2, 1, 1, 
       		   0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0,0,0,0));
       
       addComponentInContainer(sub2, new JLabel("blubb"), 
    		   0, 0, 1, 1, 
       		   0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0,0,0,0));
       
       addComponentInContainer(sub2, new JLabel("bla"), 
    		   0, 1, 1, 1, 
       		   0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0,0,0,0));

die Seite an der ich mich orientiere sagt diese dinge :

public int gridx
Die Spaltennummer der linken oberen Zelle des Anzeigebereichs. Der Zählbeginn ist bei null.
public int gridy
Die Zeilennummer der linken oberen Zelle des Anzeigebereichs. Der Zählbeginn ist bei null.
public int gridwidth
Die Anzahl an Zellen, die der Anzeigebereich horizontal einnimmt.
public int gridheight
Die Anzahl an Zellen, die der Anzeigebereich vertikal einnimmt.

da ich in jeder zeile nur ein element haben möchte habe ich gridx mit 0 angegeben und dann eben die zeilen durchgezählt (0,1,2). width und height hab ich jeweils mit 1 angegeben. für sub 1 hab ich eine height von 3 angegeben damit die 3 elemente reinpassen (weiss nicht obs auch 2 hätte sein dürfen ->0,1,2) und für sub2 2. trotzdem funktioniert es nicht.. ich versteh hier wohl auch irgendwas grundsätzlich falsch
 

Shai

Aktives Mitglied
hab jetzt mal die funktion weggelassen und mal 2 per hand gesetzt:

Code:
       GridBagConstraints c = new GridBagConstraints();
       c.gridx = 0;
       c.gridy = 0;
       c.gridheight =1;
       c.gridwidth = 1;
       
       JLabel b = new JLabel("bli");
       sub1.add(b, c);
//addComponentInContainer(sub1, new JLabel("bli"), 
  //     		   0, 0, GridBagConstraints.RELATIVE, GridBagConstraints.RELATIVE, 
    //   		   1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0,0,0,0));
       
       c.gridx = 0;
       c.gridy = 1;
       c.gridheight =1;
       c.gridwidth = 1;
       
       JLabel d = new JLabel("blu");
       sub1.add(d, c);  
       //addComponentInContainer(sub1, new JLabel("blu"), 
    	//	   GridBagConstraints.RELATIVE, GridBagConstraints.RELATIVE, GridBagConstraints.RELATIVE, GridBagConstraints.RELATIVE,
    		//   1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0,0,0,0));

funktioniert noch immer nicht.. *schulterzuck* sorry keine ahnung :-/
 

Shai

Aktives Mitglied
ausführbarer code falls jemand auch mal lust hat sich damit zu beschäftigen:

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

import java.io.*; 

// JFrame mit RadionButtons 
// Es kann jede RadionButton gesetzt werden 


public class test extends JFrame { 

          JLabel label1; 
          JLabel label2; 
          
          JRadioButton rb1; 
          JRadioButton rb2; 

          ButtonGroup group; 
      
          //constructor 
          public test() { 
                    this.setLayout(new GridBagLayout()); 
                    setSize(300, 200); 
                addComponentInLayout(getContentPanel(), 
                                0, 0, 1, 1, 
                        0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0,0,0,0)); 
                addComponentInLayout(getSplitPanel(), 
                                0, 1, 1, 1, 
                        1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0,0,0,0)); 
                
          } 

          private JPanel getContentPanel() { 
                JPanel ContentPanel = new JPanel(); 
                ContentPanel.setLayout(new FlowLayout()); 
                ContentPanel.setMinimumSize(new Dimension(150,22)); 
                ContentPanel.setPreferredSize(new Dimension(150,22)); 
                  
                JLabel welcomeLabel = new JLabel("asdafasas"); 
                welcomeLabel.setFont(new java.awt.Font("MS Sans Serif", Font.BOLD, 11)); 
                ContentPanel.add(welcomeLabel); 
                  
                return ContentPanel; 
            } 

           private JPanel getSplitPanel() { 
                    

                   rb1 = new JRadioButton("a", false); 
                   rb2 = new JRadioButton("b", false); 
                   JRadioButton rb3 = new JRadioButton("c", false); 
               JPanel splitPane = new JPanel(); 
               JPanel sub1 = new JPanel(); 
                
               JPanel sub2 = new JPanel(); 
               sub2.setBackground(Color.lightGray); 
               splitPane.setLayout(new GridBagLayout()); 

               addComponentInContainer(splitPane, sub1, 
                       0, 0, 1, 3, 
                       0.85, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0,0,0,0)); 
                
               addComponentInContainer(splitPane, sub2, 
                       1, 0, 1, 2, 
                       0.15, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0,0,0,0)); 
                
               addComponentInContainer(sub1, new JLabel("asdf"), 
                               0, 0, 1, 1, 
                               0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.VERTICAL, new Insets(0,0,0,0)); 
                    
               addComponentInContainer(sub1, rb1, 
                                  0, 1, 1, 1, 
                                  0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.VERTICAL, new Insets(0,0,0,0)); 
                  
               addComponentInContainer(sub1, rb2, 
                               0, 2, 1, 1, 
                                  0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.VERTICAL, new Insets(0,0,0,0)); 
                    
               addComponentInContainer(sub1, rb3, 
                               0, 3, 1, 1, 
                                  0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.VERTICAL, new Insets(0,0,0,0)); 
                
               addComponentInContainer(sub2, new JLabel("Übersicht"), 
                               0, 4, 1, 1, 
                                  0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.VERTICAL, new Insets(0,0,0,0)); 
                
               addComponentInContainer(sub2, new JLabel("qwerr"), 
                               0, 0, 1, 1, 
                                  0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.VERTICAL, new Insets(0,0,0,0)); 
                
                    
                  
                   rb2.setEnabled(false); 
                   rb3.setEnabled(false); 

                   group = new ButtonGroup(); 
                   group.add(rb1); 
                   group.add(rb2); 
                   group.add(rb3); 
                
               return splitPane; 
            } 
            
             //Fügt eine Componenten in das GridbagLayout ein 

            public void addComponentInLayout(Component aComp, int gridX, int gridY, int gridWidth, int gridHeight, double weightX, 
                            double weightY, int anchor, int fill, Insets insets) { 
               GridBagConstraints theConstraints = new GridBagConstraints(gridX, gridY, gridWidth, gridHeight, weightX, 
                               weightY, anchor, fill, insets, 0, 0); 

               add(aComp, theConstraints); 
            } 
              
             // Fügt eine Componenten in ein z.B. Panel ein. 
            
            public void addComponentInContainer(JComponent aPanel,Component aComp, int gridX, int gridY, int gridWidth, int gridHeight, double weightX, 
                    double weightY, int anchor, int fill, Insets insets) { 
               GridBagConstraints theConstraints = new GridBagConstraints(gridX, gridY, gridWidth, gridHeight, weightX, 
                     weightY, anchor, fill, insets, 0, 0); 
               aPanel.add(aComp, theConstraints); 
              
            } 

  public static void main(String[] args) { 
    test frame = new test(); 
    frame.setVisible(true); 
    frame.show(); 
  }}
 

noisebreath

Bekanntes Mitglied
sagt mal weiss einer welcher parameter bei den constraints dafür verantwortlich ist , dass die zeilenabstände kleiner werden? hab meines erachtens zu viel raum zwischen meinen labels. oder geht das so gar nicht?
 
S

SlaterB

Gast
Insets ist innerhalb des GridagLayouts dafür verantwortlich,
+ hGap, vGap

falls die einzelnen Zeilen in mehreren JPanels stecken, dann bringt das Zwischenpanel auch noch paar Pixel Rand mit rein
 

noisebreath

Bekanntes Mitglied
hm.. aber die insets sind bei mir alle auf 0 gesetzt

Code:
		addComponentInContainer(sub, button, 
				0, 0, 1, 1, 
      		    0.75, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0,0,0,0));
       
		addComponentInContainer(sub, button, 
      		    0, 1, 1, 1, 
      		    0.75, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0,0,0,0));
 
		addComponentInContainer(sub, button, 
				0, 2, 1, 1,
				0.75, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0,0,0,0));
          
		addComponentInContainer(sub, button, 
				0, 3, 1, 1, 
				0.75, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0,0,0,0));
      
		addComponentInContainer(sub, new JLabel("button"), 
				1, 0, 1, 1, 
				0.25, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0,0,0,0));
      
		addComponentInContainer(sub, new JLabel("button"), 
				1, 1, 1, 1, 
				0.25, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0,0,0,0));
 
S

SlaterB

Gast
mit einem lauffähigen Beispielprogramm könnte man irgendwas dazu sagen
(ich mache jetzt keine Annahmen zu Codes anderer Poster wie Shai oder im Rest des Threads)

und bitte möglichst kurze Beispiele, wenn zwischen 10 Komponenten Platz ist, dann doch sicher auch zwischen nur 2 oder 3,

10x hintereinander
> GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0,0,0,0));
zuschreiben macht auch keinen Sinn,
schreibe dir doch eine Hilfsmethode

addGridCenterBothNoInsets(x,y, ..);

die den immergleichen Standard setzt
 

noisebreath

Bekanntes Mitglied
danke für den typ

ausführbarer Code:

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

import java.io.*; 

// JFrame mit RadionButtons 
// Es kann jede RadionButton gesetzt werden 


public class test extends JFrame { 

           private static final long serialVersionUID = 1L; 
            
           public test() { 
               this.setLayout(new GridBagLayout()); 
               this.setSize(400,400); 
               addComponentInLayout(getContentPanel(), 
                               0, 0, 1, 1, 
                       0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0,0,0,0)); 
               addComponentInLayout(getSplitPanel(), 
                       0, 1, 1, 1, 
                       1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0,0,0,0)); 
                
           } 
            
           private JPanel getContentPanel() { 
               JPanel ContentPanel = new JPanel(); 
               ContentPanel.setLayout(new GridBagLayout());          
               JLabel welcomeLabel = new JLabel("a"); 
               welcomeLabel.setFont(new java.awt.Font("MS Sans Serif", Font.BOLD, 11)); 
               ContentPanel.add(welcomeLabel); 
                
               return ContentPanel; 
           } 

           private JPanel getSplitPanel() { 
                
        	   JLabel label1 = new JLabel("bli");
               JLabel label2 = new JLabel("bla");

               
               JPanel sub = new JPanel(); 
               
               sub.setLayout(new GridBagLayout());
               
               addComponentInContainer(sub, label1, 
              		    0, 0, 1, 1, 
              		    0.75, 0.5, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0,0,0,0));
         
               addComponentInContainer(sub, label2, 
           		    0, 1, 1, 1,
           		    0.75, 0.5, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0,0,0,0));
               
               addComponentInContainer(sub, new JLabel("sdf"), 
           		    1, 0, 1, 1, 
           		    0.25, 1.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0,0,0,0));
              
               addComponentInContainer(sub, new JLabel("asdfe"), 
           		    1, 1, 1, 1, 
           		    0.25, 1.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0,0,0,0));
              
               return sub; 
               
            } 
            
             //Fügt eine Componenten in das GridbagLayout ein 

            public void addComponentInLayout(Component aComp, int gridX, int gridY, int gridWidth, int gridHeight, double weightX, 
                            double weightY, int anchor, int fill, Insets insets) { 
               GridBagConstraints theConstraints = new GridBagConstraints(gridX, gridY, gridWidth, gridHeight, weightX, 
                               weightY, anchor, fill, insets, 0, 0); 

               add(aComp, theConstraints); 
            } 
              
             // Fügt eine Componenten in ein z.B. Panel ein. 
            
            public void addComponentInContainer(JComponent aPanel,Component aComp, int gridX, int gridY, int gridWidth, int gridHeight, double weightX, 
                    double weightY, int anchor, int fill, Insets insets) { 
               GridBagConstraints theConstraints = new GridBagConstraints(gridX, gridY, gridWidth, gridHeight, weightX, 
                     weightY, anchor, fill, insets, 0, 0); 
               aPanel.add(aComp, theConstraints); 
              
            } 

  public static void main(String[] args) { 
    test frame = new test(); 
    frame.setVisible(true); 
    frame.show(); 
  }}
 

noisebreath

Bekanntes Mitglied
wenn ich hier :

public test() {
this.setLayout(new GridBagLayout());
this.setSize(400,400);
addComponentInLayout(getContentPanel(),
0, 0, 1, 1,
0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0,0,0,0));
addComponentInLayout(getSplitPanel(),
0, 1, 1, 1,
1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0,0,0,0));

}



GridBagConstraints.BOTH auf NONE stelle macht er das was ich will aber dann mittig und nicht oben im fenster.
 
S

SlaterB

Gast
jo, also habs inzwischen auch mal gesehen,
mit FILL haben die Insets natürlich weniger zu sagen,
 
Status
Nicht offen für weitere Antworten.

Ähnliche Java Themen

Neue Themen


Oben