gpt4 book ai didi

java - 获取异常 : java. lang.IllegalArgumentException:无法添加到布局:约束必须是字符串(或 null)

转载 作者:行者123 更新时间:2023-11-30 06:28:27 26 4
gpt4 key购买 nike

我正在实现浏览器类型的项目,但出现异常。

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

}

});

}
}

这里是异常(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)

最佳答案

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

关于java - 获取异常 : java. lang.IllegalArgumentException:无法添加到布局:约束必须是字符串(或 null),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12550073/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com