1

私はGroovyチュートリアルを行っており、「ProgrammingGroovy」という本のコードを使用しました。この本で次のコードを使用して、Groovyのイベントハンドラーについて学習しました。

++++++++++++++++++++++++++++++++++++++++++++++++++++ +++++++++++++++++++++++++

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:org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:50)のexecise2 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

2 に答える 2

4

上のパッケージjava.swing.WindowConstants.EXIT_ON_CLOSEが間違っています。する必要がありますjavax.swing.WindowConstants.EXIT_ON_CLOSE。groovyが。という名前の変数java.swing...のフィールドとして解釈しようとしているため、エラーメッセージは混乱を招きます。swingjava

javax.swingまた、すでにインポートしているので、を使用してWindowConstants.EXIT_ON_CLOSEください。

于 2012-12-28T17:20:14.910 に答える
2

の場合WindowConstants、次を置き換えることができます。

java.swing.WindowConstants.EXIT_ON_CLOSE

javax.swing.WindowConstants.EXIT_ON_CLOSE

または単に

WindowConstants.EXIT_ON_CLOSE
于 2012-12-28T17:17:49.993 に答える