我参加 Groovy 派对(和大多数其他派对一样)有点晚了,而且我能够在网上找到的文档SwingBuilder
有些有限。我正在尝试在 Groovy 中构建一个游戏应用程序(将我当前的 Java 代码转换为 Groovy)。在 Java 中,我有一个JFrame
和多个JPanel
s,其中包括标签、文本字段和按钮以及您可以在构建 GUI 时使用的所有其他有趣的摇摆项目。
所有这些的代码可能很麻烦,必须为每个等等声明变量JButton
等等JPanel
,所以当我SwingBuilder
几天前在 groovy 中发现时,我很自然地欣喜若狂。
然而,这些例子有些有限。所以这是我的初始代码。
def Game(){
mainMenu = new MainMenuPanel()
gPane = new SwingBuilder()
gPane.edt{
frame(size:[1024,768],
defaultCloseOperation: JFrame.EXIT_ON_CLOSE, location:[75,50], show:true, pack:false){
container(container:mainMenu){}
widget(mainMenu)
}
}
}
mainMenu.shell.show()
}
在MainMenuPanel
我有这个代码
class MainMenuPanel extends JPanel implements ActionListener, KeyListener, ItemListener {
public shell
shell = new SwingBuilder()
shell.panel(id: "mainM",/*size:[512, 354],
shell.edt{frame(show:true){panel(size:[512, 354],
layout: new GridLayout(cols:1, rows: 7),*/
visible:true, constraints: BL.NORTH){
textLabel = label(text: "Welcome Travelers", horizontalAlignment: 0)
speak = button(CreateButton("Say Hello", 83),
actionPerformed:{ shell.optionPane(message: "Hello World").createDialog(null, "Hello").show()})
}
}
当我运行 groovy 脚本时,我得到一个JFrame
, 里面什么都没有。我尝试了这段代码的各种组合,有时我得到两个框架,但它们并没有相互嵌套。大多数时候我什么都得不到。
我试图做的目的是有一个MainMenu
类,它返回一个菜单面板,该面板具有控制该面板的功能,并有各种其他类也返回面板,这些面板执行其他各种事情并将它们全部嵌套在窗格中。
是否有可能做到这一点?我在这里错过了什么吗?