gpt4 book ai didi

java - 使用 MigLayout 将 JLabel 添加到 JPanel 时出现类转换异常(java.lang.String 无法转换为 net.miginfocom.layout.CC)

转载 作者:行者123 更新时间:2023-11-30 10:00:45 25 4
gpt4 key购买 nike

我有以下代码:

JPanel panel = new JPanel(new MigLayout());;
JLabel label = new JLabel("Sample Label")
panel.add(label, "gaptop 20, gapbottom 20,wrap");

当我浏览 MigLayout 文档时,上面的代码片段看起来是有效的 对我来说,但在运行时它会抛出 java.lang.ClassCastException 异常和下面的堆栈跟踪

java.lang.ClassCastException: java.lang.String cannot be cast to net.miginfocom.layout.CC
at com.intellij.ui.layout.migLayout.patched.MigLayout.addLayoutComponent(MigLayout.kt:403)
at java.awt.Container.addImpl(Container.java:1128)
at java.awt.Container.add(Container.java:973)
at com.xxx.size.plugin.MemoryMeasurementTool_Tool.loadToolResultPanel(xxxMeasurementTool_Tool.java:58)
at com.xxx.size.plugin.MemoryAnalyzer$1.run(xxxAnalyzer.java:75)
at jetbrains.mps.smodel.WorkbenchModelAccess$UndoContextSetup.run(WorkbenchModelAccess.java:378)
at jetbrains.mps.smodel.ActionDispatcher.dispatch(ActionDispatcher.java:84)
at jetbrains.mps.smodel.ActionDispatcher.lambda$wrap$0(ActionDispatcher.java:105)
at jetbrains.mps.smodel.ActionDispatcher.dispatch(ActionDispatcher.java:84)
at jetbrains.mps.smodel.ActionDispatcher.lambda$wrap$0(ActionDispatcher.java:105)
at jetbrains.mps.smodel.LockRunnable.run(LockRunnable.java:60)
at jetbrains.mps.smodel.TryRunPlatformWriteHelper.lambda$tryWrite$2(TryRunPlatformWriteHelper.java:122)
at com.intellij.openapi.application.impl.ApplicationImpl.runWriteAction(ApplicationImpl.java:1057)
at jetbrains.mps.smodel.TryRunPlatformWriteHelper.runWrite(TryRunPlatformWriteHelper.java:103)
at jetbrains.mps.smodel.TryRunPlatformWriteHelper.tryWrite(TryRunPlatformWriteHelper.java:120)
at jetbrains.mps.smodel.WorkbenchModelAccess.lambda$tryWriteInCommand$4(WorkbenchModelAccess.java:226)
at jetbrains.mps.util.ComputeRunnable.compute(ComputeRunnable.java:32)
at jetbrains.mps.util.AbstractComputeRunnable.run(AbstractComputeRunnable.java:27)
at com.intellij.openapi.command.impl.CoreCommandProcessor.executeCommand(CoreCommandProcessor.java:220)
at com.intellij.openapi.command.impl.CoreCommandProcessor.executeCommand(CoreCommandProcessor.java:178)
at com.intellij.openapi.command.impl.CoreCommandProcessor.executeCommand(CoreCommandProcessor.java:168)
at jetbrains.mps.smodel.WorkbenchModelAccess.tryWriteInCommand(WorkbenchModelAccess.java:243)
at jetbrains.mps.smodel.WorkbenchModelAccess.lambda$runCommandInEDT_$3(WorkbenchModelAccess.java:157)
at jetbrains.mps.smodel.EDTExecutor$1.tryRun(EDTExecutor.java:60)
at jetbrains.mps.smodel.EDTExecutorInternal.tryToRunTopTask(EDTExecutorInternal.java:231)
at jetbrains.mps.smodel.EDTExecutorInternal.flushNTasks(EDTExecutorInternal.java:194)
at jetbrains.mps.smodel.EDTExecutorInternal.doFlush(EDTExecutorInternal.java:168)
at jetbrains.mps.smodel.EDTExecutorInternal.flushTasksQueue(EDTExecutorInternal.java:156)
at jetbrains.mps.smodel.EDTExecutorInternal.lambda$guaranteeWriteSafetyViaHack$1(EDTExecutorInternal.java:147)
at com.intellij.openapi.application.TransactionGuardImpl$2.run(TransactionGuardImpl.java:315)
at com.intellij.openapi.application.impl.LaterInvocator$FlushQueue.doRun(LaterInvocator.java:435)
at com.intellij.openapi.application.impl.LaterInvocator$FlushQueue.runNextEvent(LaterInvocator.java:419)
at com.intellij.openapi.application.impl.LaterInvocator$FlushQueue.run(LaterInvocator.java:403)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:762)
at java.awt.EventQueue.access$500(EventQueue.java:98)
at java.awt.EventQueue$3.run(EventQueue.java:715)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:732)
at com.intellij.ide.IdeEventQueue.defaultDispatchEvent(IdeEventQueue.java:719)
at com.intellij.ide.IdeEventQueue._dispatchEvent(IdeEventQueue.java:668)
at com.intellij.ide.IdeEventQueue.dispatchEvent(IdeEventQueue.java:363)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

最佳答案

看起来,您使用了错误的导入(您使用了 Intellij Idea 修补的 MigLayout,该用法可能与标准用法不同)。检查您的 MigLayout 是否导入为:

import net.miginfocom.swing.MigLayout;

关于java - 使用 MigLayout 将 JLabel 添加到 JPanel 时出现类转换异常(java.lang.String 无法转换为 net.miginfocom.layout.CC),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57819120/

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