GridBagLayout



  • huhu,
    ich versuche mich gerade am GridBagLayout,
    docht irgendwie will das nicht so wie ich^^.

    import javax.media.j3d.BranchGroup;
    import javax.media.j3d.Canvas3D;
    import javax.swing.*;
    import com.sun.j3d.utils.geometry.ColorCube;
    import com.sun.j3d.utils.universe.SimpleUniverse;
    import java.awt.*;
    import java.util.Hashtable;
    
    public class Test
    {
    	public static void main(String [] args)
    	{
    		JFrame frame = new JFrame();
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setSize(800,600);
    		Container inhalt = frame.getContentPane();
    
    		inhalt.setLayout( new GridBagLayout() );
    		GridBagConstraints c = new GridBagConstraints();
    		c.weightx = 55;
    		c.weighty = 55;
    
    		JPanel panel = new JPanel();
    		panel.setLayout(new GridLayout(2,1));
    		panel.add( new JLabel("#Balls") );
    		JSpinner spinner = new JSpinner(new SpinnerNumberModel(0, 0, 5, 1));
    		spinner.setEditor(new javax.swing.JSpinner.NumberEditor(spinner));
    		panel.add(spinner);	
    		c.gridx = 1;
    		c.gridy = 0;
    		c.gridwidth = 1;
    		c.gridheight = 1; 
    		inhalt.add( panel, c );
    
            // Begin JAVA3D -> Problem: Wird nicht angezeigt.
    		JPanel panel3d = new JPanel();
    		c.gridx = 1;
    		c.gridy = 1;
    		c.gridwidth = 1;
    		c.gridheight = 1; 
    		GraphicsConfiguration config 	= SimpleUniverse.getPreferredConfiguration();
    		Canvas3D canvas					= new Canvas3D(config);;
    		panel3d.add( canvas);
    		inhalt.add(  panel3d, c );
    		SimpleUniverse universe			= new SimpleUniverse(canvas);
    		universe.getViewingPlatform().setNominalViewingTransform();
    		BranchGroup bg = new BranchGroup();
    		bg.addChild(new ColorCube(0.5f));
    		bg.compile();
    		universe.addBranchGraph(bg);
    		//END JAVA3D
    
    		c.gridx = 1;
    		c.gridy = 2;
    		c.gridwidth = 1;
    		c.gridheight = 1;
    		JSlider slider = new JSlider(1, 50,50);
    		slider.setPaintLabels(true);
    		Hashtable labelTable = new Hashtable();
    		labelTable.put(new Integer(1), new JLabel("1"));
    		labelTable.put(new Integer(10), new JLabel("10"));
    		labelTable.put(new Integer(20), new JLabel("20"));
    		labelTable.put(new Integer(30), new JLabel("30"));
    		labelTable.put(new Integer(40), new JLabel("40"));
    		labelTable.put(new Integer(50), new JLabel("50"));
    		slider.setLabelTable(labelTable);
    		inhalt.add( slider, c );
    
    		c.gridx = 0;
    		c.gridy = 2;
    		c.gridwidth = 1;
    		c.gridheight = 1;
    		inhalt.add( new JButton("stop"), c );
    
    		c.gridx = 2;
    		c.gridy = 2;
    		c.gridwidth = 1;
    		c.gridheight = 1;
    		inhalt.add( new JButton("start"), c );
    
    		frame.setVisible(true);
    
    	}
    }
    

    Mein Ziel:
    In der Framemitte soll Java3D dargestellt werden.
    Über der Java3D-Szene soll ein JSpinner sein.
    Unter der Java3D-Szene soll ein JSlider sein.
    Links und rechts neben dem JSlider soll jeweils ein JButton sein.

    Problem:
    Die Java3D Szene wird überhaupt nicht angezeigt 😞

    Hat jemand eine Idee??

    Danke schonmal 🙂



  • Man musste einfach nur "fill" auf "GridBagConstraints.BOTH" setzen,
    bevor ich die Java3D-Szene dem Frame hinzufüge.

    // BEGIN JAVA3D
            c.fill= GridBagConstraints.BOTH;
            c.weightx = 100;
            c.weighty = 100;
            c.gridx = 1;
            c.gridy = 1;
            c.gridwidth = 1;
            c.gridheight = 1;
            GraphicsConfiguration config  		= SimpleUniverse.getPreferredConfiguration();
            Canvas3D canvas                    	= new Canvas3D(config);;
            inhalt.add(  canvas, c );
            SimpleUniverse universe            	= new SimpleUniverse(canvas);
            c.weightx = 1;
            c.weighty = 1;
            c.fill= GridBagConstraints.NONE;
            //END JAVA3D
    

Anmelden zum Antworten