我不是摇摆布局管理器的专业人士,这种行为完全让我感到困惑。本质上,我无法在没有得到 NullPointerException 的情况下将 JTextArea 或 JTextPane 添加到我的 MigLayout。
我创建了一个扩展 JPanel 的类:
public ComponentPanel(TestComponent testComponent) {
setLayout(new MigLayout("", "[]", "[][][][][][]"));
JLabel lblNewLabel = new JLabel("New label");
add(lblNewLabel, "cell 0 0");
JButton button = new JButton("New button");
add(button, "cell 0 3");
JButton btnNewButton = new JButton("New button");
add(btnNewButton, "cell 0 5");
}
这工作正常。但是,如果我尝试添加一个 textArea,它会在我尝试构建时引发异常:
public ComponentPanel(TestComponent testComponent) {
setLayout(new MigLayout("", "[grow]", "[][grow][][][][]"));
JLabel lblNewLabel = new JLabel("New label");
add(lblNewLabel, "cell 0 0");
JTextArea textArea = new JTextArea();
add(textArea, "cell 0 1,grow");
JButton button = new JButton("New button");
add(button, "cell 0 3");
JButton btnNewButton = new JButton("New button");
add(btnNewButton, "cell 0 5");
}
我在 Eclipse 中使用窗口设计器,所以我假设有什么东西正在启动 MigLayout 构造函数?我一直在努力解决这个问题,我设法弄清楚的是添加 textArea 或 textPanes 会导致以下异常:
java.lang.NullPointerException at javax.swing.BoxLayout.preferredLayoutSize(Unknown Source) at java.awt.Container.preferredSize(Unknown Source) at java.awt.Container.getPreferredSize(Unknown Source) at javax.swing.JComponent.getPreferredSize( Unknown Source) at javax.swing.JRootPane$RootLayout.preferredLayoutSize(Unknown Source) at java.awt.Container.preferredSize(Unknown Source) at java.awt.Container.getPreferredSize(Unknown Source) at javax.swing.JComponent.getPreferredSize( Unknown Source) at java.awt.BorderLayout.preferredLayoutSize(Unknown Source) at java.awt.Container.preferredSize(Unknown Source) at java.awt.Container.getPreferredSize(Unknown Source) at net.miginfocom.swing.MigLayout.adjustWindowSize(未知来源)在 net.miginfocom.swing.MigLayout。layoutContainer(Unknown Source) at net.miginfocom.swing.MigLayout.preferredLayoutSize(Unknown Source) at java.awt.Container.preferredSize(Unknown Source) at java.awt.Container.getPreferredSize(Unknown Source) at javax.swing.JComponent。 getPreferredSize(Unknown Source) at javax.swing.BoxLayout.checkRequests(Unknown Source) at javax.swing.BoxLayout.layoutContainer(Unknown Source) at java.awt.Container.layout(Unknown Source) at java.awt.Container.doLayout( Unknown Source) at java.awt.Container.validateTree(Unknown Source) at java.awt.Container.validateTree(Unknown Source) at java.awt.Container.validateTree(Unknown Source) at java.awt.Container.validateTree(Unknown Source) ) 在 java.awt.Container.validate(Unknown Source) 在 java.awt.Window.show(Unknown Source) 在 java.awt.Component。show(Unknown Source) at java.awt.Component.setVisible(Unknown Source) at java.awt.Window.setVisible(Unknown Source) at com.soartech.ssim.testbed.gui.TestbedApplication$1.run(TestbedApplication.java:25 ) at java.awt.event.InvocationEvent.dispatch(Unknown Source) at java.awt.EventQueue.dispatchEventImpl(Unknown Source) at java.awt.EventQueue.access$000(Unknown Source) at java.awt.EventQueue$1.run( Unknown Source) at java.awt.EventQueue$1.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue.dispatchEvent(未知来源)在 java.awt.EventDispatchThread.pumpOneEventForFilters(未知来源)在 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(未知来源)线程“AWT-EventQueue-0”中的异常 java.lang.NullPointerException 在 javax.swing.SizeRequirements.calculateTiledPositions(未知来源)在 javax.swing.BoxLayout.layoutContainer(未知来源)在 java.awt.Container.layout (Unknown Source) at java.awt.Container.doLayout(Unknown Source) at java.awt.Container.validateTree(Unknown Source) at java.awt.Container.validateTree(Unknown Source) at java.awt.Container.validateTree(Unknown源)在 java.awt.Container.validateTree(未知源)在 java.awt。Container.validate(Unknown Source) at java.awt.Window.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.EventQueue.dispatchEventImpl(Unknown Source) at java.awt.EventQueue。 access$000(Unknown Source) at java.awt.EventQueue$1.run(Unknown Source) at java.awt.EventQueue$1.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.AccessControlContext $1.doIntersectionPrivilege(Unknown Source) at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue$2.run(Unknown Source) at java.awt.EventQueue$2.run(Unknown Source) at java.security .AccessController.doPrivileged(Native Method) 在 java.security.AccessControlContext$1。doIntersectionPrivilege(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)java.awt.EventDispatchThread.run(Unknown Source) 处的 EventDispatchThread.pumpEvents(Unknown Source)java.awt.EventDispatchThread.run(Unknown Source) 处的 EventDispatchThread.pumpEvents(Unknown Source)