正文

记事本(未完全完成)打开功能完成2006-05-26 09:25:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/sword2008/14876.html

分享到:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.*;
import java.io.*;
import java.util.*;
import java.awt.Color;
import java.lang.*;
import java.text.*; 
import java.util.*;
import javax.accessibility.*;
import java.awt.datatransfer.*;

public class Sword_Code{
 public static void main(String args[]){
  new TextGUI();
 }
}

class TextGUI extends JFrame{
//############################################################private变量
 private String open;
 private String xinjianS;
 JTextArea textArea;
 JMenuItem setcolor;
 JColorChooser jColor=new JColorChooser();
 JFileChooser jFileChooser = new JFileChooser();
 
//_________________________________________________________________________________
 TextGUI(){
  super("记事本");
  Container container=getContentPane();
 
  container.setLayout(new FlowLayout());

//***************JMenu所有***************** 
  JMenuBar bar=new JMenuBar();
  setJMenuBar(bar);
  JMenu fileMenu=new JMenu("File");
  fileMenu.setMnemonic('F');
 
  JMenu aboutMenu=new JMenu("About");
  aboutMenu.setMnemonic('A');
 
  JMenu windowface=new JMenu("Windows");
  windowface.setMnemonic('W');
 
  JMenu editJMenu=new JMenu("Edit");
  editJMenu.setMnemonic('e');
 
  JMenu formatMenu=new JMenu("格式");
  JMenu settingMenu=new JMenu("设置");
 
  JToolBar toolbar=new JToolBar();
 
//_______________________________________________________________________________
//--------------------------------------------------------JButton工具类
JButton newbutton=new JButton(new ImageIcon("images/new.gif"));
JButton openbutton=new JButton(new ImageIcon("images/open.gif"));
JButton savebutton=new JButton(new ImageIcon("images/save.gif"));
JButton saveasbutton=new JButton(new ImageIcon("images/saveas.gif"));
JButton cutbutton=new JButton(new ImageIcon("images/cut.gif"));
JButton copybutton=new JButton(new ImageIcon("images/copy.gif"));
JButton pastebutton=new JButton(new ImageIcon("images/paste.gif"));
JButton undobutton=new JButton(new ImageIcon("images/undo.gif"));
JButton findbutton=new JButton(new ImageIcon("images/find.gif"));
//-_______________________________________________________________________________
 
//****************JTextArea************* 
  JScrollPane roll=new JScrollPane();
  textArea=new JTextArea(100,93);
  roll.getViewport().add(textArea);
  textArea.setLineWrap(false);
  container.add(roll);
//_______________________________________________________________________________

//*********************JMenuBar ********************
  bar.add(fileMenu);
  bar.add(aboutMenu);
  bar.add(editJMenu);
  bar.add(windowface);
  bar.add(formatMenu);
  bar.add(toolbar);
//添加按钮到工具条
toolbar.add(newbutton);
toolbar.addSeparator();
toolbar.add(openbutton);
toolbar.add(savebutton);
toolbar.add(saveasbutton);
toolbar.addSeparator();
toolbar.add(cutbutton);
toolbar.add(copybutton);
toolbar.add(pastebutton);
toolbar.addSeparator();
toolbar.add(undobutton);
toolbar.addSeparator();
toolbar.add(findbutton);
   
//_______________________________________________________________________________
 
//********************  JMenultem**************************************
JMenuItem openItem=new JMenuItem("打开");//第一个打开
JMenuItem xinjianItem=new JMenuItem("保存");//
JMenuItem exitItem=new JMenuItem("退出");

JRadioButtonMenuItem javaface=new JRadioButtonMenuItem("Java外观感觉");
JRadioButtonMenuItem motifface=new JRadioButtonMenuItem("Motif外观感觉");
JRadioButtonMenuItem windowsface=new JRadioButtonMenuItem("Windows外观感觉");
final JCheckBoxMenuItem autonewline=new JCheckBoxMenuItem("自动换行");

//编辑
JMenuItem textcopy=new JMenuItem("复制");
JMenuItem textcut =new JMenuItem("剪切");
JMenuItem textpaste=new JMenuItem("粘贴");
JMenuItem textdelete=new JMenuItem("删除");
//格式
JMenuItem textdelete1=new JMenuItem("清除");
JMenuItem setfont=new JMenuItem("字体");
setcolor=new JMenuItem("颜色");
JMenuItem checkall=new JMenuItem("全选");
JMenuItem timeanddate=new JMenuItem("时间/日期");

//_______________________________________________________________________________

//-------------------------------add JRadioButtonMenuItem
  fileMenu.add(openItem);
  fileMenu.add(xinjianItem);
  fileMenu.addSeparator();
  windowface.add(javaface);
  windowface.add(motifface);
  windowface.add(windowsface);
  settingMenu.add(autonewline);
 
  fileMenu.add(exitItem);
  editJMenu.add(textcopy);
  editJMenu.add(textcut);
  editJMenu.add(textpaste);
  editJMenu.add(textdelete);
  formatMenu.add(textdelete1);
//格式
  formatMenu.add(settingMenu);
  formatMenu.add(checkall);
  formatMenu.addSeparator();
  formatMenu.add(setfont);
  formatMenu.add(setcolor);
  formatMenu.addSeparator();
  formatMenu.add(timeanddate);
 
//_______________________________________________________________________________


//*********EditJMenu*******************************

  pack();
  show();
  setVisible(true);
 
  setBounds(200,200,600,450);
//#############################################################Item-listener动作监听
 openItem.addActionListener(new ActionListener(){ 
 public void actionPerformed(ActionEvent event){
   FileOpen();}
   });
 
 
 
  xinjianItem.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent event){
    fileSave();
   }
  });
 
  this.addWindowListener(new WindowAdapter(){
   public void windowClosing(WindowEvent e){
    System.exit(0);
   }
   });
//------------------------------------------------------------------------------------

//--------------------------------------------------********退出exitItem的监听类
  exitItem.addActionListener(new ActionListener(){
     public void actionPerformed(ActionEvent event){
      if(textArea.getText().equals(""))    //=true的时候文本无字
      System.exit(0);
      else
      JOptionPane.showMessageDialog(null,"退出有误","错误",JOptionPane.INFORMATION_MESSAGE);
     }
  });

//--------------------------------------------------------------------------------------------
//--------------------------------------------------********自动换行autonewline的监听
   autonewline.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent event){
      if(autonewline.getState()==false)
  textArea.setLineWrap(true);
     else
  textArea.setLineWrap(false);
    }
    });

//清除监听
 textdelete1.addActionListener(new ActionListener(){
  public void actionPerformed(ActionEvent event){
   textArea.setText("");
  }
 });
 
//时间
timeanddate.addActionListener(new ActionListener(){
 public void actionPerformed(ActionEvent event){
  FileTime(event);
  }
 });
//颜色
 setcolor.addActionListener(new ActionListener(){
  public void actionPerformed(ActionEvent e){
   fcolor(e);

}
  }
 ); 
 


//--------------------------------------------------------------------------------------
 }
//--------------------------------------------------------------------------方法
//打开
void FileOpen()
{ int result=jFileChooser.showOpenDialog(this);
 
  String currentFilePath = null;
  File file = null;
  String currentFileName = null;
 
  if(result == JFileChooser.APPROVE_OPTION){
   
   try
   { currentFilePath = jFileChooser.getSelectedFile().getPath();
    file = new File(currentFilePath); 

    FileInputStream aboutFileInputStream=new FileInputStream(file);
    InputStreamReader aboutInputStream=new InputStreamReader(aboutFileInputStream);
    BufferedReader aboutBuffer=new BufferedReader(aboutInputStream);
   
    String c;String output="";
     while((c=aboutBuffer.readLine())!=null){
      output+=c;
     }
     textArea.setText(output);
     aboutBuffer.close();

 }
    catch(NullPointerException e){
     System.out.println(e.toString());
     }
    catch(FileNotFoundException e){
     System.out.println(e.toString());
     }
    catch(IOException e){
     System.out.println(e.toString());
  }
}
  if(result == JFileChooser.CANCEL_OPTION){
   
  }
}
//保存
void fileSave(){
 int result=jFileChooser.showSaveDialog(this);
 
  String currentFilePath = null;
  File file = null;
 
  if(result == JFileChooser.APPROVE_OPTION){
   
   try
   { currentFilePath = jFileChooser.getSelectedFile().getPath();
    file = new File(currentFilePath); 
   
    FileWriter fw=new FileWriter(file,true);
   BufferedWriter bw=new BufferedWriter(fw);
      bw.write(textArea.getText());
      bw.newLine();
      bw.close();
   
 }
    catch(NullPointerException e){
     System.out.println(e.toString());
     }
    catch(FileNotFoundException e){
     System.out.println(e.toString());
     }
    catch(IOException e){
     System.out.println(e.toString());
  }
}
  if(result == JFileChooser.CANCEL_OPTION){
   
  }
 
}
//时间
 void FileTime(ActionEvent e){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
textArea.append("\r\n当前时间:"+sdf.format(new Date()));
}
//颜色
public void fcolor(ActionEvent e){
Color bcolor=textArea.getForeground();
  jColor.setColor(bcolor);
  textArea.setForeground(
  jColor.showDialog(textArea,"选择颜色",bcolor)); 
  }
//________________________________________________________________________________________
  public void KeyTyped(KeyEvent e){
  if(e.getKeyChar()=='q')
  JOptionPane.showInputDialog("输入文件地址");
  else
  JOptionPane.showInputDialog("34114");
 }
 public void KeyPressed(KeyEvent e){
 }
 public void KeyReleased(KeyEvent e){
 }
}

阅读(787) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册