⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mainsystem.java

📁 Java记事本源文件 分享! 实现简易功能
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
import java.awt.*; 
import javax.swing.*; 
import java.awt.event.*; 
import java.io.*; 
import javax.swing.event.*; 
import javax.swing.undo.*; 
import java.util.*; 
/* 
*一个用java实现的记事本 
* 于2008.05.07 23:10 完成 
*/ 
class MainSystem extends JFrame implements ActionListener { 
/** 
  * 
  */ 
private static final long serialVersionUID = 1L; 
private JTextArea textarea = new JTextArea(); 
JCheckBoxMenuItem jcbmi = new JCheckBoxMenuItem("自动换行", true); 
private JScrollPane scroll = new JScrollPane(textarea, 
       ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, 
       ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED); 
String s1 = textarea.getText(); 
Container pane = getContentPane(); 

UndoableEditListener ue=new UndoHander(); 
UndoManager undo = new UndoManager(); 

int startindex = 0, a = 0, b = 0; 
MainSystem() { 
  super("晓春记事本"); 
  setSize(600, 600); 
  setLocation(100, 100); 
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
   
  textarea.setLineWrap(true); 
  textarea.setWrapStyleWord(true); 
  JMenuBar jmenubar = new JMenuBar(); 
  setJMenuBar(jmenubar); 
  JMenu file = new JMenu("文件"); 
  JMenuItem item1 = new JMenuItem("新建"); 
  JMenuItem item2 = new JMenuItem("打开"); 
  JMenuItem item3 = new JMenuItem("保存"); 
  JMenuItem item4 = new JMenuItem("另存为"); 
  JMenuItem item5 = new JMenuItem("退出"); 
  item1.addActionListener(this); 
  item2.addActionListener(this); 
  item3.addActionListener(this); 
  item4.addActionListener(this); 
  item5.addActionListener(this); 
  file.add(item1); 
  file.add(item2); 
  file.addSeparator(); 
  file.add(item3); 
  file.add(item4); 
  file.addSeparator(); 
  file.add(item5); 
  jmenubar.add(file); 
  JMenu edit = new JMenu("编辑"); 
  JMenuItem item6 = new JMenuItem("撤销"); 
  JMenuItem item7 = new JMenuItem("剪切"); 
  JMenuItem item8 = new JMenuItem("复制"); 
  JMenuItem item9 = new JMenuItem("粘贴"); 
  JMenuItem item10 = new JMenuItem("查找"); 
  JMenuItem item11 = new JMenuItem("替换"); 
  JMenuItem item12 = new JMenuItem("全选"); 
  JMenuItem item13 = new JMenuItem("时间/日期"); 
  item6.addActionListener(this); 
  textarea.getDocument().addUndoableEditListener(ue); 
  if(undo.canUndo()) { 
   item6.setEnabled(false); 
  } 
  item7.addActionListener(this); 
  item8.addActionListener(this); 
  item9.addActionListener(this); 
  item10.addActionListener(this); 
  item11.addActionListener(this); 
  item12.addActionListener(this); 
  item13.addActionListener(this); 
  edit.add(item6); 
  edit.add(item7); 
  edit.add(item8); 
  edit.addSeparator(); 
  edit.add(item9); 
  edit.add(item10); 
  edit.add(item11); 
  edit.addSeparator(); 
  edit.add(item12); 
  edit.add(item13); 
  jmenubar.add(edit); 
  JMenu gesh = new JMenu("格式"); 
  JMenuItem item14 = new JMenuItem("字体"); 
  jcbmi.addActionListener(this); 
  item14.addActionListener(this); 
  gesh.add(jcbmi); 
  gesh.addSeparator(); 
  gesh.add(item14); 
  jmenubar.add(gesh); 
  JMenu look = new JMenu("查看"); 
  JMenuItem state = new JMenuItem("状态栏"); 
  state.setEnabled(false); 
  look.add(state); 
  jmenubar.add(look); 
  JMenu help = new JMenu("帮助"); 
  JMenuItem about = new JMenuItem("关于记事本"); 
  about.addActionListener(this); 
  help.add(about); 
  jmenubar.add(help); 
  pane.add(scroll); 
  setContentPane(pane); 
  setVisible(true); 
} 
@SuppressWarnings({ "static-access", "deprecation" }) 
public void actionPerformed(ActionEvent evt) { 
  if (evt.getActionCommand().equals("新建")) { 
   textarea.setText(""); 
  } 
  if (evt.getActionCommand().equals("打开")) { 
   try { 
    Frame frame = new Frame(); 
    FileDialog fd = new FileDialog(frame, "打开文件", FileDialog.LOAD); 
    fd.setVisible(true); 
    String fpath = fd.getDirectory(); 
    String fname = fd.getFile(); 
    BufferedReader br = new BufferedReader(new FileReader(fpath 
      + fname)); 
    textarea.setText(""); 
    String s = br.readLine(); 
    while (s != null) { 
     textarea.append(s + "\n"); 
     s = br.readLine(); 
    } 
    br.close(); 
   } catch (Exception e) { 
   } 
  } 
  if (evt.getActionCommand().equals("保存")) { 
   Frame frame = new Frame("保存"); 
   FileDialog fd = new FileDialog(frame, "保存文件", FileDialog.SAVE); 
   fd.setFile(".txt"); 
   fd.setVisible(true); 
   try { 
    String savepath = fd.getDirectory(); 
    String savename = fd.getFile(); 
    if (savename != null) { 
     PrintWriter pw = new PrintWriter(new BufferedWriter( 
       new FileWriter(savepath + savename))); 
     pw 
       .write(textarea.getText(), 0, textarea.getText() 
         .length()); 
     pw.flush(); 
    } 
   } catch (Exception esave) { 
   } 
  } 
  if (evt.getActionCommand().equals("另存为")) { 
   Frame frame = new Frame("保存"); 
   FileDialog fd = new FileDialog(frame, "文件另存为", FileDialog.SAVE); 
   fd.setFile(".txt"); 
   fd.setVisible(true); 
   try { 
    String savepath = fd.getDirectory(); 
    String savename = fd.getFile(); 
    if (savename != null) { 
     PrintWriter pw = new PrintWriter(new BufferedWriter( 
       new FileWriter(savepath + savename))); 
     pw 
       .write(textarea.getText(), 0, textarea.getText() 
         .length()); 
     pw.flush(); 
    } 
   } catch (Exception esave) { 
   } 
  } 
  if (evt.getActionCommand().equals("退出")) { 
   String s2 = textarea.getText(); 
   if (!s1.equals(s2)) { 
    JOptionPane p = new JOptionPane(null, JOptionPane.QUESTION_MESSAGE); 
    int result = p.showConfirmDialog(this, "文档被改变,是否保存?", "晓春记事本", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE); 
    if(result == JOptionPane.YES_OPTION) { 
     try { 
      FileOutputStream file = new FileOutputStream("新建文档.txt"); 
      byte buf[] = s2.getBytes(); 
      for(int i = 0; i < buf.length; i++) { 
       file.write(buf[i]); 
      } 
      file.close(); 
     } catch(Exception e) { 
      System.out.println("Exception: " + e); 
     } 
     System.exit(0); 
    } 
     
    if(result == JOptionPane.NO_OPTION) { 
     System.exit(0); 
    } 
     
    if(result == JOptionPane.CANCEL_OPTION) { 
     //Do nothing; 
    } 
   } 
   else 
    System.exit(0); 
  } 
   
  if(evt.getActionCommand().equals("撤销")) { 
   try { 
    undo.undo(); 
   } catch(Exception e) { 
   } 
  } 
   
   
  if(evt.getActionCommand().equals("剪切")) { 
   textarea.cut(); 
  } 
   
  if(evt.getActionCommand().equals("复制")) { 
   textarea.copy(); 
  } 
   
  if(evt.getActionCommand().equals("粘贴")) { 
   textarea.paste(); 
  } 
   
  if(evt.getActionCommand().equals("查找")) { 
   final JDialog search = new JDialog(this, "查找", true); 
   Container searchpane = search.getContentPane(); 
   searchpane.setLayout(new FlowLayout()); 
   search.setSize(365, 80); 
   search.setLocation(300, 400); 
   JButton searchbutton = new JButton("查找下一个"); 
   JButton cancel = new JButton("取消"); 
   JLabel label = new JLabel("查找项目: "); 
   final JTextField searchfield = new JTextField(10); 
   searchpane.add(label); 
   searchpane.add(searchfield); 
   searchpane.add(searchbutton); 
   searchpane.add(cancel); 
   searchbutton.addActionListener(new ActionListener() { 
    public void actionPerformed(ActionEvent se) { 
     String textstr = textarea.getText(); 
     String fieldstr = searchfield.getText(); 
     if(a >= 0) { 
      a = textstr.indexOf(fieldstr, startindex); 
      b = fieldstr.length(); 
      startindex = a + b; 
      if(a == -1) { 
       JOptionPane.showMessageDialog(null, "没有你要查找的信息", "查找结果", 1); 
       a = 0; 
       startindex = 0; 
      } 
      textarea.select(a, startindex); 
     } 
    } 
   } 
   ); 
   cancel.addActionListener(new ActionListener() { 
    public void actionPerformed(ActionEvent ce) { 
     search.dispose(); 
    } 
   } 
   ); 
   search.setVisible(true); 
  } 
   
   
  if(evt.getActionCommand().equals("替换")) { 
   final JDialog jd = new JDialog(this, "替换", true); 
   Container changepane = jd.getContentPane(); 
   changepane.setLayout(new GridLayout(4, 2, 5, 5)); 
   jd.setSize(230, 170); 
   jd.setLocation(300, 400); 
   JLabel slabel = new JLabel("查找内容: "); 
   JLabel clabel = new JLabel("替换内容: "); 
   final JTextField sfield = new JTextField(10); 
   final JTextField cfield = new JTextField(10); 
   JButton sbutton = new JButton("查找下一个"); 
   JButton cbutton = new JButton("替换"); 
   JButton cabutton = new JButton("全部替换"); 
   JButton cancel = new JButton("取消"); 
   changepane.add(slabel); 
   changepane.add(sfield); 
   changepane.add(clabel); 
   changepane.add(cfield); 
   changepane.add(sbutton); 
   changepane.add(cbutton); 
   changepane.add(cabutton); 
   changepane.add(cancel); 
   sbutton.addActionListener(new ActionListener() { 
    public void actionPerformed(ActionEvent e) { 
     String textstr = textarea.getText(); 
     String fieldstr = sfield.getText(); 
     if(a >= 0) { 
      a = textstr.indexOf(fieldstr, startindex); 
      b = fieldstr.length(); 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -