【发布时间】:2013-11-20 19:24:00
【问题描述】:
我刚开始和锡兰一起玩,我真的很喜欢......
但是我在使用 Swing 时遇到了这个问题...我想使用 BorderLayout 将组件添加到 JPanel。
这是我正在使用的代码:
import javax.swing {
JLabel,
SwingUtilities { invokeLater },
JFrame { exitOnClose = \iEXIT_ON_CLOSE },
JButton,
JPanel
}
import java.lang { Runnable }
import java.awt {
Dimension,
BorderLayout { north = \iNORTH, center = \iCENTER }
}
class MySwingApp() satisfies Runnable {
shared actual void run() {
value frame = JFrame();
frame.title = "Renato app";
frame.defaultCloseOperation = exitOnClose;
frame.size = Dimension(300, 200);
frame.setLocationRelativeTo(null);
value panel = JPanel();
panel.layout = BorderLayout();
frame.add(panel);
panel.add(JLabel("Hello world"), north);
panel.add(JButton("Click me"), center);
frame.visible = true;
}
}
错误是:
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: cannot add to layout: constraint must be a string (or null)
at java.awt.BorderLayout.addLayoutComponent(BorderLayout.java:426)
at java.awt.Container.addImpl(Container.java:1120)
at java.awt.Container.add(Container.java:966)
at firstModule.MySwingApp.run(run.ceylon:52)
我运行应用程序:
invokeLater(MySwingApp());
在我看来,这是在锡兰映射字符串的问题?!?谁能看到我做错了什么(作为锡兰的新手,我不会感到惊讶)??
【问题讨论】:
-
当我尝试使用 IDE 关注
BorderLayout { NORTH }时,我最终会出现 BorderLayout.north(小写),它不是字符串而是组件!!这可能是问题的根源吗?? -
即使我使用字符串“North”和“Center”它仍然无法工作:(
-
你可以使用字符串代替导入
panel.add(JLabel("Hello world"), "North");panel.add(JButton("Click me"), "Center");