MigLayout 中的 JTextArea 导致 NullPointerException

发布于 2024-12-28 01:57:13 字数 4876 浏览 2 评论 0原文

我不是摇摆布局管理器的专业人士,这种行为完全让我困惑。本质上,我无法在不出现 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 中使用 Window Designer,所以我假设有什么东西正在占用 MigLayout 构造函数?我已经为此绞尽脑汁有一段时间了,我设法弄清楚的是添加 textArea 或 textPanes 会导致以下异常:

java.lang.NullPointerException 位于 javax.swing.BoxLayout.preferredLayoutSize(来源未知)位于 java.awt.Container.preferredSize(来源未知)位于 java.awt.Container.getPreferredSize(来源未知)位于 javax.swing.JComponent.getPreferredSize(来源未知)位于 javax.swing.JRootPane$RootLayout.preferredLayoutSize(来源未知) 在 java.awt.Container.preferredSize(来源不明) java.awt.Container.getPreferredSize(来源未知)位于 javax.swing.JComponent.getPreferredSize(来源未知)位于 java.awt.BorderLayout.preferredLayoutSize(来源未知)位于 java.awt.Container.preferredSize(来源未知)位于 java.awt.Container.getPreferredSize(来源未知)位于 net.miginfocom.swing.MigLayout.adjustWindowSize(来源未知) net.miginfocom.swing.MigLayout.layoutContainer(来源未知) net.miginfocom.swing.MigLayout.preferredLayoutSize(来源未知) java.awt.Container.preferredSize(来源未知)位于 java.awt.Container.getPreferredSize(来源未知)位于 javax.swing.JComponent.getPreferredSize(来源未知)位于 javax.swing.BoxLayout.checkRequests(来源未知)位于 javax.swing.BoxLayout.layoutContainer(来源未知)位于 java.awt.Container.layout(来源未知)位于 java.awt.Container.doLayout(来源未知)位于 java.awt.Container.validateTree(来源未知)位于 java.awt.Container.validateTree(来源未知)位于 java.awt.Container.validateTree(来源未知)位于 java.awt.Container.validateTree(来源未知)位于 java.awt.Container.validate(来源未知)位于 java.awt.Window.show(来源未知)位于 java.awt.Component.show(来源未知)位于 java.awt.Component.setVisible(来源未知)位于 java.awt.Window.setVisible(来源未知)位于 com.soartech.ssim.testbed.gui.TestbedApplication$1.run(TestbedApplication.java:25) 在 java.awt.event.InspirationEvent.dispatch(来源未知)处 java.awt.EventQueue.dispatchEventImpl(来源未知)位于 java.awt.EventQueue.access$000(来源不明)位于 java.awt.EventQueue$1.run(来源不明)位于 java.awt.EventQueue$1.run(来源不明)位于 java.security.AccessController.doPrivileged(本机方法)位于 java.security.AccessControlContext$1.doIntersectionPrivilege(未知 来源)位于 java.awt.EventQueue.dispatchEvent(来源未知) java.awt.EventDispatchThread.pumpOneEventForFilters(来源未知) 在 java.awt.EventDispatchThread.pumpEventsForFilter(来源未知) 在 java.awt.EventDispatchThread.pumpEventsForHierarchy(未知 来源)位于 java.awt.EventDispatchThread.pumpEvents(来源未知) 在 java.awt.EventDispatchThread.pumpEvents(来源未知) java.awt.EventDispatchThread.run(来源未知)线程中的异常 “AWT-EventQueue-0”java.lang.NullPointerException at javax.swing.SizeRequirements.calculateTiledPositions(来源未知) 在 javax.swing.BoxLayout.layoutContainer(来源未知)处 java.awt.Container.layout(来源未知)位于 java.awt.Container.doLayout(来源未知)位于 java.awt.Container.validateTree(来源未知)位于 java.awt.Container.validateTree(来源未知)位于 java.awt.Container.validateTree(来源未知)位于 java.awt.Container.validateTree(来源未知)位于 java.awt.Container.validate(来源未知)位于 java.awt.Window.dispatchEventImpl(来源未知)位于 java.awt.Component.dispatchEvent(来源未知)位于 java.awt.EventQueue.dispatchEventImpl(来源未知)位于 java.awt.EventQueue.access$000(来源不明)位于 java.awt.EventQueue$1.run(来源不明)位于 java.awt.EventQueue$1.run(来源不明)位于 java.security.AccessController.doPrivileged(本机方法)位于 java.security.AccessControlContext$1.doIntersectionPrivilege(未知 来源)于 java.security.AccessControlContext$1.doIntersectionPrivilege(未知 源)位于 java.awt.EventQueue$2.run(未知源)位于 java.awt.EventQueue$2.run(来源未知)位于 java.security.AccessController.doPrivileged(本机方法)位于 java.security.AccessControlContext$1.doIntersectionPrivilege(未知 来源)位于 java.awt.EventQueue.dispatchEvent(来源未知) java.awt.EventDispatchThread.pumpOneEventForFilters(来源未知) 在 java.awt.EventDispatchThread.pumpEventsForFilter(来源未知) 在 java.awt.EventDispatchThread.pumpEventsForHierarchy(未知 来源)位于 java.awt.EventDispatchThread.pumpEvents(来源未知) 在 java.awt.EventDispatchThread.pumpEvents(来源未知) java.awt.EventDispatchThread.run(来源未知)

I'm not a pro with swing layout managers and this behavior is completely baffling me. Essentially I can't add a JTextArea or JTextPane to my MigLayout without getting a NullPointerException.

I created a class that extends 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");
}

This works fine. However, if I try adding a textArea, it will throw an exception when I try to build:

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");
}

I'm using the Window Designer in eclipse, so I'm assuming something is horking up the MigLayout constructor? I've been beating my head on this for awhile and all I've managed to figure out is that adding textArea or textPanes causes the following exception:

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(Unknown Source) at
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) at
java.awt.Container.validate(Unknown Source) at
java.awt.Window.show(Unknown Source) at
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(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) Exception in thread
"AWT-EventQueue-0" java.lang.NullPointerException at
javax.swing.SizeRequirements.calculateTiledPositions(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) at
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) at
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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情域 2025-01-04 01:57:14

使用 MigLayout 4.0,您的代码在我的计算机上运行良好。如果您修复了源附件,您也许能够深入研究 BoxLayout 并更好地了解正在发生的事情。

如果源代码中没有实际行号,我在 BoxLayout.preferredLayoutSize() 中看到的唯一可能抛出的情况是,如果您的 UI 设置返回 null 的 Border插图。这似乎不太可能,但您可以尝试在文本区域上明确设置边框作为实验。

With MigLayout 4.0 your code runs fine on my computer. If you fix your source attachment you might be able to dig down into BoxLayout and get a better idea about what is going on.

Without having the actual line number in the source, the only thing I see in BoxLayout.preferredLayoutSize() that might throw is if your UI is setting a Border that returns a null insets. This seems unlikely, but you could try setting a border on the text area explicitly just as an experiment.

空‖城人不在 2025-01-04 01:57:14

问题最终是我需要让 SwingUtilities 为我创建面板,因为它们是通过编程生成的。

    Runnable doWorkRunnable = new Runnable() {
        public void run() {
            for(TestComponent c : Config.getComponents()){
                frame.getContentPane().add(new ComponentPanel(c));
                frame.getContentPane().add(Box.createRigidArea(new Dimension(5,0)));
            }
        }
    };
SwingUtilities.invokeLater(doWorkRunnable);

现在一切就绪,我可以在 MigLayout 中包含文本区域,不会出现任何问题。

The problem ended up being that I needed to let SwingUtilities create my panels for me, because they were being programmatically generated.

    Runnable doWorkRunnable = new Runnable() {
        public void run() {
            for(TestComponent c : Config.getComponents()){
                frame.getContentPane().add(new ComponentPanel(c));
                frame.getContentPane().add(Box.createRigidArea(new Dimension(5,0)));
            }
        }
    };
SwingUtilities.invokeLater(doWorkRunnable);

Now that this is in place, I can include text areas in my MigLayout without any problems.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文