怎么取得文本框中第一行的内容? 全部取出来,使用StringTokenizer进行分割,使用回车作为分割符 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 try{ //第一行 String line1=jTextArea1.getText(0,jTextArea1.getLineEndOffset(0)); //第二行 String line2=jTextArea1.getText(jTextArea1.getLineEndOffset(0),jTextArea1.getLineEndOffset(1)-jTextArea1.getLineEndOffset(0)); }catch(BadLocationException ex){ System.out.println("行数不够..."); } import java.awt.*;import java.awt.event.*;import java.util.*;public class TextAreaTest extends Frame{ TextArea ta; Button btn; Button btn2; Panel p; public TextAreaTest(){ super("Text Area Test"); ta=new TextArea(10,20); add(ta); btn=new Button("get first line"); //the simplest implementation to get the first line btn.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent ev){ String content=ta.getText(); int pos=content.indexOf("\n"); String fLine=null; if(pos<0){ fLine=content.substring(0,pos); }else{ fLine=content; } System.out.print(fLine); } } ); btn2=new Button("get random line"); //implementation for getting the random line content btn2.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent ev){ Vector v=new Vector(); String content=ta.getText(); StringTokenizer tk=new StringTokenizer(content,"\n"); while(tk.hasMoreTokens()){ v.add(tk.nextToken()); } //only an example here if(v.size()>1){ System.out.println("get the 2nd Line: " + v.elementAt(1)); } } } ); p=new Panel(); p.add(btn); p.add(btn2); add(p,BorderLayout.SOUTH); setVisible(true); pack(); } public static void main(String[] args){ TextAreaTest t=new TextAreaTest(); }} 栈帧 DataInputStrean 和 DataOutputStream MS IE浏览器自带的java虚拟机比sun的jre 缺少那些包的支持? 在SWING中怎么调用保存文件和打开文件的对话框?有没有? 可否定义一个没有长度的数组,而后用switch初始不同长度的数组? 请问如何读取动态页面里的数据? 小弟不才,求助简单题目一道,有分散 morelocal2 问大家一个非技术的问题:北京和上海那的开发坏境好一些,生活环境好一些,选择去上海还是北京? java中 .class成员变量的问题 烦请大家帮忙解决一下! 刚学java都需要装什么工具~~~~~~~~~
//第一行
String line1=jTextArea1.getText(0,jTextArea1.getLineEndOffset(0));
//第二行
String line2=jTextArea1.getText(jTextArea1.getLineEndOffset(0),jTextArea1.getLineEndOffset(1)-jTextArea1.getLineEndOffset(0));
}catch(BadLocationException ex){
System.out.println("行数不够...");
}
import java.awt.event.*;
import java.util.*;public class TextAreaTest extends Frame{
TextArea ta;
Button btn;
Button btn2;
Panel p;
public TextAreaTest(){
super("Text Area Test");
ta=new TextArea(10,20);
add(ta);
btn=new Button("get first line");
//the simplest implementation to get the first line
btn.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent ev){
String content=ta.getText();
int pos=content.indexOf("\n");
String fLine=null;
if(pos<0){
fLine=content.substring(0,pos);
}else{
fLine=content;
}
System.out.print(fLine);
}
}
);
btn2=new Button("get random line");
//implementation for getting the random line content
btn2.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent ev){
Vector v=new Vector();
String content=ta.getText();
StringTokenizer tk=new StringTokenizer(content,"\n");
while(tk.hasMoreTokens()){
v.add(tk.nextToken());
} //only an example here
if(v.size()>1){
System.out.println("get the 2nd Line: " + v.elementAt(1));
}
}
}
);
p=new Panel();
p.add(btn);
p.add(btn2);
add(p,BorderLayout.SOUTH);
setVisible(true);
pack();
}
public static void main(String[] args){
TextAreaTest t=new TextAreaTest();
}
}