Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
571 views
in Technique[技术] by (71.8m points)

swing - getting Exception : java.lang.IllegalArgumentException: cannot add to layout: constraint must be a string (or null)

I am implementing browser kind of project and I am getting an exception.

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
import chrriis.dj.nativeswing.swtimpl.NativeInterface;
import chrriis.dj.nativeswing.swtimpl.components.JWebBrowser;

public class WebPageDisplay extends JPanel{

    public WebPageDisplay() {  

         super(new BorderLayout());  
         try{
       JPanel webBrowserPanel = new JPanel(new BorderLayout());   
       // webBrowserPanel.setBorder(BorderFactory.createTitledBorder("Native Web Browser component"));   
        final JWebBrowser webBrowser = new JWebBrowser();   
        webBrowser.setBarsVisible(false);   
        webBrowser.setStatusBarVisible(true);   
        webBrowserPanel.add(webBrowser, BorderLayout.CENTER);   
        add(webBrowserPanel, BorderLayout.CENTER);
                    webBrowser.navigate("www.google.com");
        JScrollPane scrollPane = new JScrollPane();   
        Dimension preferredSize = scrollPane.getPreferredSize();   
        preferredSize.height += 20;   
        scrollPane.setPreferredSize(preferredSize);   
//      add(scrollPane, BorderLayout.NORTH);   
        add(scrollPane,new GridBagConstraints(0, 2, 2, 1, 1.0, 1.0, GridBagConstraints.CENTER, 
            GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));   

         }catch(Exception e){
             e.printStackTrace();

        }
      }

    public static void main(String[] args){
        NativeInterface.open();

         SwingUtilities.invokeLater(new Runnable() {

              public void run() {

                 WebPageDisplay webDisplay = new WebPageDisplay();

              }

            });

    }
}

Here is the exception.

java.lang.IllegalArgumentException: cannot add to layout: constraint must be a string (or null)
    at java.awt.BorderLayout.addLayoutComponent(BorderLayout.java:409)
    at java.awt.Container.addImpl(Container.java:1074)
    at java.awt.Container.add(Container.java:927)
    at WebPageDisplay.<init>(PBXPageDisplay.java:35)
    at WebPageDisplay$1.run(PBXPageDisplay.java:51)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:677)
    at java.awt.EventQueue.access$000(EventQueue.java:85)
    at java.awt.EventQueue$1.run(EventQueue.java:638)
    at java.awt.EventQueue$1.run(EventQueue.java:636)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:647)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:296)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:211)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:196)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:188)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can't use GridBagConstraints if you're not using a GridBagLayout. The compiler is telling you that this makes no sense, and I quite agree.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...