您好我在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 :(得分:1)
如果您使用的是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();