简体   繁体   English

Java Swing中的GridBagLayout问题

[英]Problem with GridBagLayout in Java Swing

I have a simple problem with my JPanel / Gridbaglayout: 我的JPanel / Gridbaglayout有一个简单的问题:

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;
    }

}

Then I get the following error: 然后我得到以下错误:

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

You haven't initialized the b1 field. 您尚未初始化b1字段。 You cannot add null to a container. 您不能将null添加到容器。

You need to initialize subcomponents before adding them to their container. 您需要先初始化子组件,然后再将其添加到其容器中。 The content pane you're putting b1 into has no idea how to handle the button because it doesn't actually exist yet. 您要放入b1的内容窗格不知道如何处理该按钮,因为它实际上还不存在。

You declared that 'b1' is a JButton, but you never instantiated it. 您宣称'b1'是一个JButton,但从未实例化它。

Somewhere before gbl.setConstraints(b1,gc) , you need to say b1=new JButton(); gbl.setConstraints(b1,gc)之前的某个地方,您需要说b1=new JButton(); gbl.setConstraints(b1,gc)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM