2010-03-16 165 views
7

我在我的应用程序中使用JTextArea的对象来处理发送短信。如何保持JTextArea的大小不变?

我已经使用了一个DocumentFilter,只允许在textarea中输入160个字符,但是现在我希望textarea的大小保持不变。如果我继续在同一行上书写而不按“输入”键,或者即使按住输入键,它也会继续增加。我曾尝试过使用'滚动条',但问题仍然存在。向我建议一些事情。以下是我的代码。请检查一下。

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文档textarea的,这有什么好做控制文本区域的大小,只有字符数,可以被添加到文档中。另外,用户已经编写了DocumentFilter来执行此操作,这是限制字符数量的首选方法。 – camickr 2010-03-16 19:42:48