结构分析:
- 界面布局 : EditFrame
- main方法所在: EditText
- 打开功能实现: FileReadThread
- 保存跟能实现: save
实际运行效果:
附:完整代码实现
一、 EditFrame
包括一个菜单Menu
底部:日期时间
代码附上:
代码语言:javascript复制public class EditFrame extends JFrame {
// TODO 自动生成的构造函数存根
boolean saveFlag = false;
File saveFileRoot = null;
JFrame jFrame;
JPanel jPanelSouth;
JMenuBar jMenuBar1;
JMenu jMenu1;
JMenuItem jMenuItem1;
JMenuItem jMenuItem2;
JMenuItem jMenuItem3;
JMenuItem jMenuItem4;
JSeparator jSeparator1;
JTextArea jTextArea;
JScrollPane scrollPane;// 滚动条
public EditFrame() {
// TODO 自动生成的构造函数存根
jFrame = new JFrame("水中鱼之1999-文本编辑器");
jPanelSouth = new JPanel();
jMenuBar1 = new JMenuBar();
jMenu1 = new JMenu("文件");
jMenuItem1 = new JMenuItem("打开");
jMenuItem2 = new JMenuItem("保存");
jMenuItem3 = new JMenuItem("另存为");
jMenuItem4 = new JMenuItem("退出");
jSeparator1 = new JSeparator();
jTextArea = new JTextArea();
scrollPane = new JScrollPane(jTextArea);
jFrame.setSize(800, 500);
jFrame.setLocationRelativeTo(null);
jFrame.setVisible(false);
setLayout();
setSouthPanel();
// set relationship for your component
setRelationShip();
// 设置 scrollPane for TextArea
setScscrollPane();
iniClick();
}
private void setRelationShip() {
jFrame.add(BorderLayout.CENTER, scrollPane);
jFrame.add(BorderLayout.SOUTH, jPanelSouth);
jMenu1.add(jMenuItem1);
jMenu1.add(jMenuItem2);
jMenu1.add(jMenuItem3);
jMenu1.add(jSeparator1);
jMenu1.add(jMenuItem4);
jMenuBar1.add(jMenu1);
jFrame.setJMenuBar(jMenuBar1);
}
private void setLayout() {
GridLayout gridLayout = new GridLayout(1, 2);
jPanelSouth.setLayout(gridLayout);
}
private void setScscrollPane() {
// jTextArea.setLineWrap(true);// 设置满一行自动换行
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
}
private void setSouthPanel() {
// add time for SouthPanel
JLabel jLabelDate = new JLabel("Date");
JLabel jLabelTime = new JLabel("Time");
Timer timeAction = new Timer(1000, new ActionListener() {
public void actionPerformed(ActionEvent e) {
long timemillis = System.currentTimeMillis();
// 转换日期显示格式
SimpleDateFormat date = new SimpleDateFormat("yyyy 年 MM 月 dd 日 ");
jLabelDate.setText(" 当前日期: " date.format(new Date(timemillis)));
SimpleDateFormat time = new SimpleDateFormat("HH:mm:ss ");
jLabelTime.setText(" 当前时间: " time.format(new Date(timemillis)));
}
});
jPanelSouth.add(jLabelDate);
jPanelSouth.add(jLabelTime);
timeAction.start();
}
private void iniClick() {
jFrame.addWindowListener(new WindowListener() {
@Override
public void windowOpened(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowIconified(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowDeiconified(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowDeactivated(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowClosing(WindowEvent e) {
// TODO Auto-generated method stub
int x = JOptionPane.showConfirmDialog(null, "确认退出么?", "友情提示", JOptionPane.OK_CANCEL_OPTION,
JOptionPane.WARNING_MESSAGE);
if (x == 0) {
System.exit(0);
}
}
@Override
public void windowClosed(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowActivated(WindowEvent e) {
// TODO Auto-generated method stub
}
});
jMenuItem4.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
int x = JOptionPane.showConfirmDialog(null, "确认退出么?", "友情提示", JOptionPane.OK_CANCEL_OPTION,
JOptionPane.WARNING_MESSAGE);
if (x == 0) {
System.exit(0);
}
}
});
jMenuItem1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
FileReadThread fileReadThread = new FileReadThread(EditFrame.this);// 开启文件读取线程
fileReadThread.start();
System.out.println(saveFileRoot);
saveFlag = true;
jTextArea.setText("");
}
});
jMenuItem3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
Save save = new Save(EditFrame.this);
save.start();
saveFlag = true;
}
});
jMenuItem2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (!saveFlag) {
Save save = new Save(EditFrame.this);
save.start();
saveFlag = true;
} else {
new Save(EditFrame.this, saveFileRoot);
}
}
});
}
public JTextArea getjTextArea() {
return jTextArea;
}
public void setjTextArea(JTextArea jTextArea) {
this.jTextArea = jTextArea;
}
public File getSaveFileRoot() {
return saveFileRoot;
}
public void setSaveFileRoot(File saveFileRoot) {
this.saveFileRoot = saveFileRoot;
}
public JFrame getjFrame() {
return jFrame;
}
public void setjFrame(JFrame jFrame) {
this.jFrame = jFrame;
}
}
二、测试类 EditText
分两部分:
1.闪屏 由于加载页面需要时间 原想用 SplashScreen 由于打包成jar包才能用所以这里用jframe进行替代
闪屏图片直接粘贴到:
2.new 一个EditFrame 对象, 闪屏结束后设置为可见
代码语言:javascript复制public class EditText {
public static void main(String[] args) {
new Thread() {
@Override
public void run() {
// TODO Auto-generated method stub
EditFrame editFrame = new EditFrame();
JFrame jFrame = new JFrame();
JPanel jPanel = new javax.swing.JPanel(){
protected void paintComponent(java.awt.Graphics g){
super.paintComponent(g);
g.drawImage(new ImageIcon("experiment_bac.jpg").getImage(),0,0,400,250,null);
}
};
jFrame.add(jPanel);
jFrame.setVisible(true);
jFrame.setSize(400, 300);
jFrame.setLocationRelativeTo(null);
try {
sleep(1500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
jFrame.dispose();
editFrame.getjFrame().setVisible(true);
}
}.start();
}
}
三、FileReadThread
调用swing自带的 JFileChooser
选择文件路径
代码语言:javascript复制class FileReadThread extends Thread {
private EditFrame test;
public FileReadThread(EditFrame test ) {
this.test = test;
}
@Override
public void run() {
JFileChooser chooser = new JFileChooser("d:/");
chooser.setFileFilter(new FileFilter() {// 定义文件过滤器,仅显示文件夹和txt文本
@Override
public String getDescription() {
return null;
}
@Override
public boolean accept(File file) {
if (file.isDirectory() || file.getName().endsWith(".txt"))
return true;
return false;
}
});
int option = chooser.showOpenDialog(test);
if (option == JFileChooser.APPROVE_OPTION) {
File selFile = chooser.getSelectedFile();
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(selFile), "gbk"));
test.setSaveFileRoot(selFile);
String line = null;
while ((line = reader.readLine()) != null) {
test.getjTextArea().append(line "n");
Thread.sleep(30);// 线程暂停,以看到读取过程效果
}
} catch (Exception e1) {
e1.printStackTrace();
}
JOptionPane.showMessageDialog(test, "读取完毕");
}
}
}
四、save 保存
保存的调用分三种:
- 如果该文件是打开的 那么保存到打开文件中
- 如果该文件还未保存 这调用:进行保存
- 如果该文件已经另存为,则直接保存到另存为得的目录下
public class Save extends Thread {
private EditFrame area;
private File saveFileRoot = null;
public Save(EditFrame area, File saveFileRoot) {
System.out.println(saveFileRoot "123");
String text = area.getjTextArea().getText();
String[] lines = text.trim().split("n");
try {
PrintWriter out = new PrintWriter(new FileOutputStream(saveFileRoot), true);
for (String line : lines)
out.println(line);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public Save(EditFrame area) {
this.area = area;
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new File("."));
chooser.setFileFilter(new FileFilter() {
@Override
public boolean accept(File f) {
return f.getName().toLowerCase().endsWith(".txt");
}
@Override
public String getDescription() {
return "SAVE TO";
}
});
int r = chooser.showSaveDialog(area);
if (r != JFileChooser.APPROVE_OPTION)
return;
File f = chooser.getSelectedFile();
area.setSaveFileRoot(f);
String text = area.getjTextArea().getText();
String[] lines = text.trim().split("n");
try {
PrintWriter out = new PrintWriter(new FileOutputStream(f), true);
for (String line : lines)
out.println(line);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public File getSaveFileRoot() {
return saveFileRoot;
}
public void setSaveFileRoot(File saveFileRoot) {
this.saveFileRoot = saveFileRoot;
}
}