java 01 02_Java实例开发01-02 文本阅读器

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();

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值