⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 example.java

📁 拥有文本编辑器的大部分功能.它是个英文版的简单文本编辑器
💻 JAVA
字号:
package swing;

import java.awt.*;

import javax.swing.*;

import java.awt.event.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.swing.event.*;

 class Frame1 extends JFrame 
 {
    Container c = new Container();
    JEditorPane content;
    JSplitPane splitPane;
    
    JToolBar jt=new JToolBar();//工具条

    Label la = new Label();
    
    JMenuBar jmb = new JMenuBar();  //新建菜单条

    JMenu file = new JMenu("File");  //文件

    JMenu edit = new JMenu("Edit");  //编辑

    JMenu option = new JMenu("Option"); //字体

    JMenu look = new JMenu("Look");//查看
    
    JMenu help = new JMenu("Help");//帮助
    
    JMenuItem New = new JMenuItem("New",new ImageIcon("new.gif"));//新建
    
    JMenuItem open = new JMenuItem("Open",new ImageIcon("open.gif"));//打开
    
    JMenuItem save = new JMenuItem("Save as",new ImageIcon("save.gif"));//另存为

    JMenuItem exit = new JMenuItem("Exit");//退出

    JMenuItem copy = new JMenuItem("Copy",new ImageIcon("copy.gif"));//复制

    JMenuItem cut = new JMenuItem("Cut",new ImageIcon("cut.gif"));//剪切

    JMenuItem paste = new JMenuItem("Paste",new ImageIcon("paste.gif"));//粘贴
    
    JMenuItem select = new JMenuItem("SelectAll");//全选

    JMenuItem delete = new JMenuItem("DeleteAll");//删除全部

    JMenuItem italic = new JMenuItem("Italic",new ImageIcon("Italic.gif"));//斜体

    JMenuItem bold = new JMenuItem("Bold",new ImageIcon("bold.gif"));//粗体

    JMenuItem version = new JMenuItem("About",new ImageIcon("about.gif"));//关于

    JMenuItem GroupItem=new JMenuItem("About study java",new ImageIcon("java.gif"));//关于学习Java
    
    JMenuItem helptheme=new JMenuItem("HelpTheme",new ImageIcon("help.gif"));
    
    JTextPane ta = new JTextPane();

    JFileChooser chooser = new JFileChooser();

    FileInputStream filestream = null;

    myversion exitversion = new myversion();
    
    study exitve = new study();

    public Frame1() 
    {
    	super("简单的文本编辑器");
        chooser.setSize(400, 350);
        chooser.setVisible(true);
        Font f = new Font("TimesRoman", Font.PLAIN, 16);
        c = this.getContentPane();
		c.add(ta, "Center");
        this.setJMenuBar(jmb);
        ta.setLayout(new FlowLayout(FlowLayout.LEFT));
        content=new JEditorPane();
		content.setEditable(false);
		content.setLayout(new FlowLayout(FlowLayout.RIGHT));
		
		c.validate();
		validate();
	
        jmb.setLayout(new FlowLayout(FlowLayout.LEFT));//居左的布局对象
        jmb.add(file);  //将文件加到菜单条中
        jmb.add(edit);  //将编辑加到菜单条中
        jmb.add(option);//将字体加到菜单条中
        jmb.add(look);  //将查看加到菜单条中
        jmb.add(help);  //将帮助加到菜单条中
        file.add(New);  //将新建加到文件项中
        file.add(open); //将打开加到文件项中
        file.add(save); //将另存为加到文件项中
        file.addSeparator();//分组条
        file.add(exit); //将退出加到文件项中
        edit.add(select);//将全选加到编辑项中
        edit.addSeparator();//分组条
        edit.add(copy);//将复制加到编辑项中
        edit.add(cut);//将剪切加到编辑项中
        edit.add(paste);//将粘贴加到编辑项中
        edit.addSeparator();//分组条
        edit.add(delete);//将删除全部加到编辑项中
        option.add(italic);//将斜体加到字体项中
        option.add(bold);  //将粗体加到字体项中
        look.add(version); //将关于加到查看项中
        look.add(GroupItem);//将关于学习Java加到查看项中
        help.add(helptheme);//将帮助主题加到帮助项中
        New.addActionListener(new NewListener());//新建事件
        open.addActionListener(new ListenActionForJfilechooser());//打开事件
        save.addActionListener(new ListenActionForJfilechooser());//另存为事件
        exit.addActionListener(new exitListener());//退出事件
        copy.addActionListener(new copyListener());//复制事件
        cut.addActionListener(new cutListener());//剪切事件
        paste.addActionListener(new pasteListener());//粘贴事件
        select.addActionListener(new selectListener());//全选事件
        delete.addActionListener(new deleteListener());//删除全部事件
        italic.addActionListener(new italicListener());//斜体事件
        bold.addActionListener(new boldListener());//粗体事件
        version.addActionListener(new showversion());//关于事件
        GroupItem.addActionListener(new showver());//关于学习Java事件
        ta.setFont(f);
        jt.add(new Label("wuhan university of science and engineering"));
        c.add(jt,BorderLayout.SOUTH);
        
		
    }

    class boldListener implements ActionListener 
    {
        public void actionPerformed(ActionEvent e) 
        {
            Font f = new Font("TimesRoman", Font.BOLD, 16);
            ta.setFont(f);
            
            
        }
    }

    class italicListener implements ActionListener 
    {
        public void actionPerformed(ActionEvent e) 
        {
            Font f = new Font("TimesRoman", Font.ITALIC, 16);
            ta.setFont(f);
        }
    }

    class deleteListener implements ActionListener 
    {
        public void actionPerformed(ActionEvent e) 
        {
            Font f = new Font("TimesRoman", Font.PLAIN, 16);
            ta.setText(null);
            ta.setFont(f);
        }
    }
    class selectListener implements ActionListener 
    {
        public void actionPerformed(ActionEvent e) 
        {
              ta.selectAll();
        }
    }

    class cutListener implements ActionListener 
    {
        public void actionPerformed(ActionEvent e) 
        {
              ta.cut();
        }
    }
    
    class NewListener implements ActionListener 
    {
        public void actionPerformed(ActionEvent e) 
        {
              ta.setText(null);
        }
    }


    class pasteListener implements ActionListener 
    {
        public void actionPerformed(ActionEvent e) 
        {
         ta.paste();
        }
    }

    class copyListener implements ActionListener 
    {
        public void actionPerformed(ActionEvent e) 
        {
        	
        	ta.copy();
            
        }
    }

    class exitListener implements ActionListener 
    {
        public void actionPerformed(ActionEvent e) 
        {
            System.exit(0);
        }
    }

    class ListenActionForJfilechooser implements ActionListener 
    {
        int result;

        File file;

        public void actionPerformed(ActionEvent e)   
        {
            
            if (e.getSource() == open) 
            {
                int state = chooser.showOpenDialog(null);
                file = chooser.getSelectedFile();
                if (file != null && state == JFileChooser.APPROVE_OPTION) 
                {
                    try 
                    {
                        filestream = new FileInputStream(file);
                        la.setText("打开的文件是:" + file.getName());
                    } 
                    catch (Exception e1) 
                    {
                        
                        e1.printStackTrace();
                    }
                    try 
                    {

                        ta.read(filestream, this);
                    } 
                    catch (Exception e1) 
                    {

                        e1.printStackTrace();
                    }
                }
            }
            if (e.getSource() == save) 
            {
                result = chooser.showSaveDialog(ta);
                file = null;
                if (result == JFileChooser.APPROVE_OPTION) 
                {
                    file = chooser.getSelectedFile();
                    la.setText("存储文件名为:" + file.getName());
                } 
                else if (result == JFileChooser.CANCEL_OPTION) 
                {
                    la.setText("您没有选择任何文件");
                }

                FileOutputStream fileOutStream = null;

                if (file != null) 
                {
                    try 
                    {
                        fileOutStream = new FileOutputStream(file);
                    } 
                    catch (FileNotFoundException fe) 
                    {
                        la.setText("File Not Found");
                        return;
                    }

                    String content = ta.getText();

                    try 
                    {
                        fileOutStream.write(content.getBytes());
                    } 
                    catch (IOException ioe) 
                    {
                        la.setText("写入文件错误");
                    } 
                    finally 
                    {
                        try 
                        {
                            if (fileOutStream != null)
                                fileOutStream.close();
                        } 
                        catch (IOException ioe2) 
                        {
                        }
                    }
                }
            }

        }
      }
    class showversion implements ActionListener 
    {

        public void actionPerformed(ActionEvent e) 
        {
            exitversion.setVisible(true);
        }
    }

    class myversion extends Frame 
    {

        Label l1 = new Label("Author :  JingLili ");

        Label l2 = new Label("2007.12.24");

        Label l3 = new Label("Tel:62033285");

        myversion() 
        {
            this.setLayout(new FlowLayout(FlowLayout.CENTER, 50, 20));

            this.add(l1);
            this.add(l2);
            this.add(l3);
            
            this.setBounds(200, 180, 300, 200);

            this.addWindowListener(new myWindowListener1());

        }
    }
     class myWindowListener1 extends WindowAdapter 
    {
        public void windowClosing(WindowEvent e) 
        {
            exitversion.setVisible(false);
        }
    }
    class showver implements ActionListener 
    {

        public void actionPerformed(ActionEvent e) 
        {
            exitve.setVisible(true);
        }
    }
    class study extends Frame 
    {

        Label l5 = new Label("With the development of our country ,the language of Java have been becomed more and more importment for us.");

        Label l6 = new Label("So we must  be good at it.");
        
        Label l7 = new Label("Therefore,we must study it very well.");
        study() 
        {
            this.setLayout(new FlowLayout(FlowLayout.CENTER, 50, 20));

            this.add(l5);
            
            this.add(l6);
            
            this.add(l7);
            
            this.setBounds(200, 180, 300, 200);

            this.addWindowListener(new myWindowListener2());

        }
    }

    class myWindowListener2 extends WindowAdapter 
    {
        public void windowClosing(WindowEvent e) 
        {
            exitve.setVisible(false);
        }
    }

    
    public static void main(String[] args) 
    {
    	
        Frame1 frame1 = new Frame1();
        frame1.setSize(500, 400);
        frame1.setVisible(true);
    }
}

 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -