JTextArea设置最大显示3行,而默认文本有5行。但是显示时显示最后的3行,我要一开始就显示前面的3行,怎么做?

解决方案 »

  1.   

    我试了下没有你说的这种情,一开始就在上面了。你运一下我的这个代码
    /**
       @version 1.32 2004-05-05
       @author Cay Horstmann
    */import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
     
    public class TextEditTest 
    {
       public static void main(String[] args)
       {  
          TextEditFrame frame = new TextEditFrame();
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.setVisible(true);
       }
    }/**
       A frame with a text area and components for search/replace.
    */
    class TextEditFrame extends JFrame
    {  
       public TextEditFrame()
       {  
          setTitle("TextEditTest");
          setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);      JPanel panel = new JPanel();      // add button, text fields and labels
          
          JButton replaceButton = new JButton("Replace");
          panel.add(replaceButton);
          replaceButton.addActionListener(new ReplaceAction());      from = new JTextField("brown", 8);
          panel.add(from);      panel.add(new JLabel("with"));      to = new JTextField("purple", 8);
          panel.add(to);
          add(panel, BorderLayout.SOUTH);
        
          // add text area with scroll bars
          String text ="775555555555555555555555555555555555555555555555555555555555555555555555555555llllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllll55555555555555555555555555555555555551111";
          textArea = new JTextArea(text,3,20);
         // textArea.setText
            // ("The quick brown fox jumps over the lazy dog.");
          //JScrollPane scrollPane = new JScrollPane(textArea);
          TextArea.setLineWrap(true);
          //add(scrollPane, BorderLayout.CENTER);
          add(textArea, BorderLayout.CENTER);
       }
       
       public static final int DEFAULT_WIDTH = 200;
       public static final int DEFAULT_HEIGHT = 120;     private JTextArea textArea;
       private JTextField from;
       private JTextField to;   /**
          The action listener for the replace button.
       */
       private class ReplaceAction implements ActionListener
       {
          public void actionPerformed(ActionEvent event)
          {  
             String f = from.getText();
             int n = textArea.getText().indexOf(f);
             if (n >= 0 && f.length() > 0)
                textArea.replaceRange(to.getText(), n, 
                   n + f.length());
          }
       }
    }
      

  2.   

    利用javax.swing.text.JTextComponent的public void setCaretPosition(int position);以前用过,可以实现你的功能。public void setCaretPosition(int position)
    Sets the position of the text insertion caret for the TextComponent. Note that the caret tracks change, so this may move if the underlying text of the component is changed. If the document is null, does nothing. The position must be between 0 and the length of the component's text or else an exception is thrown. Parameters:
    position - the position 
    Throws: 
    IllegalArgumentException - if the value supplied for position is less than zero or greater than the component's text length