Как разместить текст TextArea в его центре? - PullRequest
1 голос
/ 09 сентября 2011

У меня есть диалог и TextArea. Компонент TextArea имеет выравнивание, установленное на 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 ]

3 голосов
/ 11 сентября 2011

Мой предыдущий ответ был неправильным, я забыл, что мы уже сделали ...

TextArea поддерживает выравнивание по центру, даже если его недокументированное просто устанавливает стиль выравнивания по центру, и он будет работать из коробки.

0 голосов
/ 13 сентября 2011

Я нашел решение без использования класса 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.И это работает!

...