码迷,mamicode.com
首页 > 其他好文 > 详细

第十三周课程总结

时间:2019-11-23 16:39:24      阅读:82      评论:0      收藏:0      [点我收藏+]

标签:class   组件   jframe   sts   new t   NPU   except   package   npe   

记事本部分功能实现:

代码:

package zy123;

public class Text {

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

}
package zy123;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream ;

import javax.swing.*;

public class TextPad  implements ActionListener{
    JFrame frame;
    JMenuBar bar;
    JMenu fileMenu,editMenu;
    JMenuItem newItem, openItem, saveItem, saveasItem, closeItem;
    ImageIcon newIcon, openIcon, saveIcon, saveasIcon, closeIcon;
    JScrollPane scroll;
    JTextArea area;
    JFileChooser chooser;
    File file;
    
    public TextPad() {
        frame = new JFrame("记事本");
        bar = new JMenuBar();
        fileMenu = new JMenu("文件");
        editMenu = new JMenu("编辑");
        newIcon = new ImageIcon("d:" + File.separator + "Textpad" + File.separator + "1.jpg");
        openIcon = new ImageIcon("d:" + File.separator + "Textpad" + File.separator + "2.jpg");
        saveIcon = new ImageIcon("d:" + File.separator + "Textpad" + File.separator + "3.jpg");
        saveasIcon = new ImageIcon("d:" + File.separator + "Textpad" + File.separator + "4.jpg");
        closeIcon = new ImageIcon("d:" + File.separator + "Textpad" + File.separator + "5.jpg");
        newItem = new JMenuItem("新建(N)", newIcon);
        openItem = new JMenuItem("打开(O)", openIcon);
        saveItem = new JMenuItem("保存(S)", saveIcon); 
        saveasItem = new JMenuItem("另存为(A)", saveasIcon);
        closeItem = new JMenuItem("退出(x)",closeIcon);
        area=new JTextArea();
        scroll=new JScrollPane(area);
        
        newItem.setMnemonic(KeyEvent.VK_N);
        openItem.setMnemonic(KeyEvent.VK_O);
        saveItem.setMnemonic(KeyEvent.VK_S);
        saveasItem.setMnemonic(KeyEvent.VK_A);
        closeItem.setMnemonic(KeyEvent.VK_X);
        
        newItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, InputEvent.CTRL_DOWN_MASK));
        openItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, InputEvent.CTRL_DOWN_MASK));
        saveItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, InputEvent.CTRL_DOWN_MASK));
        saveasItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A, InputEvent.CTRL_DOWN_MASK));
        closeItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, InputEvent.CTRL_DOWN_MASK));

        fileMenu.add(newItem);
        fileMenu.add(openItem);
        fileMenu.add(saveItem);
        fileMenu.add(saveasItem);
        fileMenu.addSeparator();
        fileMenu.add(closeItem);
        
        newItem.addActionListener(this);
        openItem.addActionListener(this);
        saveItem.addActionListener(this);
        saveasItem.addActionListener(this);
        closeItem.addActionListener(this);
        
        bar.add(fileMenu);
        bar.add(editMenu);
        frame.setJMenuBar(bar);
        frame.add(scroll);
        frame.setSize(600, 400);
        frame.setVisible(true);

    }
    
    public void actionPerformed(ActionEvent event) {
        
        Object obj=event.getSource();
        if(obj instanceof JMenuItem) {
            JMenuItem item=(JMenuItem) obj;
            if(item==newItem) {
                new TextPad();
                
            }else if(item==openItem) {
                chooser=new JFileChooser();
                chooser.showOpenDialog(null);
                file=chooser.getSelectedFile();
                try {
                    
                    FileInputStream fis =new FileInputStream(file);
                    byte[]b = new byte[fis.available()];
                    fis.read(b);
                    area.append(new String(b));
                    
                    fis.close();
                    
                }catch(FileNotFoundException e) {
                    e.printStackTrace();
                    
                }catch(IOException e) {
                    e.printStackTrace();
                }
                
              }else if(item == saveItem) {
                  if(file!=null) {
                      try {
                          
                           PrintStream out = new PrintStream(new FileOutputStream(file));
                          out.print(this.area.getText());
                          out.close();
                      } catch (FileNotFoundException e) {
                          
                          e.printStackTrace();
                      }
                  }
                  
              }else if(item == saveasItem) {
                  chooser.showSaveDialog(null);
                  file = chooser.getSelectedFile();
                  if(!file.exists()) {
                      try {
                          
                          if(!file.exists()) {
                          file.createNewFile();
                          }
                          FileOutputStream fos=new FileOutputStream(file);
                          byte[] b = area.getText().getBytes();
                          fos.write(b);
                          fos.close();
                      } catch (IOException e) {
                          
                          e.printStackTrace();
                      
                  }
              }
          }else if(item == closeItem) {
              System.exit(1);
          }
       }
      
      }
 }
                

运行图:技术图片

技术图片

            按照老师上课的讲解以及代码照着写的 相当于自己重新复习了一遍 也更有了理解

本周课程总结:

事件处理监听处理

技术图片

菜单组件JMenu JMenuBar JMenuItem

技术图片
技术图片
技术图片
技术图片

图型界面内容大致就快已经结束了 又学到很多新的知识和语法用法

第十三周课程总结

标签:class   组件   jframe   sts   new t   NPU   except   package   npe   

原文地址:https://www.cnblogs.com/clearlove1215/p/11918168.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!