如何在smalltalk中以变形显示字典的内容? [英] How to display the contents of a dictionary in a morph in smalltalk?

查看:19
本文介绍了如何在smalltalk中以变形显示字典的内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因为我似乎无法找到一些可以显示 Dictionary 内容的预定义 Morph,我决定我最好停止寻找并想要创建我自己的 Morph.我找到了一个 不错的描述 如何从一些不错的示例代码开始让我开始,但很快我就遇到了一个问题,我似乎无法在画布上绘制文本或类似内容.

Because I don't seem to be able to find some predefined Morph that can display the contents of a Dictionary, I decided I'd better stop looking and wanted to create my own Morph. I found a nice description how to start with some nice example code to get me started, but quite soon I got to the problem that I don't seem to manage to draw text or anything like that on a canvas.

我创建了一个类

Morph subclass: #DictionaryView
    instanceVariableNames: 'dictionary'
    classVariableNames: ''
    poolDictionaries: ''
    category: 'StatisticsTool'

我想按如下方式覆盖 drawOn:

and I wanted to override drawOn as follows:

drawOn: aCanvas

    | x y |
    x := 0.
    y := 0.
    dictionary associationsDo: [ :assoc |
        aCanvas drawString: assoc key at: x@y.
        aCanvas drawString: assoc value at: x+10@y.
        y := y + 10. ].

我知道这不是最好的一段代码(我还不知道我应该如何考虑最长的字符串等,但我已经到了我什至不想再考虑那个的地步),但我只是想显示一些东西.不幸的是,当我尝试时,这似乎不起作用

I know this is not exactly the best piece of code (I have no idea yet how I should take into account the longest string etc, but I got to this point where I don't even really want to think about that anymore), but I just wanted to get something displayed. Unfortunately, this does not seem to work when I try

d := Dictionary new.
d at: 'test1' put: 5.
d at: 'test2' put: 23.
d at: 'test3' put: 514.

view := DictionaryView new.
view dictionary: d.
view openInWorld.

我收到一个错误:SmallInteger 的实例不可索引

我不知道该怎么办了.我实际上没有时间写这些冗长的问题,也没有时间为这样的事情花上整整一周的时间.这一切让我非常紧张和不耐烦,因此我想原谅自己直接提问:

I don't know what to do anymore. I actually don't have time to write these long questions or to look a whole week for something like this. This all makes me very nervous and impatient and therefore I would like to excuse myself for the direct way of asking:

如何在 Smalltalk 中显示字典以便我可以在 GUI 中使用它?

How can I display a dictionary in Smalltalk so that I can use it in a GUI?

PS:也欢迎任何有关应对压力的提示;)

PS: any tips on coping with stress are also welcome ;)

推荐答案

你的错误根源在这里

    aCanvas drawString: **assoc key** at: x@y.
    aCanvas drawString: **assoc value** at: x+10@y.

不能保证它们中的任何一个都是字符串(在您的情况下,值是数字),因此您必须手动转换它们

There's no guarantee that any of them will be string (and in your case the values are numbers), so you have to convert them manually

    aCanvas drawString: assoc key printString at: x@y. "or asString"
    aCanvas drawString: assoc value printString at: x+10@y.

您应该能够很容易地调试此类问题.

You should be able to debug this kind of problem quite easily.

关于字符串的宽度,您可以询问字体的字符串长度.

Regarding the width of string, you can ask a font for the length of a string.

Preferences standardDefaultTextFont heightOfString: 'hello'

更新:

您也可以简单地将所有值转换为 StringMorph 并将它们组合在一起.

You can also simply convert all the values to StringMorphs and compose them together.

DictionaryView>>dictionary: aDictionary
    | container keys values |
    (container := Morph new)
        layoutPolicy: TableLayout new;
        listDirection: #leftToRight.
    (keys := Morph new) layoutPolicy: TableLayout new.
    (values := Morph new) layoutPolicy: TableLayout new.
    aDictionary
        associationsDo:
            [ :assoc | 
            keys addMorph: assoc key printString asMorph.
            values addMorph: assoc value printString asMorph ].
    container
        addMorph: keys;
        addMorph: values.
    self addMorph: container

(当然删除 #drawOn: 方法,因为它将不再需要)

(of course remove the #drawOn: method as it will be no longer needed)

显然还有很大的改进空间,但这超出了本问答的范围.

Obviously there's a lot of room for improvement, but that's out of the scope of this Q&A.

或者,您可以使用 MulticolumnLazyListMorph 小部件.

Alternatively you can use the MulticolumnLazyListMorph widget.

这篇关于如何在smalltalk中以变形显示字典的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆