2012-12-28 21 views
1

我做一個時髦的教程,我用從書「編程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

回答

4

上包是錯誤的。它應該是javax.swing.WindowConstants.EXIT_ON_CLOSE。該錯誤消息是令人困惑,因爲Groovy是試圖解釋java.swing...作爲一個名爲java變量領域swing

而且,由於要導入javax.swing已經,只是使用WindowConstants.EXIT_ON_CLOSE

+0

做到了!多謝你們!!! – ironmantis7x

2

WindowConstants,可以更換:

java.swing.WindowConstants.EXIT_ON_CLOSE 

javax.swing.WindowConstants.EXIT_ON_CLOSE 

或者乾脆

WindowConstants.EXIT_ON_CLOSE 
相關問題