2011-09-09 10 views
1

나는 Dialog 및 TextArea을 가지고 있습니다. TextArea 구성 요소의 맞춤은 Component.CENTER입니다.TextArea 텍스트를 가운데에 배치하는 방법은 무엇입니까?

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); 
    } 
} 

내 문제는 다른 양식에서 affiche() 메서드를 호출 할 때 텍스트가 TextArea의 상단에 표시됩니다이다 : 나는 대화 상자를 표시 affiche()라는 방법을 만들었습니다.

TextArea의 가운데에 텍스트를 표시하는 방법은 무엇입니까? 나는 너비가 중심이고 높이가 중심 인 것을 "센터링"하는 것을 의미합니다. 이미 수평 정렬을 코드 chp.setAlignment(Component.CENTER);을 중심으로 설정 했으므로 수직 정렬을 설정하는 방법을 알고 싶습니다.

답변

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 코드를 추가. 그리고 그것은 작동합니다!

3

내 이전 대답은 내가

텍스트 영역은 그냥 정렬을 중앙에 스타일을 설정 문서화되지에도 불구하고 중앙 정렬을 지원하고 상자 밖으로 작동합니다 ... 우리가 지금 무슨 짓을했는지 잊어 잘못이었다.

+1

이 코드를 구현하는 방법과 예제를 제공하십시오. –

+0

아래 코드를 시도했습니다. 그것은 프로젝트에서 일하고 있습니다. textArea.getStyle(). setAlignment (CENTER); // 여기 textArea는 TextArea의 인스턴스입니다. – Thirumalvalavan

관련 문제