【问题标题】:Adding Text to a Smalltalk Cell向 Smalltalk 单元添加文本
【发布时间】:2018-04-13 07:08:43
【问题描述】:

我正在做一个项目,我正在将 Pharo 中的 Lights Out 程序改编为扫雷程序,但我不知道如何向单元格添加文本,因此它会像颜色变化一样在点击时显示当它“打开”时。我到处寻找没有骰子的方法。 初始化方法:

initialize
super initialize.
self label: ''.
self borderWidth: 4.
mineState := false.
cellValue := 0.
bounds := 0@0 corner: 32@32.
offColor := Color paleYellow.
onColor := Color paleBlue darker.
self useSquareCorners.
self turnOff

新单元格代码:

newCellAt: i at: j
"Create a cell for position (i,j) and add it to my on-screen
representation at the appropriate screen position. Answer the new cell"
| c origin |
c := MFCell new.
"self labelString: 'hidden'."
origin := self innerBounds origin.
self addMorph: c.
c position: ((i - 1) * c width) @ ((j - 1) * c height) + origin.
c mouseAction: [self checkMineAt: i at: j].
^ c

【问题讨论】:

  • 请检查我的回答。

标签: smalltalk pharo


【解决方案1】:

也许您处理问题的方式过于复杂。您需要重载两个选择器,然后就完成了(不是使用扫雷器,而是使用 Cell 上的字符串)。

我已经取了原始代码,只是应用了标签。

您只需重新定义turnOn 和turnOff,它们在您正在使用的SimpleSwitchMorph 中,并应用一个选择器label:,它可以在超类SimpleButtonMorph 中找到。您甚至可以拥有与超类中相同的逻辑。

在LOCell 中你可以这样做(为他们创建一个新协议switching):

turnOn
    super turnOn.
    self label: 'X'

turnOff
    super turnOff.
    self label: ''

现在运行示例时,X 字符串将添加到打开的单元格中。

看起来像这样:

协议:

现在的变形:

【讨论】:

    猜你喜欢
    • 2017-06-19
    • 1970-01-01
    • 2022-12-11
    • 1970-01-01
    • 2021-03-04
    • 2010-12-23
    • 1970-01-01
    • 2022-11-28
    • 1970-01-01
    相关资源
    最近更新 更多