如何将TextArea文本放在其中心?

时间:2011-09-09 07:45:24

标签: java-me lwuit lwuit-textarea

我有一个Dialog和一个TextAreaTextArea组件的对齐设置为Component.CENTER。我创建了一个名为affiche()的方法,它显示了对话框:

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);将水平对齐设置为居中,所以我想知道如何设置垂直对齐?

2 个答案:

答案 0 :(得分:3)

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

TextArea支持中心对齐,即使它没有文档也只是将样式设置为居中对齐,它将开箱即用。

答案 1 :(得分:0)

我在不使用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代码。它有效!

相关问题