JTextArea得到整行 [英] JTextArea getting whole line

查看:81
本文介绍了JTextArea得到整行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从JTA中选择行?

How can I get chosen line from JTA ?

推荐答案

为什么不将行拆分为标记.那么如果您知道所需的行号,则可以通过一个字符串数组来访问它

Why not break the lines up into tokens. then if you know the line number you want, you can just access it via an array of Strings

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);
 }
}

这篇关于JTextArea得到整行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆