2

我正在实施浏览器类型的项目,但遇到了异常。

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();

              }

            });

    }
}

这是例外。

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)
4

1 回答 1

6

如果不使用 GridBagLayout,则不能使用 GridBagConstraints。编译器告诉你这是没有意义的,我非常同意。

于 2012-09-23T05:53:21.063 回答