1

How can I get chosen line from JTA ?

4

2 回答 2

2

我想您可以使用 getLineStartOffset(int line) 和 getLineEndOffset(int line) 从 getText() 返回的字符串中提取特定行

如果你的意思是你想知道用户选择了什么(使用鼠标/键盘): getSelectedText() 应该给你。

于 2010-09-08T15:34:54.260 回答
1

为什么不将线条分解成标记。那么如果你知道你想要的行号,你可以通过一个字符串数组来访问它

public class JTALineNum extends JFrame{
 JTextArea jta = null;
 JButton button = null;

 public JTALineNum(){
  jta = new JTextArea();
  button = new JButton("Hit Me");

  button.addActionListener(new ButtonListener());

  add(jta, BorderLayout.CENTER);
  add(button, BorderLayout.SOUTH);
  setSize(200,200);
  setVisible(true);
 }

 private class ButtonListener implements ActionListener{

  public void actionPerformed(ActionEvent e) {
   String text = jta.getText();
   String[] tokens = text.split("\n");
   for(String i : tokens){
    System.out.println("Token:: " + i);
   }
  }
 }

 public static void main(String args[]){
  JTALineNum app = new JTALineNum();
  app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 }
}
于 2010-09-08T15:41:32.753 回答