欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java仿Windows記事本源代碼分享

 更新時間:2019年03月31日 16:07:05   作者:cjbi  
這篇文章主要為大家詳細介紹了Java仿Windows記事本源代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了Java仿Windows記事本的具體代碼,供大家參考,具體內(nèi)容如下

先上截圖:



源代碼:

import java.awt.*;
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.undo.UndoManager;
import java.awt.event.*;
import java.io.*;

/**
 * 
 * @author cjbi@outlook.com
 * @created 2015年7月6日 上午11:22:24
 */
public class JNotepad extends JFrame implements ActionListener {

 JMenuBar menubar = new JMenuBar();
 JMenu file = new JMenu("文件(F)");
 JMenu edit = new JMenu("編輯(E)");
 JMenu format = new JMenu("格式(O)");
 JMenu help = new JMenu("幫助(H)");
 JMenuItem create = new JMenuItem("新建");
 JMenuItem open = new JMenuItem("打開...");
 JMenuItem save = new JMenuItem("保存");
 JMenuItem saveAs = new JMenuItem("另存為...");
 JMenuItem exit = new JMenuItem("退出");
 JMenuItem undo = new JMenuItem("撤銷");
 JMenuItem cut = new JMenuItem("剪切");
 JMenuItem copy = new JMenuItem("復(fù)制");
 JMenuItem paste = new JMenuItem("粘貼");
 JMenuItem findRep = new JMenuItem("查找替換");
 JMenuItem selectAll = new JMenuItem("全選");
 JMenuItem font = new JMenuItem("字體");
 JMenuItem about = new JMenuItem("關(guān)于");
 JMenuItem cut2 = new JMenuItem("剪切(X)");

 JMenuItem copy2 = new JMenuItem("復(fù)制(C)");
 JMenuItem paste2 = new JMenuItem("粘貼(V)");
 JMenuItem selectAll2 = new JMenuItem("全選(A)");
 public static JTextArea textarea = new JTextArea();
 UndoManager um = new UndoManager();
 JScrollPane scroll = new JScrollPane(textarea, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
 JPopupMenu popup = new JPopupMenu();
 String pathSelect;

 // 獲取屏幕尺寸
 Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

 public JNotepad() {

  // 此處定義鍵盤快捷鍵
  // MenuBar
  file.setMnemonic(KeyEvent.VK_F);
  edit.setMnemonic(KeyEvent.VK_E);
  format.setMnemonic(KeyEvent.VK_O);
  help.setMnemonic(KeyEvent.VK_H);
  // MenuItem
  create.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, ActionEvent.CTRL_MASK));
  open.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, ActionEvent.CTRL_MASK));
  save.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, ActionEvent.CTRL_MASK));
  undo.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Z, ActionEvent.CTRL_MASK));
  cut.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, ActionEvent.CTRL_MASK));
  copy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, ActionEvent.CTRL_MASK));
  paste.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V, ActionEvent.CTRL_MASK));
  findRep.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F, ActionEvent.CTRL_MASK));
  selectAll.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A, ActionEvent.CTRL_MASK));

  // 事件監(jiān)聽者
  save.addActionListener(this);
  create.addActionListener(this);
  open.addActionListener(this);
  saveAs.addActionListener(this);
  exit.addActionListener(this);
  undo.addActionListener(this);
  cut.addActionListener(this);
  copy.addActionListener(this);
  paste.addActionListener(this);
  selectAll.addActionListener(this);
  font.addActionListener(this);
  about.addActionListener(this);
  cut2.addActionListener(this);
  copy2.addActionListener(this);
  paste2.addActionListener(this);
  selectAll2.addActionListener(this);
  findRep.addActionListener(this);
  // 設(shè)置撤銷文本的管理器
  textarea.getDocument().addUndoableEditListener(um);
  textarea.setFont(Format.font);
  // 文件
  file.add(create);
  file.add(open);
  file.add(save);
  file.add(saveAs);
  file.addSeparator();
  file.add(exit);

  // 編輯
  edit.add(undo);
  edit.addSeparator();
  edit.add(cut);
  edit.add(copy);
  edit.add(paste);
  edit.addSeparator();
  edit.add(findRep);
  edit.addSeparator();
  edit.add(selectAll);

  // 格式
  format.add(font);

  // 幫助
  help.add(about);

  // 菜單欄
  menubar.add(file);
  menubar.add(edit);
  menubar.add(format);
  menubar.add(help);

  // 右鍵菜單
  popup.add(cut2);
  popup.add(copy2);
  popup.add(paste2);
  popup.addSeparator();
  popup.add(selectAll2);

  // 添加到文本域容器
  textarea.add(popup);

  // 匿名內(nèi)部類監(jiān)聽器右鍵動作
  textarea.addMouseListener(new MouseAdapter() {
   public void mouseReleased(MouseEvent e) {
    if (e.getButton() == MouseEvent.BUTTON3) {
     popup.show(textarea, e.getX(), e.getY());
    }
   }
  });

  // 邊界布局
  this.add(menubar, BorderLayout.NORTH);
  this.add(scroll, BorderLayout.CENTER);
  this.setTitle("記事本");
  this.setSize(500, 400);
  this.setLocationRelativeTo(null);
  this.setIconImage(new ImageIcon(this.getClass().getResource("/icon/notepad.png")).getImage());//圖標放在源目錄的icon文件夾
  this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  this.setVisible(true);

 }

 // 重寫actionPerformed
 @Override
 public void actionPerformed(ActionEvent e) {
  // Event對象發(fā)生源
  if (e.getSource() == open) {

   JFileChooser chooser = new JFileChooser();
   FileNameExtensionFilter filter = new FileNameExtensionFilter("文本文檔(*.txt)", "txt");
   chooser.setFileFilter(filter);
   chooser.setDialogTitle("文件打開");
   chooser.showOpenDialog(null);
   chooser.setVisible(true);

   try {
    pathSelect = chooser.getSelectedFile().getPath();
    FileReader wjl = new FileReader(pathSelect);
    BufferedReader hcl = new BufferedReader(wjl);
    String s = "", zfc = "";
    while ((s = hcl.readLine()) != null) {
     zfc += (s + "\n");
    }
    textarea.setText(zfc);

   } catch (Exception e1) {
   }
  }

  if (e.getSource() == saveAs) {// 另存為

   JFileChooser chooser = new JFileChooser();
   FileNameExtensionFilter filter = new FileNameExtensionFilter("文本文檔(*.txt)", "txt");
   chooser.setFileFilter(filter);
   chooser.setDialogTitle("另存為");
   chooser.showSaveDialog(null);
   chooser.setVisible(true);

   PrintStream ps;
   try {
    String select = chooser.getSelectedFile().getPath();
    ps = new PrintStream(select);
    System.setOut(ps);
    System.out.println(this.textarea.getText());

   } catch (Exception e1) {
   }
  }

  if (e.getSource() == save && (pathSelect == null)) {// 保存
   JFileChooser chooser = new JFileChooser();

   chooser.setDialogTitle("保存");
   chooser.showSaveDialog(null);
   chooser.setVisible(true);

   PrintStream ps;
   try {
    pathSelect = chooser.getSelectedFile().getPath();
    ps = new PrintStream(pathSelect);
    System.setOut(ps);
    System.out.println(this.textarea.getText());

   } catch (Exception e1) {
   }
  } else if (e.getSource() == save && !(pathSelect == null)) {
   PrintStream ps;
   try {
    ps = new PrintStream(pathSelect);
    System.setOut(ps);
    System.out.println(this.textarea.getText());
   } catch (FileNotFoundException e1) {
   }
  }

  if (e.getSource() == create) {
   textarea.setText("");
   pathSelect = null;
  }

  if (e.getSource() == exit) {
   System.exit(0);
  }

  if (e.getSource() == undo) {
   if (um.canUndo()) {
    um.undo();
   }
  }

  if (e.getSource() == cut || e.getSource() == cut2) {
   textarea.cut();
  } else if (e.getSource() == copy || e.getSource() == copy2) {
   textarea.copy();
  } else if (e.getSource() == paste || e.getSource() == paste2) {
   textarea.paste();
  } else if (e.getSource() == findRep) {
   new FindAndReplace(textarea);
  }

  else if (e.getSource() == selectAll || e.getSource() == selectAll2) {
   textarea.selectAll();
  }
  if (e.getSource() == font) {
   new Format(textarea);
  }
  if (e.getSource() == about) {
   new About();
  }

 }

 public static void main(String[] args) {
  new JNotepad();
 }

}

class FindAndReplace extends JDialog implements ActionListener {// 查找和替換
 JLabel findLabel = new JLabel("查找內(nèi)容:");
 JLabel repLabel = new JLabel(" 替換為:");
 JTextField findTf = new JTextField(8);
 JTextField repTf = new JTextField(8);
 JButton findBtn = new JButton("查找");
 JButton repBtn = new JButton("替換");
 JPanel findPn = new JPanel();
 JPanel repPn = new JPanel();
 JTextArea textarea;

 String text;
 boolean flg = false;
 int len;
 int start = 0;
 int k = 0;

 public FindAndReplace(JTextArea textarea) {

  this.textarea = textarea;

  findPn.add(findLabel);
  findPn.add(findTf);
  findPn.add(findBtn);
  repPn.add(repLabel);
  repPn.add(repTf);
  repPn.add(repBtn);
  this.add(findPn);
  this.add(repPn);

  findBtn.addActionListener(this);
  repBtn.addActionListener(this);

  this.setTitle("查找和替換");
  this.setLayout(new GridLayout(2, 1));
  // this.setBounds(400, 200, 300, 140);
  this.pack();
  this.setLocationRelativeTo(null);
  this.setResizable(false);
  this.setVisible(true);
  this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
 }

 @SuppressWarnings("deprecation")
 public void actionPerformed(ActionEvent e) {
  String findText = findTf.getText();
  String repText = repTf.getText();
  text = textarea.getText();
  if (e.getSource() == findBtn) {
   findBtn.setLabel("下一個");
   if (findText != null) {
    len = findText.length();
    start = text.indexOf(findText, k);
    k = start + len;
    textarea.select(start, start + len);
    flg = true;
    if (start == -1) {
     JOptionPane.showMessageDialog(null, "已到文件尾部!", "提示", JOptionPane.INFORMATION_MESSAGE);
     start = 0;
     k = 0;
     flg = false;
    }
   }
  } else if (e.getSource() == repBtn) {
   if (flg) {
    textarea.replaceRange(repText, start, start + len);
    flg = false;
   }
  }
 }
}

// 字體格式
class Format extends JDialog implements ActionListener {

 public static int style = 0; // 全局變量類型,默認值為0
 public static int size = 16; // 全局變量字體大小,默認值為16
 public static Font font = new Font("新宋體", style, size); // 全局變量字體,默認值為新宋體

 JPanel pn = new JPanel();
 JPanel okCelPn = new JPanel();
 JPanel fontPn = new JPanel();
 JPanel ptPn = new JPanel();
 JLabel fontLabel = new JLabel("字體: ");
 JLabel fontStyleLabel = new JLabel(" 字形: ");
 JLabel ptLabel = new JLabel("  磅值: ");
 JButton ok = new JButton("確定");
 JButton cancel = new JButton("取消");
 GraphicsEnvironment e = GraphicsEnvironment.getLocalGraphicsEnvironment();// 獲取系統(tǒng)中可用的字體的名字
 String[] fontName = e.getAvailableFontFamilyNames();// 獲取系統(tǒng)中可用的字體的名字
 String[] fontType = { "常規(guī)", "傾斜", "粗體", "粗偏斜體" };
 JList fontList = new JList(fontName);
 JList fontTypeList = new JList(fontType);
 JScrollPane fontScroll = new JScrollPane(fontList);
 JScrollPane fontTypeScroll = new JScrollPane(fontTypeList);

 JTextArea textarea;
 SpinnerModel spinnerModel = new SpinnerNumberModel(size, // initial value
   0, // min
   100, // max
   2 // Step
 );
 JSpinner spinner = new JSpinner(spinnerModel);

 public Format(JTextArea textarea) {
  this.textarea = textarea;
  ok.addActionListener(this);
  cancel.addActionListener(this);

  pn.setLayout(new GridLayout(2, 1));
  pn.add(fontPn);
  pn.add(ptPn);

  fontPn.add(fontLabel);
  fontPn.add(fontScroll);
  fontPn.add(fontStyleLabel);
  fontPn.add(fontTypeScroll);

  ptPn.add(ptLabel);
  ptPn.add(spinner);

  fontList.setVisibleRowCount(5);
  fontList.setFixedCellWidth(60);
  fontList.setSelectedIndex(50);
  fontList.setSelectedValue(font.getFontName(), true);

  fontTypeList.setVisibleRowCount(5);
  fontTypeList.setSelectedIndex(style);
  okCelPn.add(ok);
  okCelPn.add(cancel);

  okCelPn.setLayout(new FlowLayout(FlowLayout.RIGHT));

  this.add(pn, BorderLayout.CENTER);
  this.add(okCelPn, BorderLayout.SOUTH);

  this.setTitle("字體");
  this.pack();
  this.setLocationRelativeTo(null);
  this.setResizable(false);
  this.setVisible(true);
  this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
 }

 public void actionPerformed(ActionEvent e) {
  if (e.getSource() == ok) {
   System.out.println(fontList.getSelectedValue());
   style = this.type();
   size = Integer.parseInt(spinner.getValue().toString());
   font = new Font((String) fontList.getSelectedValue(), style, size);
   textarea.setFont(font);
   this.dispose();
   System.out.println(type());
  } else if (e.getSource() == cancel) {
   this.dispose();
  }
 }

 private int type() {
  if (fontTypeList.getSelectedValue().equals("傾斜")) {
   return 1;
  } else if (fontTypeList.getSelectedValue().equals("粗體")) {
   return 2;
  } else if (fontTypeList.getSelectedValue().equals("粗偏斜體")) {
   return 3;
  } else
   return 0;
 }

}

class About extends JDialog {// 關(guān)于窗口

 About() {
  JOptionPane.showMessageDialog(null, " 作者:cjb 版本:v1.5\n\n 聯(lián)系:cjbi@outlook.com", "關(guān)于",
    JOptionPane.PLAIN_MESSAGE);
 }
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 基于java實現(xiàn)簡單發(fā)紅包功能

    基于java實現(xiàn)簡單發(fā)紅包功能

    這篇文章主要為大家詳細介紹了基于java實現(xiàn)簡單發(fā)紅包功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-11-11
  • 詳談HashMap和ConcurrentHashMap的區(qū)別(HashMap的底層源碼)

    詳談HashMap和ConcurrentHashMap的區(qū)別(HashMap的底層源碼)

    下面小編就為大家?guī)硪黄斦凥ashMap和ConcurrentHashMap的區(qū)別(HashMap的底層源碼)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-08-08
  • Spring Cloud OAuth2 實現(xiàn)用戶認證及單點登錄的示例代碼

    Spring Cloud OAuth2 實現(xiàn)用戶認證及單點登錄的示例代碼

    這篇文章主要介紹了Spring Cloud OAuth2 實現(xiàn)用戶認證及單點登錄的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-10-10
  • SpringBoot是如何使用SQL數(shù)據(jù)庫的?

    SpringBoot是如何使用SQL數(shù)據(jù)庫的?

    今天給大家?guī)淼氖顷P(guān)于Springboot的相關(guān)知識,文章圍繞著SpringBoot是如何使用SQL數(shù)據(jù)庫的展開,文中有非常詳細的介紹及代碼示例,需要的朋友可以參考下
    2021-06-06
  • springboot中PostMapping正常接收json參數(shù)后返回404問題

    springboot中PostMapping正常接收json參數(shù)后返回404問題

    這篇文章主要介紹了springboot中PostMapping正常接收json參數(shù)后返回404問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-05-05
  • Java實現(xiàn)調(diào)用jython執(zhí)行python文件的方法

    Java實現(xiàn)調(diào)用jython執(zhí)行python文件的方法

    這篇文章主要介紹了Java實現(xiàn)調(diào)用jython執(zhí)行python文件的方法,結(jié)合實例形式分析了Java調(diào)用jython執(zhí)行python文件的常見操作技巧及相關(guān)問題解決方法,需要的朋友可以參考下
    2018-03-03
  • Java使用httpRequest+Jsoup爬取紅藍球號碼

    Java使用httpRequest+Jsoup爬取紅藍球號碼

    本文將結(jié)合實例代碼,介紹Java使用httpRequest+Jsoup爬取紅藍球號碼,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-07-07
  • selenium高效應(yīng)對Web頁面元素刷新的實例講解

    selenium高效應(yīng)對Web頁面元素刷新的實例講解

    今天小編就為大家分享一篇selenium高效應(yīng)對Web頁面元素刷新的實例講解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-05-05
  • Java 數(shù)組差集實例代碼

    Java 數(shù)組差集實例代碼

    這篇文章主要介紹了Java 數(shù)組差集實例代碼,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-09-09
  • 基于SpringBoot開機啟動與@Order注解

    基于SpringBoot開機啟動與@Order注解

    這篇文章主要介紹了SpringBoot開機啟動與@Order注解,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-09-09

最新評論