关于TextArea的问题

cyyfriend 2003-08-24 11:32:52
我建立了一个Frame;(495,335),里面添加了一个Textarea(180*125),但是TextArea有一个方法是insert(string s,int pos),在指定的位置插入字符串,但是这个int pos怎么表示呢,我想用x,y坐标表示,但它是一个整数,比如我要在textarea的第2行第3列插入一个字符,那pos应该是多少呢?我是彻底糊涂了。求教!
...全文
44 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
cyyfriend 2003-08-24
  • 打赏
  • 举报
回复
哦,谢谢
但是如果我要插入在第2行第3列那里,现在前面有很多字符,空格之类的东西,难道要计算出这些的长度吗?我想比如这样做:insert("",180*2+3),为什么不可以啊,这时光标乱跑,并没有到那里
yuebenxian 2003-08-24
  • 打赏
  • 举报
回复
pos是第几个..不是座标..大哥..
比如第3个后面就是insert("",3)
xjffj 2003-08-24
  • 打赏
  • 举报
回复
/*
* 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();
}

}

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧