zoukankan      html  css  js  c++  java
  • Java实例开发0102 文本阅读器

    这次采用的是SWT开发的,利用了Jigloo插件

    知识点:

    1.FileDialog

    2.文件的读写

    源代码:

    package chapter01.sample02;
    
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    
    import org.eclipse.swt.SWT;
    import org.eclipse.swt.events.SelectionAdapter;
    import org.eclipse.swt.events.SelectionEvent;
    import org.eclipse.swt.graphics.Point;
    import org.eclipse.swt.graphics.Rectangle;
    import org.eclipse.swt.layout.FillLayout;
    import org.eclipse.swt.layout.FormAttachment;
    import org.eclipse.swt.layout.FormData;
    import org.eclipse.swt.layout.FormLayout;
    import org.eclipse.swt.widgets.Composite;
    import org.eclipse.swt.widgets.Display;
    import org.eclipse.swt.widgets.FileDialog;
    import org.eclipse.swt.widgets.Label;
    import org.eclipse.swt.widgets.Menu;
    import org.eclipse.swt.widgets.MenuItem;
    import org.eclipse.swt.widgets.Shell;
    import org.eclipse.swt.widgets.Text;
    import org.eclipse.swt.widgets.ToolBar;
    import org.eclipse.swt.widgets.ToolItem;
    
    import com.cloudgarden.resource.SWTResourceManager;
    
    
    /**
    * This code was edited or generated using CloudGarden's Jigloo
    * SWT/Swing GUI Builder, which is free for non-commercial
    * use. If Jigloo is being used commercially (ie, by a corporation,
    * company or business for any purpose whatever) then you
    * should purchase a license for each developer using Jigloo.
    * Please visit www.cloudgarden.com for details.
    * Use of Jigloo implies acceptance of these licensing terms.
    * A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
    * THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
    * LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
    */
    public class FileReaderTool extends org.eclipse.swt.widgets.Composite {
    
        private Menu menu1;
        private Text text_file;
        private ToolItem toolItem_save;
        private ToolItem toolItem_edit;
        private ToolItem toolItem_open;
        private ToolBar toolBar1;
        private MenuItem aboutMenuItem;
        private MenuItem contentsMenuItem;
        private Menu helpMenu;
        private MenuItem helpMenuItem;
        private MenuItem exitMenuItem;
        private MenuItem closeFileMenuItem;
        private MenuItem saveFileMenuItem;
        private MenuItem newFileMenuItem;
        private MenuItem openFileMenuItem;
        private Menu fileMenu;
        private MenuItem fileMenuItem;
    
        {
            //Register as a resource user - SWTResourceManager will
            //handle the obtaining and disposing of resources
            SWTResourceManager.registerResourceUser(this);
        }
    
        public FileReaderTool(Composite parent, int style) {
            super(parent, style);
            initGUI();
        }
        
        /**
        * Initializes the GUI.
        */
        private void initGUI() {
            try {
                this.setSize(new org.eclipse.swt.graphics.Point(400,300));
                this.setBackground(SWTResourceManager.getColor(192, 192, 192));
                FormLayout thisLayout = new FormLayout();
                this.setLayout(thisLayout);
                this.setBackgroundImage(SWTResourceManager.getImage("img/jpg/8.jpg"));
                {
                    text_file = new Text(this, SWT.MULTI | SWT.WRAP | SWT.V_SCROLL);
                    FormData text_fileLData = new FormData();
                    text_fileLData.width = 378;
                    text_fileLData.height = 257;
                    text_fileLData.left =  new FormAttachment(0, 1000, 0);
                    text_fileLData.top =  new FormAttachment(0, 1000, 43);
                    text_fileLData.right =  new FormAttachment(1000, 1000, 0);
                    text_fileLData.bottom =  new FormAttachment(1000, 1000, 0);
                    text_file.setLayoutData(text_fileLData);
                    text_file.setVisible(false);
                }
                {
                    toolBar1 = new ToolBar(this, SWT.HORIZONTAL | SWT.H_SCROLL);
                    FormData toolBar1LData = new FormData();
                    toolBar1LData.width = 400;
                    toolBar1LData.height = 43;
                    toolBar1LData.left =  new FormAttachment(0, 1000, 0);
                    toolBar1LData.top =  new FormAttachment(0, 1000, 0);
                    toolBar1LData.right =  new FormAttachment(1000, 1000, 0);
                    toolBar1.setLayoutData(toolBar1LData);
                    {
                        toolItem_open = new ToolItem(toolBar1, SWT.NONE);
                        toolItem_open.setImage(SWTResourceManager.getImage("img/png/New Document.png"));
                        toolItem_open.addSelectionListener(new SelectionAdapter() {
                            public void widgetSelected(SelectionEvent evt) {
                                toolItem_openWidgetSelected(evt);
                            }
                        });
                    }
                    {
                        toolItem_edit = new ToolItem(toolBar1, SWT.NONE);
                        toolItem_edit.setImage(SWTResourceManager.getImage("img/png/Write Document.png"));
                        toolItem_edit.addSelectionListener(new SelectionAdapter() {
                            public void widgetSelected(SelectionEvent evt) {
                                toolItem_editWidgetSelected(evt);
                            }
                        });
                    }
                    {
                        toolItem_save = new ToolItem(toolBar1, SWT.NONE);
                        toolItem_save.setImage(SWTResourceManager.getImage("img/png/Web Browser.png"));
                        toolItem_save.addSelectionListener(new SelectionAdapter() {
                            public void widgetSelected(SelectionEvent evt) {
                                toolItem_saveWidgetSelected(evt);
                            }
                        });
                    }
                }
                {
                    menu1 = new Menu(getShell(), SWT.BAR);
                    getShell().setMenuBar(menu1);
                    {
                        fileMenuItem = new MenuItem(menu1, SWT.CASCADE);
                        fileMenuItem.setText("File");
                        {
                            fileMenu = new Menu(fileMenuItem);
                            {
                                openFileMenuItem = new MenuItem(fileMenu, SWT.CASCADE);
                                openFileMenuItem.setText("Open");
                            }
                            {
                                newFileMenuItem = new MenuItem(fileMenu, SWT.CASCADE);
                                newFileMenuItem.setText("New");
                            }
                            {
                                saveFileMenuItem = new MenuItem(fileMenu, SWT.CASCADE);
                                saveFileMenuItem.setText("Save");
                            }
                            {
                                closeFileMenuItem = new MenuItem(fileMenu, SWT.CASCADE);
                                closeFileMenuItem.setText("Close");
                            }
                            {
                                exitMenuItem = new MenuItem(fileMenu, SWT.CASCADE);
                                exitMenuItem.setText("Exit");
                            }
                            fileMenuItem.setMenu(fileMenu);
                        }
                    }
                    {
                        helpMenuItem = new MenuItem(menu1, SWT.CASCADE);
                        helpMenuItem.setText("Help");
                        {
                            helpMenu = new Menu(helpMenuItem);
                            {
                                contentsMenuItem = new MenuItem(helpMenu, SWT.CASCADE);
                                contentsMenuItem.setText("Contents");
                            }
                            {
                                aboutMenuItem = new MenuItem(helpMenu, SWT.CASCADE);
                                aboutMenuItem.setText("About");
                            }
                            helpMenuItem.setMenu(helpMenu);
                        }
                    }
                }
                this.layout();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        
        /**
        * Auto-generated main method to display this 
        * org.eclipse.swt.widgets.Composite inside a new Shell.
        */
        public static void main(String[] args) {
            Display display = Display.getDefault();
            Shell shell = new Shell(display,SWT.SHELL_TRIM);
            FileReaderTool inst = new FileReaderTool(shell, SWT.NULL);
            Point size = inst.getSize();
            shell.setLayout(new FillLayout());
            shell.setImage(SWTResourceManager.getImage("img/ico/Bookmark.ico"));
            shell.setText("\u6587\u672c\u9605\u8bfb\u5668");
            shell.layout();
            if(size.x == 0 && size.y == 0) {
                inst.pack();
                shell.pack();
            } else {
                Rectangle shellBounds = shell.computeTrim(0, 0, size.x, size.y);
                shell.setSize(shellBounds.width, shellBounds.height);
            }
            shell.open();
            while (!shell.isDisposed()) {
                if (!display.readAndDispatch())
                    display.sleep();
            }
        }
        
        private void toolItem_editWidgetSelected(SelectionEvent evt) {
            FileDialog fileDialog = new FileDialog(getShell(), SWT.OPEN);
            fileDialog.setText("Choose a file to read!");
            String[] fileFilters = new String[]{"*.txt","*.java","*.doc"};
            fileDialog.setFilterExtensions(fileFilters);
            String select = fileDialog.open();
            System.out.println(select);
            StringBuffer sBuffer = new StringBuffer();
            try {
                FileReader fileReader = new FileReader(select);
                BufferedReader bufferedReader = new BufferedReader(fileReader);
                String string = bufferedReader.readLine();
                while(string!=null){
                    sBuffer.append(string+"\n");
                    string = bufferedReader.readLine();
                }
                bufferedReader.close();
                fileReader.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            text_file.setText(sBuffer.toString());
            text_file.setVisible(true);
        }
        
        private void toolItem_openWidgetSelected(SelectionEvent evt) {
            text_file.setVisible(true);
        }
        
        private void toolItem_saveWidgetSelected(SelectionEvent evt) {
            String text = text_file.getText();
            System.out.println(text);
            FileDialog fileDialog = new FileDialog(getShell(), SWT.SAVE);
            String[] fileFilters = new String[]{"*.txt","*.java","*.doc"};
            fileDialog.setFilterExtensions(fileFilters);
            fileDialog.setText("Save a path to save the file");
            String selected = fileDialog.open( );
            System.out.println(selected);
            try {
                FileWriter fileWriter = new FileWriter(selected);
                BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
                bufferedWriter.write(text);
                bufferedWriter.close();
                fileWriter.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
    }

    界面显示:

    程序启动:

    1

    读取文件:

     2

    保存文件:

    3

    保存后的文件:

    4

    谢谢关注!

  • 相关阅读:
    最近正在计划编写一个麻将游戏
    连连看.NET v1.2版下载
    C#中使用指针转换数据类型[C#/unsafe]
    转载: 中国大部分程序员的通病
    C#休闲作品【连连看】开发心得与作品下载
    "连连看"算法笔记
    一个新游戏的思路;大家来说说看,觉得好的话,我做成游戏
    一个新游戏完成情况
    游戏演示(包含源码)[名称待定]
    Mzone 2.0换卡记
  • 原文地址:https://www.cnblogs.com/yinger/p/2171954.html
Copyright © 2011-2022 走看看