GridBagLayout Problem

bafl13

Mitglied
Hallo ,
bin mit GridBagLayout nur angefangen und ich möchte ein Programm erstellen dass das folgende interface erstellt
ImageShack® - Online Photo and Video Hosting
nun ich habe es so geschrieben,
Java:
import javax.swing.*; 
import java.awt.*; 

class Aufgabe1Blatt6 extends JFrame{ 
    JFrame frame=new JFrame("Aufgabe1"); 
    JButton button=new JButton("Button3"); 
    JRadioButton radio1=new JRadioButton("Button1"); 
    JRadioButton radio2=new JRadioButton("Button2"); 
    JScrollPane scroll=new JScrollPane(); 
    JList list ; 
    ButtonGroup gruppe=new ButtonGroup(); 
    Aufgabe1Blatt6(){ 
        this.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
        GridBagConstraints c=new GridBagConstraints(); 
        JPanel panel=new JPanel(new GridBagLayout()); 
        this.radio1.setBackground(Color.yellow); 
        this.radio2.setBackground(Color.yellow); 
        this.radio1.setSize(10,10); 
        this.radio2.setSize(10, 10); 
        
        this.gruppe.add(radio1); 
        this.gruppe.add(radio2); 
        c.gridx=0; 
        c.gridy=0; 
        c.fill=GridBagConstraints.HORIZONTAL; 
        this.frame.add(panel); 
        panel.add(radio1,c); 
        
        c.insets=new Insets(2,0,0,0); 
        c.gridy=1; 
        panel.add(radio2,c); 
        c.gridy=2; 
        c.insets=new Insets(100,0,0,0); 
        panel.add(button,c); 
        c.gridx=1; 
        c.gridy=0; 
        c.fill=GridBagConstraints.VERTICAL; 
        c.insets=new Insets(0,2,0,0); 
        String[] listWerte={"Inhalt Nr.1","Inhalt Nr.2","Inhalt Nr.3","Inhalt Nr.4","Inhalt Nr.5","Inhalt Nr.6","Inhalt Nr.7" 
                ,"Inhalt Nr.8","Inhalt Nr.9","Inhalt Nr.10"}; 
        this.list=new JList(listWerte); 
        this.scroll=new JScrollPane(this.list); 
        panel.add(this.scroll,c); 
        this.frame.setVisible(true); 
        this.frame.pack(); 
    } 
    public static void main(String ...args){ 
  
        SwingUtilities.invokeLater(new Runnable() { 
        public void run() { 
        Aufgabe1Blatt6 mw = new Aufgabe1Blatt6(); 
        } 
        }); 
    } 
}
und so sieht es aus bei mir,und leider habe ich so lange versuch,aber w könnte es nicht erstellen so das es wie im Foto aussieht und auch dass die beide radiobuttons immer obenlinks stehen,
ich habe es mit
Java:
panel.add(radio1,BorderLayout.NorthWest);
nur es geht zur seite und ich das fenster vergrößern will,kommt es wieder in die mitte
Kann jemand mir helfen bitte🙁?????
 
Hab deinen Code um fehlende Zeilen angepasst und ein paar Fehler beseitigt:
Java:
import javax.swing.*; 
import java.awt.*; 
 
class Aufgabe1Blatt6 extends JFrame{ 
    JButton button=new JButton("Button3"); 
    JRadioButton radio1=new JRadioButton("Button1"); 
    JRadioButton radio2=new JRadioButton("Button2"); 
    JList list; 
    ButtonGroup gruppe=new ButtonGroup();
     
    Aufgabe1Blatt6(String title){
        super(title);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        GridBagConstraints c=new GridBagConstraints(); 
        JPanel panel=new JPanel(new GridBagLayout()); 

        radio1.setBackground(Color.yellow); 
        radio2.setBackground(Color.yellow); 
        
        gruppe.add(radio1); 
        gruppe.add(radio2);
        radio1.setSelected(true);
 
        c.gridx=0; 
        c.gridy=0;
        c.gridwidth = 2;
        c.weightx = 0.25;
        c.fill=GridBagConstraints.HORIZONTAL; 
        this.add(panel);
        panel.add(radio1,c); 
        
        c.insets=new Insets(2,0,0,0); 
        c.gridy=1;
        c.anchor = GridBagConstraints.NORTH;
        panel.add(radio2,c); 

        c.gridy=2; 
        c.gridwidth = 1;
        c.weightx = 0.0;
        c.anchor = GridBagConstraints.SOUTH;
        c.insets=new Insets(100,0,0,0); 
        panel.add(button,c); 

        c.gridx=2; 
        c.gridy=0;
        c.gridheight = 0;
        c.weightx = 1.0;
        c.weighty = 1.0;
        c.fill=GridBagConstraints.BOTH; 
        c.insets=new Insets(0,2,0,0);
 
        String[] listWerte={"Inhalt Nr.1","Inhalt Nr.2","Inhalt Nr.3","Inhalt Nr.4","Inhalt Nr.5","Inhalt Nr.6","Inhalt Nr.7" 
                ,"Inhalt Nr.8","Inhalt Nr.9","Inhalt Nr.10"}; 

        list=new JList(listWerte); 
        panel.add(new JScrollPane(list), c); 

        this.pack();
        this.setSize(450, 250);
        this.setVisible(true);
    } 
    
    public static void main(String[] args){
  
        SwingUtilities.invokeLater(new Runnable() { 
            public void run() {
                Aufgabe1Blatt6 mw = new Aufgabe1Blatt6("Aufgabe 1");
            }
        }); 
    } 
}
 
Hab deinen Code um fehlende Zeilen angepasst und ein paar Fehler beseitigt...
Vielen Dank,nur ich hatte noch eine Frage:
Wie könnte ich ein Compononet ObenLinks setzten??
ich habe es so versucht mit diesem Programm aber es klappt nie!
Java:
import javax.swing.*; 

import java.awt.*; 
 
class GridBagLayoutDemo extends JFrame{
	GridBagLayoutDemo(String s){
		super(s);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		GridBagConstraints c=new GridBagConstraints();
		JPanel panel=new JPanel(new GridBagLayout());
		JButton button=new JButton("egal");
		c.gridx=0;
		c.gridy=0;
		c.anchor=GridBagConstraints.NORTHWEST;
		
		panel.add(button,c);
		this.add(panel);
		this.setVisible(true);
		this.setSize(400,400);
		
	}
	public static void main(String...args){
	 SwingUtilities.invokeLater(new Runnable() { 
         public void run() {
        	 GridBagLayoutDemo mw = new GridBagLayoutDemo("Beispiel");
         }
     }); 

	}
}
und es Bleibt immer in der Mitte:S:S
Danke🙂
 
Ich setze beim GridBagLayout immer weightx und weighty auf 1.0 bei der letzten Komponente, die ich hinzufüge, sowie anchor auf NORTHWEST, dann hab ich alles links oben in der Ecke.
 

Zurück
Oben