[Java] 简单记事本 →→→→→进入此内容的聊天室

来自 , 2019-09-26, 写在 Java, 查看 130 次.
URL http://www.code666.cn/view/c88d8d0a
  1. package 我的记事本;
  2. import java.awt.*;
  3.  
  4. import javax.print.attribute.standard.Destination;
  5. import javax.swing.*;
  6. import java.awt.event.*;
  7. import java.io.*;
  8. public class Mytext extends JFrame implements ActionListener{
  9.         JTextArea myTextArea;
  10.         JScrollPane my;
  11.        
  12.         JMenuBar myMenuBar;
  13.         JMenu  file,edit,source;
  14.         JMenuItem New,Open,Save;
  15.         JMenuItem Cut,Copy;
  16.         JMenuItem test;
  17.         public static void main(String []args){
  18.                 Mytext a = new Mytext();
  19.                
  20.         }
  21.         Mytext(){
  22.                 myTextArea = new JTextArea();
  23.                 my = new JScrollPane(myTextArea);
  24.                
  25.                 //标题栏
  26.                 myMenuBar = new JMenuBar();
  27.                 file = new JMenu("文件");
  28.                 edit = new JMenu("编辑");
  29.                 source = new JMenu("资源");
  30.                 New = new JMenuItem("新建");
  31.                 New.setMnemonic('f');
  32.                 Open = new JMenuItem("打开");
  33.                 Open.addActionListener(this);
  34.                 Open.setActionCommand("open");
  35.                 Save = new JMenuItem("保存");
  36.                 Save.addActionListener(this);
  37.                 Save.setActionCommand("save");
  38.                 Cut = new JMenuItem("剪切");
  39.                 Copy = new JMenuItem("复制");
  40.                 test = new JMenuItem("测试",new ImageIcon("image/shan.jpg"));
  41.                 myMenuBar.add(file);
  42.                 myMenuBar.add(edit);
  43.                 myMenuBar.add(source);
  44.                 file.add(New);
  45.                 file.addSeparator();
  46.                 file.add(Open);
  47.                 file.addSeparator();
  48.                 file.add(Save);
  49.                
  50.                 edit.add(Cut);
  51.                 edit.addSeparator();
  52.                 edit.add(Copy);
  53.                
  54.                 source.add(test);
  55.                
  56.                 this.add(my);
  57.                 this.setTitle("记事本");
  58.                 this.setIconImage(new ImageIcon("image/tubiao2.jpg").getImage());
  59.                 this.setJMenuBar(myMenuBar);
  60.                 this.setSize(500,500);
  61.                 this.setLocation(100, 200);
  62.                 this.setResizable(true);
  63.                 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  64.                 this.setVisible(true);
  65.                
  66.         }
  67.         @Override
  68.         public void actionPerformed(ActionEvent e) {
  69.                 if(e.getActionCommand().equals("open")){
  70.                         //System.out.println("打开");
  71.                         JFileChooser mychoose  = new JFileChooser();
  72.                         mychoose.setDialogTitle("文件打开");
  73.                         mychoose.showOpenDialog(null);
  74.                         mychoose.setVisible(true);
  75.                         String category = mychoose.getSelectedFile().getAbsolutePath();
  76.                        
  77.                         BufferedReader Mytextbuf = null;
  78.                         FileReader mytext = null;
  79.                         try {
  80.                                  mytext = new FileReader(category);
  81.                                  Mytextbuf = new BufferedReader(mytext);
  82.                                  String msg ="";
  83.                                  String zfc ="";
  84.                                  while((msg =Mytextbuf.readLine()) != null){
  85.                                          zfc +=msg+"\n";
  86.                                  }
  87.                                  myTextArea.setText(zfc);
  88.                         } catch (Exception e1) {
  89.                                
  90.                                 e1.printStackTrace();
  91.                         }finally{
  92.                                 try {
  93.                                         mytext.close();
  94.                                         Mytextbuf.close();
  95.                                 } catch (IOException e1) {
  96.                        
  97.                                         e1.printStackTrace();
  98.                                 }
  99.                                
  100.                         }
  101.                        
  102.                 }
  103.                 if(e.getActionCommand().equals("save")){
  104.                        
  105.                         JFileChooser filedestination = new JFileChooser();
  106.                         filedestination.setDialogTitle("文件另存为");
  107.                         filedestination.showSaveDialog(this);
  108.                         filedestination.setVisible(true);
  109.                         String category = filedestination.getSelectedFile().getAbsolutePath();
  110.                         FileWriter destination =null;
  111.                         BufferedWriter destinationbuf = null;
  112.                         try {
  113.                                 destination  = new FileWriter(category);
  114.                                 destinationbuf  = new BufferedWriter(destination);
  115.                                 myTextArea.write(destinationbuf);
  116.                                
  117.                                
  118.                         } catch (IOException e1) {
  119.                                
  120.                                 e1.printStackTrace();
  121.                         }finally{
  122.                                 try {
  123.                                         destination.close();
  124.                                         destinationbuf.close();
  125.                                 } catch (IOException e1) {
  126.                                        
  127.                                         e1.printStackTrace();
  128.                                 }
  129.                         }
  130.                        
  131.                 }
  132.                
  133.         }
  134. }
  135.  
  136.  

回复 "简单记事本"

这儿你可以回复上面这条便签

captcha