2010-01-29 4 views
1

J'ai un simple problème avec mon JPanel/GridBagLayout:problème avec GridBagLayout en Java Swing

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



public class gridfenster extends JFrame { 

    private static final long serialVersionUID = 1L; 
    private JPanel jContentPane = null; 

    private JButton b1=null; 
    private GridBagLayout gbl = null; // i tried it without this line! 

    /** 
    * @param args 
    */ 
    public void main() { 
     // TODO Auto-generated method stub 

     SwingUtilities.invokeLater(new Runnable() 
       { 
     public void run(){ 
      gridfenster thisClass = new gridfenster(); 
      thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
      thisClass.setVisible(true); 
     } 
    }); 

    } 

    /** 
    * @param owner 
    */ 
    public gridfenster() { 
     super(); 
     initialize(); 
    } 

    /** 
    * This method initializes this 
    * 
    * @return void 
    */ 
    private void initialize() { 
     this.setSize(300, 200); 
     this.setContentPane(getJContentPane()); 
     this.setTitle("testgridbag"); 
    } 

    /** 
    * This method initializes jContentPane 
    * 
    * @return javax.swing.JPanel 
    */ 
    private JPanel getJContentPane() { 
     if (jContentPane == null) { 
      jContentPane = new JPanel(); 
      GridBagLayout gbl = new GridBagLayout(); 
      jContentPane.setLayout(gbl); 
      GridBagConstraints gc = new GridBagConstraints(); 



      //konkrete Elemente: 
      gc.fill =GridBagConstraints.HORIZONTAL ; 
      gc.gridx =0; gc.gridy=0; 
      gbl.setConstraints(b1,gc); 
      jContentPane.add(b1); 

     } 
     return jContentPane; 
    } 

} 

Puis-je obtenir l'erreur suivante:

IWAV0052E Invocation Target Exception creating gridfenster 
java.lang.reflect.InvocationTargetException 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) 
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) 
    at java.lang.reflect.Constructor.newInstance(Unknown Source) 
    at org.eclipse.ve.internal.java.vce.launcher.remotevm.JFCLauncher$1.run(JFCLauncher.java:59) 
    at java.awt.event.InvocationEvent.dispatch(Unknown Source) 
    at java.awt.EventQueue.dispatchEvent(Unknown Source) 
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) 
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) 
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) 
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source) 
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source) 
    at java.awt.EventDispatchThread.run(Unknown Source) 
Caused by: java.lang.NullPointerException 
    at java.awt.Container.addImpl(Unknown Source) 
    at java.awt.Container.add(Unknown Source) 
    at gridfenster.getJContentPane(gridfenster.java:71) 
    at gridfenster.initialize(gridfenster.java:49) 
    at gridfenster.<init>(gridfenster.java:39) 
    ... 13 more 

Répondre

4

Vous n'avez pas initialisé le champ b1. Vous ne pouvez pas ajouter de null dans un conteneur.

+0

Oui, cela est juste. Le problème que j'ai signalé est une question distincte, et pourrait même ne pas être un problème du tout. –

+0

merci maintenant ça marche – Tyzak

1

Vous devez initialiser les sous-composants avant de les ajouter à leur conteneur. Le volet de contenu dans lequel vous mettez b1 n'a aucune idée de la façon de gérer le bouton, car il n'existe pas encore.

1

Vous avez déclaré que 'b1' est un JButton, mais vous ne l'avez jamais instancié.

Quelque part avant gbl.setConstraints(b1,gc), vous devez dire b1=new JButton();