Smalltalk 按钮事件处理

Ahm*_*dkt 6 oop smalltalk squeak

我是 Squeak Smalltalk 的新手。如何捕获按钮单击事件并在单击按钮时执行一些代码。

我尝试了这段代码,但它不起作用!

我创建了一个新的按钮类:

SimpleButtonMorph subclass: #ButtonTest
instanceVariableNames: ''
classVariableNames: ''
poolDictionaries: ''
category: 'test'
Run Code Online (Sandbox Code Playgroud)

在这个类中我写了这个方法:

handleMouseDown: event
^ true.
Run Code Online (Sandbox Code Playgroud)

我想在另一个 Morph 中使用按钮,所以我创建了新的 Morph 类:

RectangleMorph subclass: #RectangleMorphTest
instanceVariableNames: 'textField button stringLabel mouseAction'
classVariableNames: ''
poolDictionaries: ''
category: 'test'
Run Code Online (Sandbox Code Playgroud)

在Initialize方法中,我在RectangleMorph中编写了buttonMorph,矩形Morph的初始化方法:

initialize  
super initialize.
self bounds: ((0@0) extent: (400@400)).
self color: Color gray.

textField := TextFieldMorph new. 
textField color: Color lightYellow.
textField contents: 'text'.
textField bounds: ((25@25) extent: (300@75)).

button := ButtonTest new.
button borderWidth: 2.
button bounds: ((150@150) extent: (200@200)).
button label: 'print text'.
button target: button.  
button mouseDownOn: #yellowButtonPressed  event: [Transcript show: 'hello'].

stringLabel := StringMorph new contents: 'This is a string'.
stringLabel bounds:  ((150@180) extent: (200@200)).

self addMorph: textField.
self addMorph: button.
self addMorph: stringLabel.
Run Code Online (Sandbox Code Playgroud)

问题

我尝试在 RectangleMorph 内处理 Button 事件,但没有成功。那么我如何处理 RectangleMorph 内的 Button 单击事件呢?

Kwa*_*aku 3

在http://wiki.squeak.org/squeakSimpleButtonMorph上搜索会得到很多结果。

这里有一些如何使用按钮类的示例

您基本上必须定义按钮的目标和操作选择器。目标可能是一个代码块,而操作选择器是您发送到该块的方法#value。

所以你有了

  button target: [Transcript show: 'hello'].
Run Code Online (Sandbox Code Playgroud)

进而

  button actionSelector: #value.
Run Code Online (Sandbox Code Playgroud)

也可以看看