1

我有一个 Dialog 和一个TextArea. 该TextArea组件的对齐方式设置为Component.CENTER。我创建了一个名为affiche()显示 Dialog 的方法:

public class Alert extends Dialog {
    private TextArea chp;
    private Command[] comms;
    public Alert(String text, Command[] comms)
    {
        super();
        this.comms = comms;
        setAutoDispose(true);
        for (int c=0; c<comms.length; c++)
            addCommand(comms[c]);
        chp = new TextArea(text);
        chp.setAlignment(Component.CENTER);
        chp.setEditable(false);
        chp.getSelectedStyle().setBorder(null);
        chp.getUnselectedStyle().setBorder(null);
        chp.getSelectedStyle().setBgColor(this.getStyle().getBgColor());
        chp.getUnselectedStyle().setBgColor(this.getStyle().getBgColor());
        chp.setRowsGap(3);
    }
    public Command affiche()
    {
        return show(null, chp, comms);
    }
}

我的问题是从另一个表单TextArea调用方法时,文本显示在顶部。affiche()

那么如何在中间显示文字TextArea呢?我的意思是“居中”宽度的中心和高度的中心。我已经通过代码将水平对齐设置为居中chp.setAlignment(Component.CENTER);,所以我想知道如何设置垂直对齐?

4

2 回答 2

2

我之前的答案是错误的,我现在忘记了我们做了什么......

TextArea 支持居中对齐,即使它的未记录只是将样式设置为居中对齐,并且可以开箱即用。

于 2011-09-11T06:17:44.017 回答
1

我在不使用 DefaultLookAndFeel 类的情况下找到了解决方案。这里是 :

public class Alert extends Dialog {
    private TextArea chp;
    private Command[] comms;
    public Alert(String text, Command[] comms)
    {
        super();
        this.comms = comms;
        setAutoDispose(true);
        for (int c=0; c<comms.length; c++)
            addCommand(comms[c]);

        chp = new TextArea();
        chp.setAlignment(Component.CENTER);
        chp.setEditable(false);
        chp.getSelectedStyle().setBorder(null);
        chp.getUnselectedStyle().setBorder(null);
        chp.getSelectedStyle().setBgColor(this.getStyle().getBgColor());
        chp.getUnselectedStyle().setBgColor(this.getStyle().getBgColor());
        while (text.substring(0, (text.length()/2)+1).length() < chp.getMaxSize()/2)
        {
           text = " ".concat(text);
        }
        chp.setText(text);
    }
    public Command affiche()
    {
        return show(null, chp, comms);
    }
}

所以我只是添加了while代码。它有效!

于 2011-09-13T12:43:29.540 回答