2013-05-09 82 views
1

您好,我無法將圖像添加到Groovy中的GUI標籤。任何人都可以幫助我能夠做到這一點的代碼?我到處搜索並沒有找到答案。我正在努力完成一個無法想象的項目。將圖像添加到Groovy的GUI中

我使用SwingBuilder創建我的GUI 這裏是我曾嘗試:

// add a text panel 
def mainPanel = { 
     sB.panel(layout : new BorderLayout(), background: java.awt.Color.LIGHT_GRAY){ 
      label(text: 'Welcome to your closet', horizontalAlignment: JLabel.CENTER, 
        constraints : BorderLayout.CENTER, icon: ImageIcon('/home/*****/Documents/ComputerScience/CS315/icons/create.png')) 
      buttonPanel() 
     } 
} 

我得到的錯誤是:

Caught: groovy.lang.MissingMethodException: No signature of method: GUI.ImageIcon() is applicable for argument types: (java.lang.String) values: [/home/*****/Documents/ComputerScience/CS315/icons/create.png] 
groovy.lang.MissingMethodException: No signature of method: GUI.ImageIcon() is applicable for argument types: (java.lang.String) values: [/home/*****/Documents/ComputerScience/CS315/icons/create.png] 
at GUI$_closure11_closure119.doCall(ClosetGUI.groovy:888) 

使用以下修正:

label(text: 'Welcome to your closet', horizontalAlignment: JLabel.CENTER, 
        constraints : BorderLayout.CENTER, icon: imageIcon(resource: '/home/*****/Documents/ComputerScience/CS315/icons/create.png')) 
      buttonPanel() 

我收到以下錯誤:

Caught: java.lang.RuntimeException: Failed to create component for 'imageIcon' reason:  java.lang.RuntimeException: In imageIcon the value argument 'null' does not refer to a file or a class resource 
java.lang.RuntimeException: Failed to create component for 'imageIcon' reason:  java.lang.RuntimeException: In imageIcon the value argument 'null' does not refer to a file or a class resource 
at GUI$_closure11_closure119.doCall(ClosetGUI.groovy:888) 

任何幫助將是驚人的 謝謝!

+0

你可以張貼一些代碼?你在使用GroovyFX嗎?搖擺? SwingBuilder?你有什麼嘗試?你得到的錯誤是什麼? – 2013-05-09 00:32:37

+0

抱歉,我正在使用SwingBuilders,現在我將發佈代碼! – ola 2013-05-09 00:40:13

+0

你應該使用小寫的imageIcon,而不是大寫的ImageIcon。 – 2013-05-09 00:45:40

回答

0

如果使用SwingBuilder,加載圖像可以通過以下方式進行:

imageIcon(resource:'/groovy/ui/ConsoleIcon.png') 

或者

label(icon:imageIcon('http://docs.codehaus.org/download/userResources/GROOVY/logo') 

這是基於文檔上找到here

我嘗試以下,而對我來說,它的工作原理:

import groovy.swing.SwingBuilder; 
import java.awt.FlowLayout; 


swing = new SwingBuilder(); 
gui = swing.frame(title: "Dan's Gui", size: [400, 200], defaultCloseOperation: javax.swing.WindowConstants.EXIT_ON_CLOSE) { 

    panel() { 
     myLabel = label(text: "") 
    } 
    panel(layout: new FlowLayout()) { 

     button(text: 'next', actionPerformed: { myLabel.setText("bye") }) 
     button(text: 'previous', actionPerformed: { myLabel.setText("hello") }) 
     label(icon: imageIcon(new URL('http://jworks.nl/wp-content/jworks/logo.png'))) 
    } 
} 

gui.show(); 
+0

使用第二種方法我仍然得到我在我的問題中發佈的錯誤 – ola 2013-05-09 00:48:35

+0

您可以嘗試更新的示例嗎?我爲我工作。我改變的是我添加了一個'新的URL()'來獲取圖像。 – 2013-05-09 01:22:12

+0

更新後的示例可以正常工作,但是如何將其轉換爲使用已保存文件中的資源?用戶將選擇要顯示爲圖片的文件... – ola 2013-05-09 01:40:50