首页上一页 1 下一页尾页 4 条记录 1/1页
《JAVA从入门到精通》问题
发表在Java图书答疑
2009-05-11
是否精华
是
否
版块置顶:
是
否
下面是在《JAVA从入门到精通》259页实例,我编的有点问题,请帮忙指点一下。
package com.d;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
public class Ftest extends JFrame {
JTextArea jt=new JTextArea(10,10);
public Ftest() {
Container c=getContentPane();
setSize(500, 300);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setTitle("JFrame");
setLayout(null);
jt.setBounds(10, 10, 300, 80);
JButton jb1=new JButton("写入文件");
jb1.setBounds(40, 100, 100, 40);
JButton jb2=new JButton("读取文件");
jb2.setBounds(200, 100, 100, 40);
jb1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
File file=new File("D:/myword");
try{
FileWriter out=new FileWriter(file);
String s=jt.getText();
out.write(s);
out.close();
}catch(Exception e){
e.printStackTrace();
}
}
});
jb2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
File file=new File("D:/myword");
try{
FileReader in=new FileReader(file);
byte bte[]=new byte[1024];
int len=bte.length;
System.out.println(new String(bte,0,len));
in.close();
}catch(Exception e){
e.printStackTrace();
}
}
});
c.add(jt);
c.add(jb1);
c.add(jb2);
}
public static void main(String[] args) {
new Ftest();
}
}
package com.d;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
public class Ftest extends JFrame {
JTextArea jt=new JTextArea(10,10);
public Ftest() {
Container c=getContentPane();
setSize(500, 300);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setTitle("JFrame");
setLayout(null);
jt.setBounds(10, 10, 300, 80);
JButton jb1=new JButton("写入文件");
jb1.setBounds(40, 100, 100, 40);
JButton jb2=new JButton("读取文件");
jb2.setBounds(200, 100, 100, 40);
jb1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
File file=new File("D:/myword");
try{
FileWriter out=new FileWriter(file);
String s=jt.getText();
out.write(s);
out.close();
}catch(Exception e){
e.printStackTrace();
}
}
});
jb2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
File file=new File("D:/myword");
try{
FileReader in=new FileReader(file);
byte bte[]=new byte[1024];
int len=bte.length;
System.out.println(new String(bte,0,len));
in.close();
}catch(Exception e){
e.printStackTrace();
}
}
});
c.add(jt);
c.add(jb1);
c.add(jb2);
}
public static void main(String[] args) {
new Ftest();
}
}