熟悉java界面编程和IO操作的小例子

时间:2021-08-03 10:01:19
使用java做个小型的 记事本软件,主要是为了熟悉java的GUI编程,和java文件的操作,主要代码如下:
package com.hemin.fileedit;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FileDialog;
import java.awt.FlowLayout;
import java.awt.ScrollPane;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class Fileedit extends JFrame{

private JTextField selectFiled;
private JTextArea editArea;
private JButton saveButton;
private JButton openButton;
private int leavel = 0;

//构造方法
public Fileedit(){
this.init();
}
//初始化函数
private void init() {
this.setTitle("Editor");
this.setBounds(300, 50, 600, 650);
selectFiled = new JTextField(40);
//打开文件按钮
openButton = new JButton("Browse");
/*
* 为打开按钮添加监听事件
*/
openButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Fileedit.this.leavel = 1;
String path = selectFiled.getText();
openDirorFile(path.replace("//", "\\\\"));

}
});



//此处新建一个流布局
JPanel jpanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
jpanel.setBackground(Color.gray);
jpanel.add(selectFiled);
jpanel.add(openButton);
this.add(jpanel,BorderLayout.NORTH);
editArea = new JTextArea();

//创建滚动面板
ScrollPane scrollPane = new ScrollPane(ScrollPane.SCROLLBARS_AS_NEEDED);
scrollPane.add(editArea);
this.add(scrollPane , BorderLayout.CENTER);

saveButton = new JButton("save");
/*
* 此处需添监听事件
*/
saveButton.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
saveFile();
}
});
JPanel southJPanel = new JPanel();
southJPanel.setBackground(Color.gray);
southJPanel.add(saveButton);
this.add(southJPanel , BorderLayout.SOUTH);
//常规设置(退出和可视化)
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);

}

private void openDirorFile(String absolutePath){
File file = new File(absolutePath);
if(!(file.exists())){
editArea.setText("文件不存在");
}else if(file.isDirectory()){
editArea.setText(null);
showDir(file);
}else if(file.isFile()){
try {
FileInputStream fis = new FileInputStream(file);
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
String str = null;
editArea.setText(null);
while((str = br.readLine())!=null){
editArea.append(str+"\r\n");
}
br.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}

//展示列表
private void showDir(File directory)
{
File[] files = directory.listFiles();
int len = files.length;
for(int i = 0; i < len; i++)
{
if(files[i].isDirectory())
{
for(int j = 0; j < this.leavel; j++)
{
editArea.append(" ");
}
editArea.append("|-- "+ files[i].getName() + " (Folder)\r\n");
this.leavel++;
showDir(files[i]);
this.leavel--;
}
else if(files[i].isFile())
{
for(int j = 0; j < this.leavel; j++)
{
editArea.append(" ");
}
editArea.append("|-- " + files[i].getAbsolutePath() + "\r\n");
}
}
}

//
private void saveFile(){
//文件保存框的练习
FileDialog fd = new FileDialog(this, "Save File");
fd.setFile("untitled.txt");
fd.setMode(FileDialog.SAVE);
fd.setVisible(true);
String fileName = fd.getFile();
String dir = fd.getDirectory();
File newFile = new File(dir+File.separator+fileName);
PrintWriter pw =null;
try {
pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream(newFile)));
String str = editArea.getText();
pw.println(str);
pw.flush();
} catch (Exception e) {
e.printStackTrace();
}finally{
pw.close();
}

}

//主函数
public static void main(String args[]){
new Fileedit();
}
}
熟悉java界面编程和IO操作的小例子