pos是第几个..不是座标..大哥..
比如第3个后面就是insert("",3)

解决方案 »

  1.   

    哦,谢谢
    但是如果我要插入在第2行第3列那里,现在前面有很多字符,空格之类的东西,难道要计算出这些的长度吗?我想比如这样做:insert("",180*2+3),为什么不可以啊,这时光标乱跑,并没有到那里
      

  2.   

    /*
     * TestLabel.java
     *
     * Created on 2003年8月24日, 下午9:46
     */
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;public class TestLabel extends JFrame{
        JTextArea area;
        /** Creates a new instance of TestLabel */
        public TestLabel() {
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            test2();
        }
            
        private void test2()
        {
            area = new JTextArea();
            getContentPane().add(area, "Center");
            JButton btn = new JButton("goto");
            btn.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                    gotoPosition(2, 3);
                }
            });
            getContentPane().add(btn, "North");
        }    private void gotoPosition(int line, int col)
        {
            try
            {
                int pos = area.getLineEndOffset(line-1);
                area.requestFocus();
                area.setCaretPosition(pos+col-1);
                
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
            
        }
        
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            JFrame f = new TestLabel();
            f.setSize(100, 100);
            f.show();
        }
        
    }