实现的代码
package Homework; import javax.swing.*; import java.io.*; public class HomeworkFive { public static void main(String[] args) { new HomeworkFive().init(); } Jframe jf=new Jframe("第五次作业"); Jtextarea jta=new Jtextarea(20,60); JButton jb1=new JButton("读取文件"); JButton jb2=new JButton("写入文件"); JFileChooser jfc=new JFileChooser("."); public void init(){ jb1.addActionListener(e -> { jfc.showOpenDialog(jf); File selectedFile = jfc.getSelectedFile(); try { BufferedReader bis=new BufferedReader(new FileReader(selectedFile)); char[] c=new char[1024]; int i=0; while ((i=bis.read(c))!=-1){ jta.append(new String(c,0,i)); } bis.close(); } catch (IOException o) { o.printStackTrace(); } }); jb2.addActionListener(e->{ jfc.showSaveDialog(jf); File selectedFile = jfc.getSelectedFile(); try { BufferedWriter bis=new BufferedWriter(new FileWriter(selectedFile)); bis.write(jta.getText()); bis.close(); } catch (IOException o) { o.printStackTrace(); } }); Box box1 = Box.createHorizontalBox(); Box box2 = Box.createVerticalBox(); box1.add(jb1); box1.add(jb2); box2.add(new JScrollPane(jta)); box2.add(box1); jf.add(box2); jf.setVisible(true); jf.pack(); jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } }
实现效果:
自己不太会写,请教了同学做出来的
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)