用java写一一个简易的记事本



  1. import java.awt.Color;
  2. import java.awt.Dimension;
  3. import java.awt.EventQueue;
  4. import java.awt.Font;
  5. import java.awt.GraphicsEnvironment;
  6. import java.awt.Insets;
  7. import java.awt.Toolkit;
  8. import java.awt.event.ActionEvent;
  9. import java.awt.event.ActionListener;
  10. import java.io.File;
  11. import java.io.FileReader;
  12. import java.io.FileWriter;
  13. import java.io.IOException;
  14. import javax.swing.JCheckBoxMenuItem;
  15. import javax.swing.JColorChooser;
  16. import javax.swing.JFileChooser;
  17. import javax.swing.JFrame;
  18. import javax.swing.JList;
  19. import javax.swing.JMenu;
  20. import javax.swing.JMenuBar;
  21. import javax.swing.JMenuItem;
  22. import javax.swing.JOptionPane;
  23. import javax.swing.JScrollPane;
  24. import javax.swing.JTextArea;
  25. import javax.swing.UIManager;
  26. import javax.swing.UnsupportedLookAndFeelException;
  27. import javax.swing.undo.UndoManager;
  28. /**
  29. * swing 简单记事本
  30. * @author kissy小强
  31. *
  32. */
  33. public class Note extends JFrame {
  34. private static final long serialVersionUID = 1L;
  35. UndoManager manager = new UndoManager();
  36. JTextArea text = new JTextArea();
  37. JFileChooser chooser;
  38. String s = "新建记事本";
  39. int result = 0;
  40. private File file;
  41. JMenuBar Mb;
  42. JMenu M1, M2, M3, M4;
  43. JMenuItem m11, m12, m13, m14, m15, m21, m22, m23, m24, m25, m32, m33, m41;
  44. private final static int HEIGHT= 450;
  45. private final static int WIDTH= 750;
  46. JCheckBoxMenuItem m31 = new JCheckBoxMenuItem( "自动换行", true);
  47. public static void main(String args[]) {
  48. String lookAndFeel = UIManager.getSystemLookAndFeelClassName();
  49. try {
  50. UIManager.setLookAndFeel(lookAndFeel);
  51. } catch (ClassNotFoundException | InstantiationException
  52. | IllegalAccessException | UnsupportedLookAndFeelException e1) {
  53. e1.printStackTrace();
  54. }
  55. EventQueue.invokeLater( new Runnable() {
  56. public void run() {
  57. try {
  58. Note n = new Note();
  59. } catch (Exception e) {
  60. e.printStackTrace();
  61. }
  62. }
  63. });
  64. }
  65. public Note() {
  66. setTitle(s);
  67. setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  68. Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
  69. // 离显示屏上边缘x像素,里显示屏左边缘y像素
  70. setLocation(screenSize.width / 2 - WIDTH / 2, screenSize.height / 2- HEIGHT / 2);
  71. setSize(WIDTH, HEIGHT);
  72. setResizable( true); // 窗体是否可变
  73. setVisible( true); // 窗体是否可见
  74. init();
  75. }
  76. private void init(){
  77. Mb = new JMenuBar();
  78. this.setJMenuBar(Mb);
  79. text.getDocument().addUndoableEditListener(manager); // 设置文本框编辑监听(可撤销)
  80. text.setFont( new Font( "宋体", Font.PLAIN, 14));
  81. text.setCaretColor(Color.RED); // 光标颜色
  82. text.setSelectedTextColor(Color.RED); // 选中字体颜色
  83. text.setSelectionColor(Color.GREEN); // 选中背景颜色
  84. text.setLineWrap( true); // 是否换行
  85. text.setWrapStyleWord( true); // 是否单词边界换行(即有空白)
  86. text.setMargin( new Insets( 3, 5, 3, 5)); // 文本区与边框的间距,四个参数分别为上、左、下、右
  87. text.setDragEnabled( true); // 开启或关闭自动拖动处理
  88. add( new JScrollPane(text, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
  89. JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED));
  90. M1 = this.AddBar( "文件", Mb);
  91. M2 = this.AddBar( "编辑", Mb);
  92. M3 = this.AddBar( "格式", Mb);
  93. M4 = this.AddBar( "帮助", Mb);
  94. /* 新建选项 */
  95. m11 = this.AddItem( "新建", M1);
  96. m11.addActionListener( new ActionListener() {
  97. public void actionPerformed(ActionEvent event) {
  98. text.setText( "");
  99. setTitle(s);
  100. file = null;
  101. }
  102. });
  103. /* 打开选项 */
  104. m12 = this.AddItem( "打开", M1);
  105. m12.addActionListener( new ActionListener() {
  106. public void actionPerformed(ActionEvent event) {
  107. try {
  108. // 设置打开时的默认目录,两种方式
  109. chooser = new JFileChooser( "C:\\Users\\xiaozhx\\Desktop");
  110. // chooser.setCurrentDirectory(new File("C:\\Users\\xiaozhx\\Desktop"));
  111. chooser.setFileFilter( new filter());
  112. result = chooser.showOpenDialog( null);
  113. if (result == JFileChooser.APPROVE_OPTION) {
  114. // 点击了打开按钮
  115. file = chooser.getSelectedFile();
  116. int length = ( int) file.length();
  117. FileReader reader = new FileReader(file);
  118. char[] ch = new char[length];
  119. reader.read(ch);
  120. reader.close();
  121. text.setText( new String(ch).trim());
  122. setTitle(file.getName());
  123. } else if (result == JFileChooser.CANCEL_OPTION) {
  124. // 点击了取消按钮
  125. }
  126. } catch (Exception e) {
  127. JOptionPane.showMessageDialog( null, e);
  128. }
  129. }
  130. });
  131. /* 保存选项 */
  132. m13 = this.AddItem( "保存", M1);
  133. m13.addActionListener( new ActionListener() {
  134. public void actionPerformed(ActionEvent event) {
  135. if (file == null)
  136. try {
  137. chooser = new JFileChooser(
  138. "C:\\Users\\xiaozhx\\Desktop");
  139. chooser.setFileFilter( new filter());
  140. result = chooser.showSaveDialog( null);
  141. if (result == JFileChooser.APPROVE_OPTION) {
  142. File selectfile = chooser.getSelectedFile(); // 获得文件名
  143. // 获得被选中的过滤器中的文件扩展名
  144. String end = chooser.getFileFilter().getDescription();
  145. File newFile = null;
  146. if (selectfile.getAbsolutePath().toUpperCase().endsWith(end.toUpperCase())) {
  147. // 如果文件是以选定扩展名结束的,则使用原名
  148. newFile = selectfile;
  149. } else {
  150. // 否则加上选定的扩展名
  151. newFile = new File(selectfile.getAbsolutePath()+ end);
  152. }
  153. try {
  154. if (newFile.exists() == false) {
  155. newFile.createNewFile();
  156. }
  157. FileWriter writer = new FileWriter(newFile);
  158. char[] arry = text.getText().toCharArray();
  159. writer.write(arry);
  160. writer.flush();
  161. writer.close();
  162. setTitle(newFile.getName());
  163. file = newFile;
  164. } catch (IOException e) {
  165. }
  166. } else if (result == JFileChooser.CANCEL_OPTION) {
  167. // 点击了取消按钮
  168. }
  169. } catch (Exception e) {
  170. JOptionPane.showMessageDialog( null, e);
  171. }
  172. else
  173. try {
  174. FileWriter writer = new FileWriter(file);
  175. char[] arry = text.getText().toCharArray();
  176. writer.write(arry);
  177. writer.flush();
  178. writer.close();
  179. } catch (Exception e) {
  180. JOptionPane.showMessageDialog( null, e);
  181. }
  182. }
  183. });
  184. /* 另存为选项 */
  185. m14 = this.AddItem( "另存为", M1);
  186. m14.addActionListener( new ActionListener() {
  187. public void actionPerformed(ActionEvent event) {
  188. try {
  189. chooser = new JFileChooser( "C:\\Users\\xiaozhx\\Desktop");
  190. chooser.setFileFilter( new filter());
  191. result = chooser.showSaveDialog( null);
  192. if (result == JFileChooser.APPROVE_OPTION) {
  193. File selectfile = chooser.getSelectedFile(); // 获得文件名
  194. // 获得被选中的过滤器中的文件扩展名
  195. String end = chooser.getFileFilter().getDescription();
  196. File newFile = null;
  197. if (selectfile.getAbsolutePath().toUpperCase().endsWith(end.toUpperCase())) {
  198. // 如果文件是以选定扩展名结束的,则使用原名
  199. newFile = selectfile;
  200. } else {
  201. // 否则加上选定的扩展名
  202. newFile = new File(selectfile.getAbsolutePath()+ end);
  203. }
  204. try {
  205. if (newFile.exists() == false) {
  206. newFile.createNewFile();
  207. }
  208. FileWriter writer = new FileWriter(newFile);
  209. char[] arry = text.getText().toCharArray();
  210. writer.write(arry);
  211. writer.flush();
  212. writer.close();
  213. setTitle(newFile.getName());
  214. file = newFile;
  215. } catch (IOException e) {
  216. }
  217. } else if (result == JFileChooser.CANCEL_OPTION) {
  218. // 点击了取消按钮
  219. }
  220. } catch (Exception e) {
  221. JOptionPane.showMessageDialog( null, e);
  222. }
  223. }
  224. });
  225. M1.addSeparator(); // 横杆
  226. /* 退出选项 */
  227. m15 = this.AddItem( "退出", M1);
  228. m15.addActionListener( new ActionListener() {
  229. public void actionPerformed(ActionEvent event) {
  230. System.exit( 0);
  231. }
  232. });
  233. /* 撤消选项 */
  234. m21 = this.AddItem( "撤消", M2);
  235. m21.addActionListener( new ActionListener() {
  236. public void actionPerformed(ActionEvent event) {
  237. if (manager.canUndo())
  238. manager.undo();
  239. }
  240. });
  241. /* 剪切选项 */
  242. M2.addSeparator();
  243. m22 = this.AddItem( "剪切", M2);
  244. m22.addActionListener( new ActionListener() {
  245. public void actionPerformed(ActionEvent event) {
  246. text.cut();
  247. }
  248. });
  249. /* 复制选项 */
  250. m23 = this.AddItem( "复制", M2);
  251. m23.addActionListener( new ActionListener() {
  252. public void actionPerformed(ActionEvent event) {
  253. text.copy();
  254. }
  255. });
  256. /* 粘贴选项 */
  257. m24 = this.AddItem( "粘贴", M2);
  258. m24.addActionListener( new ActionListener() {
  259. public void actionPerformed(ActionEvent event) {
  260. text.paste();
  261. }
  262. });
  263. /* 删除选项 */
  264. m25 = this.AddItem( "删除", M2);
  265. m25.addActionListener( new ActionListener() {
  266. public void actionPerformed(ActionEvent event) {
  267. text.replaceRange( "", text.getSelectionStart(),
  268. text.getSelectionEnd());
  269. }
  270. });
  271. /* 自动换行选项 */
  272. M3.add(m31);
  273. m31.addActionListener( new ActionListener() {
  274. public void actionPerformed(ActionEvent event) {
  275. if (m31.getState())
  276. text.setLineWrap( true);
  277. else
  278. text.setLineWrap( false);
  279. }
  280. });
  281. /* 字体格式设置选项 */
  282. m32 = this.AddItem( "字体选择", M3);
  283. m32.addActionListener( new ActionListener() {
  284. public void actionPerformed(ActionEvent event) {
  285. GraphicsEnvironment ge = GraphicsEnvironment
  286. .getLocalGraphicsEnvironment();
  287. //获取系统字体
  288. JList<String> fontNames = new JList<String>(ge.getAvailableFontFamilyNames());
  289. int response = JOptionPane.showConfirmDialog( null,
  290. new JScrollPane(fontNames), "请选择字体",
  291. JOptionPane.OK_CANCEL_OPTION);
  292. Object selectedFont = fontNames.getSelectedValue();
  293. if (response == JOptionPane.OK_OPTION && selectedFont != null)
  294. text.setFont( new Font(fontNames.getSelectedValue()
  295. .toString(), Font.PLAIN, 20));
  296. }
  297. });
  298. /* 字体颜色设置选项 */
  299. m33 = this.AddItem( "字体颜色", M3);
  300. m33.addActionListener( new ActionListener() {
  301. public void actionPerformed(ActionEvent event) {
  302. Color color = JColorChooser.showDialog( null, "文字颜色选择",
  303. Color.WHITE);
  304. text.setForeground(color);
  305. }
  306. });
  307. m41 = this.AddItem( "关于记事本", M4);
  308. m41.addActionListener( new ActionListener() {
  309. public void actionPerformed(ActionEvent event) {
  310. JOptionPane.showMessageDialog( null, "记事本\n开发语言:JAVA\n开发者:袁健强",
  311. "关于", JOptionPane.PLAIN_MESSAGE);
  312. }
  313. });
  314. }
  315. /** 文件格式过滤器 **/
  316. private class filter extends javax.swing.filechooser.FileFilter {
  317. public boolean accept(File file) {
  318. String name = file.getName();
  319. name.toLowerCase();
  320. if (name.endsWith( ".txt") || file.isDirectory())
  321. return true;
  322. else
  323. return false;
  324. }
  325. public String getDescription() {
  326. return ".txt";
  327. }
  328. }
  329. /** 将菜单项JMenuItem添加到菜单JMenu **/
  330. public JMenuItem AddItem(String name, JMenu menu) {
  331. JMenuItem MI = new JMenuItem(name);
  332. menu.add(MI);
  333. return MI;
  334. }
  335. /** 将菜单JMenu添加到菜栏JMenuBar **/
  336. public JMenu AddBar(String name, JMenuBar mb) {
  337. JMenu Mb = new JMenu(name);
  338. mb.add(Mb);
  339. return Mb;
  340. }
  341. }

  1. import java.awt.Color;
  2. import java.awt.Dimension;
  3. import java.awt.EventQueue;
  4. import java.awt.Font;
  5. import java.awt.GraphicsEnvironment;
  6. import java.awt.Insets;
  7. import java.awt.Toolkit;
  8. import java.awt.event.ActionEvent;
  9. import java.awt.event.ActionListener;
  10. import java.io.File;
  11. import java.io.FileReader;
  12. import java.io.FileWriter;
  13. import java.io.IOException;
  14. import javax.swing.JCheckBoxMenuItem;
  15. import javax.swing.JColorChooser;
  16. import javax.swing.JFileChooser;
  17. import javax.swing.JFrame;
  18. import javax.swing.JList;
  19. import javax.swing.JMenu;
  20. import javax.swing.JMenuBar;
  21. import javax.swing.JMenuItem;
  22. import javax.swing.JOptionPane;
  23. import javax.swing.JScrollPane;
  24. import javax.swing.JTextArea;
  25. import javax.swing.UIManager;
  26. import javax.swing.UnsupportedLookAndFeelException;
  27. import javax.swing.undo.UndoManager;
  28. /**
  29. * swing 简单记事本
  30. * @author kissy小强
  31. *
  32. */
  33. public class Note extends JFrame {
  34. private static final long serialVersionUID = 1L;
  35. UndoManager manager = new UndoManager();
  36. JTextArea text = new JTextArea();
  37. JFileChooser chooser;
  38. String s = "新建记事本";
  39. int result = 0;
  40. private File file;
  41. JMenuBar Mb;
  42. JMenu M1, M2, M3, M4;
  43. JMenuItem m11, m12, m13, m14, m15, m21, m22, m23, m24, m25, m32, m33, m41;
  44. private final static int HEIGHT= 450;
  45. private final static int WIDTH= 750;
  46. JCheckBoxMenuItem m31 = new JCheckBoxMenuItem( "自动换行", true);
  47. public static void main(String args[]) {
  48. String lookAndFeel = UIManager.getSystemLookAndFeelClassName();
  49. try {
  50. UIManager.setLookAndFeel(lookAndFeel);
  51. } catch (ClassNotFoundException | InstantiationException
  52. | IllegalAccessException | UnsupportedLookAndFeelException e1) {
  53. e1.printStackTrace();
  54. }
  55. EventQueue.invokeLater( new Runnable() {
  56. public void run() {
  57. try {
  58. Note n = new Note();
  59. } catch (Exception e) {
  60. e.printStackTrace();
  61. }
  62. }
  63. });
  64. }
  65. public Note() {
  66. setTitle(s);
  67. setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  68. Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
  69. // 离显示屏上边缘x像素,里显示屏左边缘y像素
  70. setLocation(screenSize.width / 2 - WIDTH / 2, screenSize.height / 2- HEIGHT / 2);
  71. setSize(WIDTH, HEIGHT);
  72. setResizable( true); // 窗体是否可变
  73. setVisible( true); // 窗体是否可见
  74. init();
  75. }
  76. private void init(){
  77. Mb = new JMenuBar();
  78. this.setJMenuBar(Mb);
  79. text.getDocument().addUndoableEditListener(manager); // 设置文本框编辑监听(可撤销)
  80. text.setFont( new Font( "宋体", Font.PLAIN, 14));
  81. text.setCaretColor(Color.RED); // 光标颜色
  82. text.setSelectedTextColor(Color.RED); // 选中字体颜色
  83. text.setSelectionColor(Color.GREEN); // 选中背景颜色
  84. text.setLineWrap( true); // 是否换行
  85. text.setWrapStyleWord( true); // 是否单词边界换行(即有空白)
  86. text.setMargin( new Insets( 3, 5, 3, 5)); // 文本区与边框的间距,四个参数分别为上、左、下、右
  87. text.setDragEnabled( true); // 开启或关闭自动拖动处理
  88. add( new JScrollPane(text, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
  89. JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED));
  90. M1 = this.AddBar( "文件", Mb);
  91. M2 = this.AddBar( "编辑", Mb);
  92. M3 = this.AddBar( "格式", Mb);
  93. M4 = this.AddBar( "帮助", Mb);
  94. /* 新建选项 */
  95. m11 = this.AddItem( "新建", M1);
  96. m11.addActionListener( new ActionListener() {
  97. public void actionPerformed(ActionEvent event) {
  98. text.setText( "");
  99. setTitle(s);
  100. file = null;
  101. }
  102. });
  103. /* 打开选项 */
  104. m12 = this.AddItem( "打开", M1);
  105. m12.addActionListener( new ActionListener() {
  106. public void actionPerformed(ActionEvent event) {
  107. try {
  108. // 设置打开时的默认目录,两种方式
  109. chooser = new JFileChooser( "C:\\Users\\xiaozhx\\Desktop");
  110. // chooser.setCurrentDirectory(new File("C:\\Users\\xiaozhx\\Desktop"));
  111. chooser.setFileFilter( new filter());
  112. result = chooser.showOpenDialog( null);
  113. if (result == JFileChooser.APPROVE_OPTION) {
  114. // 点击了打开按钮
  115. file = chooser.getSelectedFile();
  116. int length = ( int) file.length();
  117. FileReader reader = new FileReader(file);
  118. char[] ch = new char[length];
  119. reader.read(ch);
  120. reader.close();
  121. text.setText( new String(ch).trim());
  122. setTitle(file.getName());
  123. } else if (result == JFileChooser.CANCEL_OPTION) {
  124. // 点击了取消按钮
  125. }
  126. } catch (Exception e) {
  127. JOptionPane.showMessageDialog( null, e);
  128. }
  129. }
  130. });
  131. /* 保存选项 */
  132. m13 = this.AddItem( "保存", M1);
  133. m13.addActionListener( new ActionListener() {
  134. public void actionPerformed(ActionEvent event) {
  135. if (file == null)
  136. try {
  137. chooser = new JFileChooser(
  138. "C:\\Users\\xiaozhx\\Desktop");
  139. chooser.setFileFilter( new filter());
  140. result = chooser.showSaveDialog( null);
  141. if (result == JFileChooser.APPROVE_OPTION) {
  142. File selectfile = chooser.getSelectedFile(); // 获得文件名
  143. // 获得被选中的过滤器中的文件扩展名
  144. String end = chooser.getFileFilter().getDescription();
  145. File newFile = null;
  146. if (selectfile.getAbsolutePath().toUpperCase().endsWith(end.toUpperCase())) {
  147. // 如果文件是以选定扩展名结束的,则使用原名
  148. newFile = selectfile;
  149. } else {
  150. // 否则加上选定的扩展名
  151. newFile = new File(selectfile.getAbsolutePath()+ end);
  152. }
  153. try {
  154. if (newFile.exists() == false) {
  155. newFile.createNewFile();
  156. }
  157. FileWriter writer = new FileWriter(newFile);
  158. char[] arry = text.getText().toCharArray();
  159. writer.write(arry);
  160. writer.flush();
  161. writer.close();
  162. setTitle(newFile.getName());
  163. file = newFile;
  164. } catch (IOException e) {
  165. }
  166. } else if (result == JFileChooser.CANCEL_OPTION) {
  167. // 点击了取消按钮
  168. }
  169. } catch (Exception e) {
  170. JOptionPane.showMessageDialog( null, e);
  171. }
  172. else
  173. try {
  174. FileWriter writer = new FileWriter(file);
  175. char[] arry = text.getText().toCharArray();
  176. writer.write(arry);
  177. writer.flush();
  178. writer.close();
  179. } catch (Exception e) {
  180. JOptionPane.showMessageDialog( null, e);
  181. }
  182. }
  183. });
  184. /* 另存为选项 */
  185. m14 = this.AddItem( "另存为", M1);
  186. m14.addActionListener( new ActionListener() {
  187. public void actionPerformed(ActionEvent event) {
  188. try {
  189. chooser = new JFileChooser( "C:\\Users\\xiaozhx\\Desktop");
  190. chooser.setFileFilter( new filter());
  191. result = chooser.showSaveDialog( null);
  192. if (result == JFileChooser.APPROVE_OPTION) {
  193. File selectfile = chooser.getSelectedFile(); // 获得文件名
  194. // 获得被选中的过滤器中的文件扩展名
  195. String end = chooser.getFileFilter().getDescription();
  196. File newFile = null;
  197. if (selectfile.getAbsolutePath().toUpperCase().endsWith(end.toUpperCase())) {
  198. // 如果文件是以选定扩展名结束的,则使用原名
  199. newFile = selectfile;
  200. } else {
  201. // 否则加上选定的扩展名
  202. newFile = new File(selectfile.getAbsolutePath()+ end);
  203. }
  204. try {
  205. if (newFile.exists() == false) {
  206. newFile.createNewFile();
  207. }
  208. FileWriter writer = new FileWriter(newFile);
  209. char[] arry = text.getText().toCharArray();
  210. writer.write(arry);
  211. writer.flush();
  212. writer.close();
  213. setTitle(newFile.getName());
  214. file = newFile;
  215. } catch (IOException e) {
  216. }
  217. } else if (result == JFileChooser.CANCEL_OPTION) {
  218. // 点击了取消按钮
  219. }
  220. } catch (Exception e) {
  221. JOptionPane.showMessageDialog( null, e);
  222. }
  223. }
  224. });
  225. M1.addSeparator(); // 横杆
  226. /* 退出选项 */
  227. m15 = this.AddItem( "退出", M1);
  228. m15.addActionListener( new ActionListener() {
  229. public void actionPerformed(ActionEvent event) {
  230. System.exit( 0);
  231. }
  232. });
  233. /* 撤消选项 */
  234. m21 = this.AddItem( "撤消", M2);
  235. m21.addActionListener( new ActionListener() {
  236. public void actionPerformed(ActionEvent event) {
  237. if (manager.canUndo())
  238. manager.undo();
  239. }
  240. });
  241. /* 剪切选项 */
  242. M2.addSeparator();
  243. m22 = this.AddItem( "剪切", M2);
  244. m22.addActionListener( new ActionListener() {
  245. public void actionPerformed(ActionEvent event) {
  246. text.cut();
  247. }
  248. });
  249. /* 复制选项 */
  250. m23 = this.AddItem( "复制", M2);
  251. m23.addActionListener( new ActionListener() {
  252. public void actionPerformed(ActionEvent event) {
  253. text.copy();
  254. }
  255. });
  256. /* 粘贴选项 */
  257. m24 = this.AddItem( "粘贴", M2);
  258. m24.addActionListener( new ActionListener() {
  259. public void actionPerformed(ActionEvent event) {
  260. text.paste();
  261. }
  262. });
  263. /* 删除选项 */
  264. m25 = this.AddItem( "删除", M2);
  265. m25.addActionListener( new ActionListener() {
  266. public void actionPerformed(ActionEvent event) {
  267. text.replaceRange( "", text.getSelectionStart(),
  268. text.getSelectionEnd());
  269. }
  270. });
  271. /* 自动换行选项 */
  272. M3.add(m31);
  273. m31.addActionListener( new ActionListener() {
  274. public void actionPerformed(ActionEvent event) {
  275. if (m31.getState())
  276. text.setLineWrap( true);
  277. else
  278. text.setLineWrap( false);
  279. }
  280. });
  281. /* 字体格式设置选项 */
  282. m32 = this.AddItem( "字体选择", M3);
  283. m32.addActionListener( new ActionListener() {
  284. public void actionPerformed(ActionEvent event) {
  285. GraphicsEnvironment ge = GraphicsEnvironment
  286. .getLocalGraphicsEnvironment();
  287. //获取系统字体
  288. JList<String> fontNames = new JList<String>(ge.getAvailableFontFamilyNames());
  289. int response = JOptionPane.showConfirmDialog( null,
  290. new JScrollPane(fontNames), "请选择字体",
  291. JOptionPane.OK_CANCEL_OPTION);
  292. Object selectedFont = fontNames.getSelectedValue();
  293. if (response == JOptionPane.OK_OPTION && selectedFont != null)
  294. text.setFont( new Font(fontNames.getSelectedValue()
  295. .toString(), Font.PLAIN, 20));
  296. }
  297. });
  298. /* 字体颜色设置选项 */
  299. m33 = this.AddItem( "字体颜色", M3);
  300. m33.addActionListener( new ActionListener() {
  301. public void actionPerformed(ActionEvent event) {
  302. Color color = JColorChooser.showDialog( null, "文字颜色选择",
  303. Color.WHITE);
  304. text.setForeground(color);
  305. }
  306. });
  307. m41 = this.AddItem( "关于记事本", M4);
  308. m41.addActionListener( new ActionListener() {
  309. public void actionPerformed(ActionEvent event) {
  310. JOptionPane.showMessageDialog( null, "记事本\n开发语言:JAVA\n开发者:袁健强",
  311. "关于", JOptionPane.PLAIN_MESSAGE);
  312. }
  313. });
  314. }
  315. /** 文件格式过滤器 **/
  316. private class filter extends javax.swing.filechooser.FileFilter {
  317. public boolean accept(File file) {
  318. String name = file.getName();
  319. name.toLowerCase();
  320. if (name.endsWith( ".txt") || file.isDirectory())
  321. return true;
  322. else
  323. return false;
  324. }
  325. public String getDescription() {
  326. return ".txt";
  327. }
  328. }
  329. /** 将菜单项JMenuItem添加到菜单JMenu **/
  330. public JMenuItem AddItem(String name, JMenu menu) {
  331. JMenuItem MI = new JMenuItem(name);
  332. menu.add(MI);
  333. return MI;
  334. }
  335. /** 将菜单JMenu添加到菜栏JMenuBar **/
  336. public JMenu AddBar(String name, JMenuBar mb) {
  337. JMenu Mb = new JMenu(name);
  338. mb.add(Mb);
  339. return Mb;
  340. }
  341. }

  • 1
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值