我做一個時髦的教程,我用從書「編程Groovy的代碼。 我在本書中使用以下代碼來了解Groovy中的事件處理程序:常規錯誤MissingPropertyException:沒有這樣的屬性
+++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++++++++++++++++++++
import javax.swing.*
import java.awt.*
import java.awt.event.*
import java.awt.Container.*
import java.lang.*
frame = new JFrame(size: [300, 300],
layout: new FlowLayout(),
defaultCloseOperation: java.swing.WindowConstants.EXIT_ON_CLOSE)
button = new JButton("click")
positionLabel = new JLabel("")
msgLabel = new JLabel("")
frame.contentPane.add button
frame.contentPane.add positionLabel
frame.contentPane.add msgLabel
button.addActionListener({ JoptionPane.showMessageDialog(frame, "You clicked!")} as ActionListener)
displayMouseLocation = {positionLabel.setText("$it.x, $it.y")}
frame.addMouseListener(displayMouseLocation as MouseListener)
frame.addMouseMotionListener(displayMouseLocation as MouseMotionListener)
handle = [
focusGained : {msg.Label.setText("Good to see you!") },
focusLost : {msg.Label.setText("Come back soon!") }
]
button.addFocusListener(handleFocus as FocusListener)
events = ['WindowListener', 'ComponentListener']
handler = {msg.Label.setText("$it") }
for (event in events)
{
handleImpl = handler.asType(Class.forName("java.awt.event.${event}"))
frame."add${event}"(handlerImpl)
}
frame.show()
+++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++++++
我得到第8行的錯誤,說:
groovy.lang.MissingPropertyException:沒有這樣的屬性:java的類:execise2 at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:50) at org.codehaus.groovy.runtime.callsite.PogoGetPropertySite.getProperty(PogoGetPropertySite.java:49) at org.codehaus.groovy。 runtime.callsite.AbstractCallSite.callGroovyObjectGetProperty(AbstractCallSite.java:231) at execise2.run(execise2.groovy:8)
我在想什麼?我覺得這很簡單,但我找不到它。
謝謝!!
ironmantis7x
做到了!多謝你們!!! – ironmantis7x