Image 在Groovy中向GUI添加图像
您好,在Groovy中将图像添加到GUI标签时遇到问题。谁能帮我写下能做到这一点的代码?我到处都找遍了,没有找到答案。我正试图完成一个项目,但我想不出来 我正在使用SwingBuilder创建我的GUI 以下是我尝试过的:Image 在Groovy中向GUI添加图像,image,user-interface,groovy,Image,User Interface,Groovy,您好,在Groovy中将图像添加到GUI标签时遇到问题。谁能帮我写下能做到这一点的代码?我到处都找遍了,没有找到答案。我正试图完成一个项目,但我想不出来 我正在使用SwingBuilder创建我的GUI 以下是我尝试过的: // add a text panel def mainPanel = { sB.panel(layout : new BorderLayout(), background: java.awt.Color.LIGHT_GRAY){ la
// 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)
任何帮助都将是惊人的
谢谢大家! 如果您使用的是SwingBuilder,可以通过以下方式加载图像:
imageIcon(resource:'/groovy/ui/ConsoleIcon.png')
或
这是基于找到的文档
我尝试了以下方法,对我来说,效果很好:
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();
你能发一些代码吗?您正在使用GroovyFX吗?摆动SwingBuilder?你试过什么?您遇到了什么错误?抱歉,我正在使用SwingBuilder,我现在将发布代码!您应该使用小写的imageIcon,而不是大写的imageIcon。@ErikPragt我将其更改为小写,但仍然得到一个空参数运行时异常。这就是为什么我如此困惑,因为我不知道我做错了什么!使用第二种方法,我仍然得到我在问题中发布的错误。你能试试更新的示例吗?我为自己工作。我改变的是,我添加了一个“new URL()”来获取图像。更新后的示例有效,但我如何将其转换为使用保存文件中的资源?用户将选择要显示为图片的文件。。。
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();