java把txt的内容显示在textarea中
area1=newTextArea("");area1.setBounds(10,70,560,190);add(area1);a1=newJButton("ShowBo...
area1=new TextArea("");
area1.setBounds(10,70,560,190);
add(area1);
a1 = new JButton("Show Book");
a1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
File file = new File("c:/books.txt");
if (file.exists() && file.isFile())
{
try {
BufferedReader input = new BufferedReader(new FileReader(file));
String text;
while ((text = input.readLine()) != null)
area1.setText(area1.getText() + text + "\n");
}
catch (IOException ioException)
{
System.err.println("File Error!");
}
}
}
});
add(a1);
可是好像那个按钮的功能还是没有实现,能帮忙看下错在哪吧,谢谢了。 展开
area1.setBounds(10,70,560,190);
add(area1);
a1 = new JButton("Show Book");
a1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
File file = new File("c:/books.txt");
if (file.exists() && file.isFile())
{
try {
BufferedReader input = new BufferedReader(new FileReader(file));
String text;
while ((text = input.readLine()) != null)
area1.setText(area1.getText() + text + "\n");
}
catch (IOException ioException)
{
System.err.println("File Error!");
}
}
}
});
add(a1);
可是好像那个按钮的功能还是没有实现,能帮忙看下错在哪吧,谢谢了。 展开
2个回答
展开全部
我已经帮你修改了,你直接拿去运行就好了。
======================================
package myswing;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFrame;
/**
* @作者 x40401
* @创建日期 Dec 23, 2010
*/
public class Test extends JFrame {
public Test() {
super();
final TextArea area1 = new TextArea("");
area1.setBounds(10, 70, 560, 190);
add(area1);
JButton a1 = new JButton("Show Book");
a1.setBounds(70, 300, 100, 40);
a1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
File file = new File("e:/books.txt");
if (file.exists() && file.isFile()) {
try {
BufferedReader input = new BufferedReader(
new FileReader(file));
String text;
while ((text = input.readLine()) != null)
area1.setText(area1.getText() + text + "\n");
} catch (IOException ioException) {
System.err.println("File Error!");
}
}
}
});
add(a1);
setLayout(null);
}
public static void main(String[] args) {
Test MS = new Test();
MS.setBounds(200, 100, 800, 600);
MS.setVisible(true);
}
}
======================================
package myswing;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFrame;
/**
* @作者 x40401
* @创建日期 Dec 23, 2010
*/
public class Test extends JFrame {
public Test() {
super();
final TextArea area1 = new TextArea("");
area1.setBounds(10, 70, 560, 190);
add(area1);
JButton a1 = new JButton("Show Book");
a1.setBounds(70, 300, 100, 40);
a1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
File file = new File("e:/books.txt");
if (file.exists() && file.isFile()) {
try {
BufferedReader input = new BufferedReader(
new FileReader(file));
String text;
while ((text = input.readLine()) != null)
area1.setText(area1.getText() + text + "\n");
} catch (IOException ioException) {
System.err.println("File Error!");
}
}
}
});
add(a1);
setLayout(null);
}
public static void main(String[] args) {
Test MS = new Test();
MS.setBounds(200, 100, 800, 600);
MS.setVisible(true);
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询