- package abc.jdm;
- import java.awt.*;
- import javax.swing.*;
- import java.io.*;
- import java.awt.event.*;
- public class MyNotepad extends JFrame{
- private JTextField filenameTF=new JTextField(),dirTF=new JTextField();
- private JButton openbutton=new JButton("open"),savebutton=new JButton("save");
- private JPanel optpane=new JPanel(),navigatepane=new JPanel();
- private JTextArea contentta=new JTextArea(5,20);
- public MyNotepad(String title){
- super(title);
- openbutton.addActionListener(new openhandeler());
- savebutton.addActionListener(new savehandeler());
- optpane.add(openbutton);
- optpane.add(savebutton);
- dirTF.setEditable(false);
- filenameTF.setEditable(false);
- navigatepane.setLayout(new GridLayout(2,1));
- navigatepane.add(filenameTF);
- navigatepane.add(dirTF);
- Container contentpane=getContentPane();
- contentpane.add(optpane,BorderLayout.SOUTH);
- contentpane.add(navigatepane,BorderLayout.NORTH);
- contentpane.add(new JScrollPane(contentta),BorderLayout.CENTER);
- setSize(500,300);
- setVisible(true);
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- }
- //自定义定义打开事件类.
- class openhandeler implements ActionListener{
- public void actionPerformed(ActionEvent e){
- JFileChooser jfilechooser = new JFileChooser();
- int result=jfilechooser.showOpenDialog(MyNotepad.this);
- if(result==JFileChooser.APPROVE_OPTION){
- File dir=jfilechooser.getCurrentDirectory();
- File file=jfilechooser.getSelectedFile();
- filenameTF.setText(file.getName());
- dirTF.setText(dir.toString());
- contentta.setText(read(new File(dir,file.getName())));
- }
- if(result==JFileChooser.CANCEL_OPTION)
- dirTF.setText("");
- }
- }
- //自定义保存文件的事件.
- class savehandeler implements ActionListener{
- public void actionPerformed(ActionEvent e){
- JFileChooser jfilechooser=new JFileChooser();
- int result=jfilechooser.showSaveDialog(MyNotepad.this);
- if(result==JFileChooser.APPROVE_OPTION){
- File dir=jfilechooser.getCurrentDirectory();
- File file=jfilechooser.getSelectedFile();
- filenameTF.setText(file.getName());
- dirTF.setText(dir.toString());
- write(new File(dir,file.getName()),contentta.getText());
- }
- if(result==JFileChooser.CANCEL_OPTION)
- dirTF.setText("");
- }
- }
- /**
- * @param args
- */
- //读文件。
- private String read(File file){
- try{
- BufferedReader read=new BufferedReader(new InputStreamReader(new FileInputStream(file),"GBK"));
- String data=null;
- StringBuffer buffer=new StringBuffer();
- while((data=read.readLine())!=null)
- buffer.append(data+"/n");
- read.close();
- return buffer.toString();
- }
- catch(IOException e){
- throw new RuntimeException(e);
- }
- }
- //保存文件.
- public void write(File file,String str){
- try{
- PrintWriter write=new PrintWriter(new OutputStreamWriter(new FileOutputStream(file),"GBK"));
- write.println(str);
- write.close();
- }
- catch(IOException e){
- throw new RuntimeException(e);
- }
- }
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- new MyNotepad("记事本");
- }
- }
java savedialog1
最新推荐文章于 2022-04-06 09:51:33 发布