问题
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)
回答1:
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.
来源:https://stackoverflow.com/questions/12550073/getting-exception-java-lang-illegalargumentexception-cannot-add-to-layout-co