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

📄 mynotepad.java

📁 根据网上的资料写的记事本程序
💻 JAVA
字号:
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import org.eclipse.swt.widgets.*;
import org.eclipse.swt.*;
import org.eclipse.swt.events.*;
import java.io.*;



public class myNotepad {
	private static String fn="";

	private static Text text;
	/**
	 * Launch the application
	 * @param args
	 */
	public static void main(String[] args) {
		
		final Display display = Display.getDefault();
		final Shell shell = new Shell();
		shell.setSize(502, 381);
		shell.setText("SWT Application");
		
		shell.open();
		
		//new button
		final Button newButton = new Button(shell, SWT.NONE);
		newButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				fn="";
				shell.setText("New SWT program.");
				text.setText("");
				
			}
		});
		newButton.setText("new");
		newButton.setBounds(10, 10, 48, 22);
		
		//Open button
		final Button openButton = new Button(shell, SWT.NONE);
		openButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				FileDialog dlg=new FileDialog(shell,SWT.OPEN);
				String fileName=dlg.open();
				try{
					if(fileName!=null){
						//open specified file
						FileInputStream fis=new FileInputStream(fileName);
						text.setText("");
						BufferedReader in=new BufferedReader(new InputStreamReader(fis));
						String s=null;
						while((s=in.readLine())!=null)
							text.append(s+"\r\n");
					}
					if(fileName!=null){
						fn=fileName;
						shell.setText(fn);
						MessageBox successBox=new MessageBox(shell);
						successBox.setText("Message");
						successBox.setMessage("Open file success!");
						successBox.open();
						
					}
				}
				catch(Exception e1){
					MessageBox errorBox=new MessageBox(shell,SWT.ICON_ERROR);
					errorBox.setText("Error");
					errorBox.setMessage("Open file failed!");
					errorBox.open();
				}
			}
		});
		openButton.setText("open");
		openButton.setBounds(71, 10, 48, 22);

		//save button
		final Button saveButton = new Button(shell, SWT.NONE);
		saveButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				try
				{
					String fileName=null;
					if(fn.equals("")){
						FileDialog dlg=new FileDialog(shell,SWT.SAVE);
						fileName=dlg.open();
						if(fileName!=null){
							fn=fileName;
						}
					}
					if(fn!=""){
						FileOutputStream fos=new FileOutputStream(fn);
						OutputStreamWriter out=new OutputStreamWriter(fos);
						out.write(text.getText());
						out.close();
						shell.setText(fn);
						MessageBox successBox=new MessageBox(shell);
						successBox.setText("Message");
						successBox.setMessage("Save file success.");
						successBox.open();
					}
				}
				catch(Exception e1)
				{
					MessageBox errorBox=new MessageBox(shell,SWT.ICON_ERROR);
					errorBox.setText("Error");
					errorBox.setMessage("File save failed.");
					errorBox.open();
				}
			}
		});
		saveButton.setText("save");
		saveButton.setBounds(131, 10, 48, 22);

		text = new Text(shell, SWT.BORDER);
		text.setBounds(13, 38, 469, 293);
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
	}

}

⌨️ 快捷键说明

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