2010-03-16 4 views
7

SMS를 보내는 것을 다루는 내 응용 프로그램에서 JTextArea 객체를 사용하고 있습니다.JTextArea의 크기를 일정하게 유지하는 방법은 무엇입니까?

텍스트 영역에 160 자만 입력 할 수 있도록 DocumentFilter를 사용했지만 텍스트 영역의 크기를 일정하게 유지하려고합니다. '입력'키를 누르지 않고도 같은 줄에 계속 글자를 쓰면 계속 증가합니다. 을 입력하십시오. 한 번 '스크롤바'를 사용하여 시도했지만 문제는 동일하게 유지됩니다. 이것 저에게 뭔가 제안 해주십시오. 아래는 제 코드입니다. 그것을 확인하십시오.

class Send_sms extends JPanel implements ActionListener,DocumentListener  
{  
    JButton send; 
    JTextArea smst; 
    JLabel title,limit; 
    JPanel mainp,titlep,sendp,wrap,titlewrap,blankp1,blankp2,sendwrap; 
    JScrollPane scroll; 
    Border br,blackbr; 
    Boolean flag = false; 
    PlainDocument plane; 
    public static final int LINES = 4; 
    public static final int CHAR_PER_LINE = 40;  
     //character limit 160 for a sms 

    public Send_sms() 
     { 
     br = BorderFactory.createLineBorder(Color.RED); 
     blackbr = BorderFactory.createEtchedBorder(EtchedBorder.RAISED,Color.DARK_GRAY,Color.GRAY); 
     setBorder(blackbr); 

       title = new JLabel("Enter the text you want to send!"); 
     title.setFont(new Font("",Font.BOLD,17)); 
     limit = new JLabel(""+charCount+" Characters"); 
     smst = new JTextArea(LINES,CHAR_PER_LINE); 
     smst.setSize(100,100); 
     plane = (PlainDocument)smst.getDocument(); 
     //adding DocumentSizeFilter 2 keep track of characters entered 
     plane.setDocumentFilter(new DocumentSizeFilter(charCount)); 
     plane.addDocumentListener(this); 
     send = new JButton("Send"); 
     send.setToolTipText("Click Here To Send SMS"); 
     send.addActionListener(this); 

     //scroll = new JScrollPane(smst); 
     //scroll.setPreferredSize(new Dimension(200,200)); 
     //scroll.setVerticalScrollBarPolicy(null); 
     //scroll.setHorizontalScrollBarPolicy(null); 
     smst.setBorder(br); 

     blankp1 = new JPanel(); 
     blankp2 = new JPanel(); 
     titlep = new JPanel(new FlowLayout(FlowLayout.CENTER)); 
     titlewrap = new JPanel(new GridLayout(2,1)); 
     mainp = new JPanel(new BorderLayout()); 
     sendwrap = new JPanel(new GridLayout(3,1)); 
     sendp = new JPanel(new FlowLayout(FlowLayout.CENTER)); 
     wrap = new JPanel(new BorderLayout()); 

     titlep.add(title); 
     titlewrap.add(titlep); 
     titlewrap.add(blankp1); 

     sendp.add(send); 
     sendwrap.add(limit); 
     sendwrap.add(blankp2); 
     sendwrap.add(sendp); 

     wrap.add(smst,BorderLayout.CENTER); 
     mainp.add(titlewrap,BorderLayout.NORTH); 
     mainp.add(wrap,BorderLayout.CENTER); 
     mainp.add(sendwrap,BorderLayout.SOUTH); 
     add(mainp); 


       } 

    public void actionPerformed(ActionEvent e) 
    { 
     Vector<Vector<String>> info = new Vector<Vector<String>>(); 
     Vector<String> numbers = new Vector<String>(); 
     if(e.getSource() == send) 
     { 
      //Call a function to send he message to all the clients using text 
      //charCount = 165; 
      String msg = smst.getText(); 
      if(msg.length() == 0) 
       JOptionPane.showMessageDialog(null,"Please Enter Message","Error",JOptionPane.ERROR_MESSAGE); 
      else 
      { 
      // System.out.println("Message:"+msg); 

       Viewdata frame = new Viewdata(msg); 

       limit.setText(""+charCount+" Characters"); 
       charCount = 160; 
       } 
     } 
    } 
    public void insertUpdate(DocumentEvent e) 
    { 
     System.out.println("The legth:(insert) "+e.getLength()); 
     for(int i = 0;i<e.getLength(); i++) 
     { 
      if(charCount >0) 
       charCount--; 
      else 
       break; 
     } 
     limit.setText(""+charCount+" Characters"); 

    } 
    public void removeUpdate(DocumentEvent e) 
    { 
     //System.out.println("The legth(remove): "+e.getLength()); 
     for(int i = 0;i<e.getLength(); i++) 
     { 

      charCount++; 

     } 
     limit.setText(""+charCount+" Characters");  
    } 
    public void changedUpdate(DocumentEvent e) 
    { 
     //System.out.println("The legth(change): "+e.getLength()); 

    } 

}//end Send_sms 

답변

5

당신은 지정해야합니다

textArea.setColumns (160); 
textArea.setLineWrap (true); 
textArea.setWrapStyleWord (false); //default 

을하지만 진짜 문제는 160 자보다 더 입력 할 수 있다는 것입니다. 이미 160자를 쓰면 입력 된 모든 문자를 건너 뛰는 일종의 유효성 검사기를 만들어야합니다.

10

사운드 당신이 성장에 유지되도록 텍스트 영역이 적절한 사이즈가없는이 형식을 사용하는 경우

JTextArea textArea = new JTextArea(); 

사용하여 텍스트 영역을 만드는있다. 당신이 사용하는 경우 :

JTextArea textArea = new JTextArea(2, 30); 
JScrollPane scrollPane = new JScrollPane(textArea); 

그런 다음 텍스트 영역이 선호하는 2 행의 크기 (약) 30 열이있을 것이다. 원하는 너비를 초과 할 때 입력 할 때 가로 스크롤 막대가 나타납니다. 또는 포장을 켜면 텍스트가 줄 바꿈되고 세로 스크롤 막대가 나타납니다.

-1

PlainDocument 확장 및의 insertString 방법 160

+0

-1 문자 제한 문서와 텍스트 영역을 초기화한다,이 문자의 수를 텍스트 영역의 크기를 제어하고 무관 그 수 문서에 추가 될 수 있습니다. 또한 사용자는 이미 문서 필터를 작성하여 문자 수를 제한하는 것이 좋습니다. – camickr

관련 문제