菜鸟问题:读文本区 2个文本区域,一个按钮,一个文本区域输入了文本,要求点击按钮,会把该文本区域的内容显示到另一个文本区域,请问按钮事件该怎么写(我要逐行读文本,因为要记录行号)? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 for exampleJButton btn = new JButton("copy");JTextArea t1 = new JTextArea();JTextArea t2 = new JTextArea();t1.append("abcd\n");t1.append("efgh\n");btn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String[] s = t1.getText().split("\n"); for (int i=0; i<s.length; i++) { t2.append((i+1) + " " + s[i] + "\n"); } }});直接手写的,没有测试过,大概这么个思路,LZ自己参考着改吧 package net.xiaohai;import java.awt.BorderLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JTextField;public class TestMain extends JFrame { JTextField tf,tf1; public TestMain() { super("Copy"); JButton btn = new JButton("Copy"); tf = new JTextField(19); tf1 = new JTextField(20); tf1.setEditable(false); this.getContentPane().add(btn, BorderLayout.SOUTH); this.getContentPane().add(tf, BorderLayout.WEST); this.getContentPane().add(tf1, BorderLayout.EAST); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setLocationRelativeTo(null); this.setSize(450, 300); this.setVisible(true); btn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String[] s = tf.getText().split("\n"); for (int i=0; i <s.length; i++) { tf1.setText((i+1) + " " + s[i] + "\n"); } } }); } public static void main(String[] args) { new TestMain(); }}仅供参考 楼主 求高手解决问题,急急! 利用线程技术来制作动画 关于构造方法的疑问 什么是“软件外包”和“欧美软件外包”? 请教错哪? JCreator DOS 窗口问题 forte和jbuilder哪个更合适开发?jbuider的光标怎么回事? 连接ORACLE数据库时,报错! Multiple process in java 我在JB3。5里面MAKE我的Cabin远程接口,怎么会出这个错? java 编译时遇到的问题,请高手指点 今天是我的生日!!!
JTextArea t1 = new JTextArea();
JTextArea t2 = new JTextArea();
t1.append("abcd\n");
t1.append("efgh\n");btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String[] s = t1.getText().split("\n");
for (int i=0; i<s.length; i++) {
t2.append((i+1) + " " + s[i] + "\n");
}
}
});直接手写的,没有测试过,大概这么个思路,LZ自己参考着改吧
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;public class TestMain extends JFrame {
JTextField tf,tf1;
public TestMain() {
super("Copy");
JButton btn = new JButton("Copy");
tf = new JTextField(19);
tf1 = new JTextField(20);
tf1.setEditable(false); this.getContentPane().add(btn, BorderLayout.SOUTH);
this.getContentPane().add(tf, BorderLayout.WEST);
this.getContentPane().add(tf1, BorderLayout.EAST);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
this.setSize(450, 300);
this.setVisible(true);
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String[] s = tf.getText().split("\n");
for (int i=0; i <s.length; i++) {
tf1.setText((i+1) + " " + s[i] + "\n");
}
}
}); } public static void main(String[] args) {
new TestMain();
}
}
仅供参考 楼主