最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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("復制");
 JMenuItem paste = new JMenuItem("粘貼");
 JMenuItem findRep = new JMenuItem("查找替換");
 JMenuItem selectAll = new JMenuItem("全選");
 JMenuItem font = new JMenuItem("字體");
 JMenuItem about = new JMenuItem("關于");
 JMenuItem cut2 = new JMenuItem("剪切(X)");

 JMenuItem copy2 = new JMenuItem("復制(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);
  // 設置撤銷文本的管理器
  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 {// 關于窗口

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

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

相關文章

  • Java實現(xiàn)斷點下載功能的示例代碼

    Java實現(xiàn)斷點下載功能的示例代碼

    當下載一個很大的文件時,如果下載到一半暫停,如果繼續(xù)下載呢?斷點下載就是解決這個問題的。本文將用Java語言實現(xiàn)斷點下載,需要的可以參考一下
    2022-05-05
  • Java 解決讀寫本地文件中文亂碼的問題

    Java 解決讀寫本地文件中文亂碼的問題

    這篇文章主要介紹了Java 解決讀寫本地文件中文亂碼的問題的相關資料,需要的朋友可以參考下
    2017-01-01
  • 詳解SpringBoot注冊Windows服務和啟動報錯的原因

    詳解SpringBoot注冊Windows服務和啟動報錯的原因

    這篇文章主要介紹了詳解SpringBoot注冊Windows服務和啟動報錯的原因,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-03-03
  • TraceIdPatternLogbackLayout日志攔截源碼解析

    TraceIdPatternLogbackLayout日志攔截源碼解析

    這篇文章主要為大家介紹了TraceIdPatternLogbackLayout日志攔截源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-11-11
  • @RereshScope刷新的原理詳解

    @RereshScope刷新的原理詳解

    在配合配置中心修改配置讓應用自動刷新配置時,我們要在需要感知配置變化的bean上面加上@RereshScope。如果我們不加上這注解,那么有可能無法完成配置自動刷新。本文就來和大家講講@RereshScope刷新的原理,需要的可以參考一下
    2022-12-12
  • SpringMVC使用RESTful接口案例

    SpringMVC使用RESTful接口案例

    RESTful是一種web軟件風格,它不是標準也不是協(xié)議,它不一定要采用,只是一種風格,它倡導的是一個資源定位(url)及資源操作的風格,這篇文章主要介紹了SpringBoot使用RESTful接口
    2022-12-12
  • logback高效狀態(tài)管理器StatusManager源碼解析

    logback高效狀態(tài)管理器StatusManager源碼解析

    這篇文章主要為大家介紹了logback高效狀態(tài)管理器StatusManager源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-11-11
  • Spring加載properties文件的方法

    Spring加載properties文件的方法

    這篇文章主要為大家詳細介紹了Spring加載properties文件的兩種方法,一是通過xml方式,另一種方式是通過注解方式,感興趣的小伙伴們可以參考一下
    2016-06-06
  • 解決新版 Idea 中 SpringBoot 熱部署不生效的問題

    解決新版 Idea 中 SpringBoot 熱部署不生效的問題

    這篇文章主要介紹了解決新版 Idea 中 SpringBoot 熱部署不生效的問題,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-08-08
  • SpringBoot啟動報錯Whitelabel Error Page: This application has no explicit mapping for的解決方法

    SpringBoot啟動報錯Whitelabel Error Page: This&nbs

    當我們使用Spring Boot框架開發(fā)Web應用時,有時會遇到啟動報錯信息為"Whitelabel Error Page: This application has no explicit mapping for",種報錯信息意味著我們的應用缺少某個URL映射的配置,導致請求無法處理,在本篇文章中,我們將詳細討論如何解決這個問題
    2024-03-03

最新評論

德令哈市| 蓬溪县| 北宁市| 秭归县| 筠连县| 马龙县| 姚安县| 体育| 香港| 绵阳市| 汉川市| 鹤壁市| 调兵山市| 绵阳市| 行唐县| 江华| 仪陇县| 湘潭市| 五莲县| 肇东市| 永川市| 旅游| 崇信县| 永新县| 元氏县| 西城区| 长葛市| 加查县| 沿河| 德化县| 买车| 沙田区| 大城县| 中卫市| 永宁县| 荆州市| 内乡县| 尤溪县| 平泉县| 贞丰县| 广安市|