熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java核心技術 >> 正文

Jjava一個“記事本”小程序

2013-11-23 19:24:01  來源: Java核心技術 

  package NoteBook;

  import javaxswingJFrame;

  import javaawt*;

  import javaawteventActionEvent;

  import javaawteventActionListener;

  import javaxswingJButton;

  import javaxswingJCheckBox;

  import javaxswingJDialog;

  import javaxswingJFileChooser;

  import javaxswingJLabel;

  import javaxswingJList;

  import javaxswingJMenuBar;

  import javaxswingJMenu;

  import javaxswingJMenuItem;

  import javaxswingJOptionPane;

  import javaxswingJPanel;

  import javaxswingJPopupMenu;

  import javaxswingJScrollBar;

  import javaxswingJScrollPane;

  import javaxswingJTextArea;

  import javaxswingJTextField;

  import javaxswingScrollPaneLayout;

  import javaxswingfilechooserFileSystemView;

  import apachexmlinternalserializePrinter;

  import javaio*;

  import javatextSimpleDateFormat;

  import javautil*;

  import javautilList;

  public class NoteBook implements ActionListener {

  JFrame jf;

  JTextArea jta;

  JMenuItem[][] jmi;

  myStack ms=new myStack();

  public NoteBook(){

  jf=new JFrame(記事本);

  JMenuBar jmb=new JMenuBar();

  jta=new JTextArea();

  jfadd(jta);

  mspush(jtagetText());

  JScrollPane jsp=new JScrollPane(jta);

  jfadd(jsp);

  String[] str={文件編輯格式幫助};

  String[][] str={{新建打開保存另存頁面設置打印退出}

  {撤銷剪貼拷貝粘貼查找替換刪除時間}

  {字體}

  {幫助主題關於記事本}};

  JMenu[] jm=new JMenu[strlength];

  

  //   jmi=new JMenuItem[strlength][];//注意在這裡定義有空指針異常!

  for(int i=;i<strlength;i++){

  jm[i]=new JMenu(str[i]);

  Systemoutprintln(jm[i]);

  jmbadd(jm[i]);

  }

  for(int i=;i<strlength;i++){

  jmi=new JMenuItem[strlength][str[i]length];

  for(int j=;j<str[i]length;j++){

  if(equals(str[i][j])){

  jm[i]addSeparator();//添加分割線

  }else {

  jmi[i][j]=new JMenuItem(str[i][j]);

  jm[i]add(jmi[i][j]);

  jmi[i][j]addActionListener(this);

  }

  }

  }

  jfadd(jmbBorderLayoutNORTH);//注意需要注明其放的位置不然會在左邊

  jfsetVisible(true);

  jfpack();

  Toolkit tk=ToolkitgetDefaultToolkit();

  jfsetLocation((tkgetScreenSize()widthjfgetSize()width)/(tkgetScreenSize()heightjfgetSize()height)/);

  jfsetDefaultCloseOperation(JFrameEXIT_ON_CLOSE);

  }

  public JTextArea getJTextArea(){

  return thisjta;

  }

  public JFrame getJFrame(){

  return thisjf;

  }

  public void actionPerformed(ActionEvent e) {

  // TODO Autogenerated method stub

  String str=egetActionCommand();

  if(新建equals(str)){

  new NoteBook();

  }else if (打開equals(str)){

  JFileChooser jfc=new JFileChooser();

  jfcshowOpenDialog(null);//注意在這裡的null表示對話框彈出的位置

  if(jfcshowOpenDialog(null)==JFileChooserAPPROVE_OPTION){

  File selectfile=jfcgetSelectedFile();

  try {

  FileReader reader=new FileReader(selectfile);

  Scanner sc=new Scanner(reader);

  while(schasNextLine()){

  String line=scnextLine();

  jtasetText(line);

  }

  

  try {

  readerclose();//////////////////////閱讀器需要關閉嗎?///////////////////

  } catch (IOException e) {

  // TODO Autogenerated catch block

  eprintStackTrace();

  }

  } catch (FileNotFoundException e) {

  // TODO Autogenerated catch block

  eprintStackTrace();

  }

  }

  if(jfcshowOpenDialog(null)==JFileChooserCANCEL_OPTION){

  ///////////////////////處理/////////////////////////////

  javaxswingJOptionPaneshowMessageDialog(jfc你已經取消了);

  }

  }else if(保存equals(str)){

  JFileChooser jfc=new JFileChooser();

  jfcshowSaveDialog(null);//注意在這裡的null表示對話框彈出的位置

  if(jfcshowOpenDialog(null)==JFileChooserAPPROVE_OPTION){

  File selectfile=jfcgetSelectedFile();

  try {

  PrintWriter pw=new PrintWriter(selectfile);//往文件裡寫

  pwprint(jtagetText());

  pwclose();//關閉///////////////////////////////////////////

  } catch (FileNotFoundException e) {

  // TODO Autogenerated catch block

  eprintStackTrace();

  }

  }

  if(jfcshowOpenDialog(null)==JFileChooserCANCEL_OPTION){

  ///////////////////////////////////添加//////////////////////////

  javaxswingJOptionPaneshowMessageDialog(jfc你已經取消了);

  }

  }else if(另存equals(str)){

  JOptionPane jop=new JOptionPane();

  String filePath=jopshowInputDialog(請輸入路徑(型如C:/savetxt));//直接返回的是用戶輸入的信息!

  //    jopgetInputValue();//注意這種的到用戶的輸入是不對的

  String str=^[cdefCDEF]:/[AZaZ]{}txt$;//路徑正則表達式

  Systemoutprintln(filePath);

  if(filePathmatches(str)){//特別注意了政則表達式在後面

  javaxswingJOptionPaneshowMessageDialog(jop文件路徑真確);

  }else {

  javaxswingJOptionPaneshowMessageDialog(jop文件路徑不對);

  }

  PrintWriter pw;

  try {

  pw = new PrintWriter(filePath);

  pwprint(jtagetText());

  pwclose();//關閉//////////////////////////

  } catch (FileNotFoundException e) {

  // TODO Autogenerated catch block

  eprintStackTrace();

  }//往文件裡寫

  }else if(頁面設置equals(str)){

  javaxswingJOptionPaneshowMessageDialog(jf沒有打印機);

  }else if(打印equals(str)){

  javaxswingJOptionPaneshowMessageDialog(jf沒有打印機);

  }else if(退出equals(str)){

  jfdispose();//注意這是退出

  }else if(撤銷equals(str)){

  String str=mspop();//出堆棧

  jtasetText(str);

  }else if(剪貼equals(str)){

  mspush(jtagetText());//入堆棧

  jtacut();

  

  }else if(拷貝equals(str)){

  py();

  }else if(粘貼equals(str)){

  mspush(jtagetText());//入堆棧

  jtapaste();

  }else if(查找equals(str)){

  String str=JOptionPaneshowInputDialog(jf請輸入查找內容);

  StringBuffer strbuf=new StringBuffer(jtagetText());

  int temp=strbufindexOf(str);

  jtaselect(temp (temp+strlength()));

  }else if(替換equals(str)){

  mspush(jtagetText());//入堆棧

  exchange myExchange=new exchange(this);

  }else if(刪除equals(str)){

  mspush(jtagetText());//入堆棧

  String str=jtagetSelectedText();

  StringBuffer str=new StringBuffer(jtagetText());

  strdelete(strindexOf(str) strindexOf(str)+strlength());

  str=new String(str);

  jtasetText(str);

  }else if(時間equals(str)){

  mspush(jtagetText());//入堆棧

  Date dt=new Date();

  SimpleDateFormat formater=new SimpleDateFormat();

  formaterapplyPattern(yyyyMMddTHH:mm:ssSSSZ);

  String str=formaterformat(dt);

  StringBuffer sb=new StringBuffer(jtagetText());

  String temp=new String(sbappend(str));

  jtasetText(temp);

  /////////////////加載時間////////////////////////

  }else if(字體equals(str)){

  mspush(jtagetText());//入堆棧

  }else if(幫助主題equals(str)){

  help myhelp=new help(this);

  }else if(關於記事本equals(str)){

  javaxswingJOptionPaneshowMessageDialog(jf該記事本用java語言編寫作者瑚江水單位陝西科技大學時間);

  }

  }

  public static void main(String[] args){

  NoteBook nb=new NoteBook();

  }

  }

  ////////////////////替換類////////////////////////

  class exchange implements ActionListener{

  JDialog jd;

  JTextField jf;

  JTextField jf;

  JTextArea jta;

  public exchange(Object obj){

  if(obj instanceof NoteBook){

  NoteBook nb=(NoteBook)obj;

  jd=new JDialog(nbgetJFrame()替換);

  jta=nbgetJTextArea();

  }

  

  jf=new JTextField();

  jf=new JTextField();

  JLabel jl=new JLabel(替換內容);

  JLabel jl=new JLabel(替換對象);

  JButton jb=new JButton(確定);

  jbaddActionListener(this);

  JButton jb=new JButton(取消);

  jbaddActionListener(this);

  JPanel jp=new JPanel(new FlowLayout());

  jpadd(jl);

  jpadd(jf);//替換內容

  JPanel jp=new JPanel(new FlowLayout());

  jpadd(jl);

  jpadd(jf);//替換對象

  JPanel jp=new JPanel(new FlowLayout());

  jpadd(jb);jpadd(jb);

  jdsetLayout(new GridLayout());

  jdadd(jp);jdadd(jp);jdadd(jp);

  jdsetSize( );

  Toolkit tk=ToolkitgetDefaultToolkit();

  jdsetLocation((tkgetScreenSize()widthjdgetSize()width)/(tkgetScreenSize()heightjdgetSize()height)/);

  jdsetResizable(false);

  jdsetVisible(true);

  jdsetDefaultCloseOperation(JDialogDISPOSE_ON_CLOSE);

  }

  public void actionPerformed(ActionEvent e) {

  // TODO Autogenerated method stub

  Systemoutprintln(ok);

  if(確定equals(egetActionCommand())){

  String str=jfgetText();String str=jfgetText();

  String str=new String(jtagetText());

  str=strreplaceAll(str str);

  jtasetText(str);javaxswingJOptionPaneshowMessageDialog(jd替換完成);

  }

  if(取消equals(egetActionCommand())){

  jddispose();//關閉對話框

  }

  }

  }

  /////////////////幫助類///////////////////

  class help implements ActionListener{

  JDialog jd;

  public help(Object obj){

  if(obj instanceof NoteBook){

  NoteBook nb=(NoteBook)obj;

  jd=new JDialog(nbgetJFrame()help);

  }

  

  JMenuBar jmb=new JMenuBar();

  JMenuItem jm=new JMenuItem(文件);

  jmaddActionListener(this);

  JMenuItem jm=new JMenuItem(編輯);

  jmaddActionListener(this);

  jmbadd(jm);jmbadd(jm);

  jdadd(jmbBorderLayoutNORTH);

  jdsetSize( );

  Toolkit tk=ToolkitgetDefaultToolkit();

  jdsetLocation((tkgetScreenSize()widthjdgetSize()width)/(tkgetScreenSize()heightjdgetSize()height)/);

  jdsetResizable(false);

  jdsetVisible(true);

  jdsetDefaultCloseOperation(JDialogDISPOSE_ON_CLOSE);

  }

  public void actionPerformed(ActionEvent e) {

  // TODO Autogenerated method

  Systemoutprintln();

  if(文件equals(egetActionCommand())){

  javaxswingJOptionPaneshowMessageDialog(jd主要的功能有新建打開保存等具體的操作點擊菜單即可);

  }

  if(編輯equals(egetActionCommand())){

  javaxswingJOptionPaneshowMessageDialog(jd主要的功能有撤銷剪貼粘貼復制刪除等具體的操作點擊菜單即可);

  }

  }

  }

  //////////////////////////我的堆棧///////////////////////

  class myStack{

  List list;

  public myStack(){

  list=new ArrayList();

  }

  public void push(Object obj){

  if(obj instanceof String){

  String str=(String)obj;

  listadd(obj);

  }else{

  Systemexit();

  }

  }

  public String pop(){

  String obj=(String)listget(listsize());

  listremove(listsize());

  return obj;

  }

  }

  ////////////////////////////////////////////////////////////


From:http://tw.wingwit.com/Article/program/Java/hx/201311/26798.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.