锡兰不被Swing识别

时间:2013-11-19 22:43:46

标签: swing ceylon

我刚开始玩Ceylon,我非常喜欢......

但是我在使用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());

在我看来,这是在锡兰中映射字符串的问题吗?!?任何人都可以看到我做错了什么(对锡兰不熟悉我不会感到惊讶)??

1 个答案:

答案 0 :(得分:2)

这里发生的事情是Container.add()的第二个参数被声明为Object,而不是java.lang.String,因此Ceylon编译器没有'要意识到需要拆除锡兰String。根据方法的签名,任何Object都是可以接受的,只是该方法的实现决定它实际上需要一个Java String。

在以下情况下,您可以使用javaString()模块中的ceylon.interop.java函数将Ceylon String转换为Java String

panel.add(JLabel("Hello world"), javaString(north));
panel.add(JButton("Click me"), javaString(center));