栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

制作一个记事本

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

制作一个记事本

目录

1. 为什么要使用SWT?

2. SWT应用程序的组成 

3.准备工作

4.部分功能演示

 5.代码部分

6.总结


 

1. 为什么要使用SWT?


  SWT是一个IBM开发的跨平台GUI开发工具包。在SWT之前,Sun已经提供了一个跨平台GUI开发工具包AWT (Abstract Windowing Toolkit)。虽然AWT框架也使用的是原生窗口部件(native widgets),但是它一直未能突破LCD问题。LCD问题导致了一些主要平台特征的遗失。如果你不明白的话,换句话说,如果平台A有窗口部件(widgets)1–40,而平台B有窗口部件(widgets)20–25,那么跨平台的AWT框架只能提供这两个窗口部件集的交集。 
 

2. SWT应用程序的组成 


  一个SWT应用程序的基本组成部分为显示界面(Display)、命令界面(Shell,使命令进入并使运行初始化)和窗口部件(Widgets)。Display负责管理事件循环和控制UI线程和其他线程之间的通讯。Shell是应用程序中被操作系统窗口管理器管理的窗口。每个SWT应用程序至少需要一个Display和大于等于1个的Shell实例

3.准备工作

        1.一些必要的java开发环境等,这里就不再做解释了

        2.你得有支持UI开发的环境,需要导入一个swt包,详情可以看这个

        http://t.csdn.cn/vyAW7

        3.新建一个项目                                   注:没有导入swt包是没有这个选项的

 

    4.在此目录下建一个包,在这个包下建一个类,最好选这个

    

 

4.部分功能演示

 

 

 

 

 

 5.代码部分

1.工具类

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;

public class utils {
	
	//显示对话框
	public static void showMessageDialog(Shell shell,String title,String content) {
		MessageBox md=new MessageBox(shell,SWT.NONE);
		md.setText(title);
		md.setMessage(content);
		md.open();
	}
	
	//全屏
	public static void showFullScreen(Shell shell) {
		shell.setBounds(0,0,shell.getDisplay().getBounds().width,shell.getDisplay().getBounds().height);
	}
	
}

2.读取文件

//打开一个文件,这个文件我是用对象流写的,可以用其他方法读取
public void widgetSelected(SelectionEvent e) {
				FileDialog fd=new FileDialog(shell,SWT.NONE);
				String s=fd.open();
				System.out.println(s);
				
				try {
					ObjectInputStream ois=new ObjectInputStream(new FileInputStream(new File(s)));
					Object obj=ois.readObject();
					if(obj instanceof List) {
						List li=(List)obj;
						int i=0;
						for(stu stu:li) {
							stu=li.get(i);
							
							        text.insert(stu.getId()+"t"+stu.getName()+"t"+stu.getAge()+"n");
							i++;
							if(i==30) {
								break;
							}
						}
					}
				} catch (ClassNotFoundException  | IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}

3.保存内容到文件

public void widgetSelected(SelectionEvent e) {
				FileDialog fd=new FileDialog(shell,SWT.SAVE);
				fd.setFilterExtensions(new String[] {".txt",".docx",".pdf",".jpg"});
				String s=fd.open();
				
				//System.out.println(s);
				try {
					BufferedWriter br=new BufferedWriter(new FileWriter(new File(s)));
					String values=text.getText();
					
					br.write(values);
					br.flush();
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});

5.退出

public void widgetSelected(SelectionEvent e) {
				boolean r=MessageDialog.openConfirm(shell,"退出","确定退出吗");
				if(r)
					shell.close();
			}
		});

6.颜色大小设置

public void widgetSelected(SelectionEvent e) {
				FontDialog fd=new FontDialog(shell,SWT.NONE);
				FontData fontdata=fd.open();
				Font font=new Font(shell.getDisplay(),fontdata);
				Color co=new Color(shell.getDisplay(),fd.getRGB().red,fd.getRGB().green,fd.getRGB().blue);
				
				text.setFont(font);
				text.setForeground(co);
			}
		});

7.显示时间

SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		lblNewLabel.setText("当前时间为:"+sdf.format(new Date()));

8.全部代码

import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.FontDialog;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.layout.FillLayout;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.ObjectInputStream;


import java.util.Date;
import java.util.List;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Text;

import com.ibm.icu.text.SimpleDateFormat;

import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.internal.win32.LOGFONT;

import jdbc1.stu;
import org.eclipse.wb.swt.SWTResourceManager;
public class 记事本 {

	protected Shell shell;
	private Text text;

	
	public static void main(String[] args) {
		try {
			记事本 window = new 记事本();
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	
	public void open() {
		Display display = Display.getDefault();
		createContents();
		shell.open();
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	
	protected void createContents() {
		shell = new Shell();
		shell.setImage(SWTResourceManager.getImage("D:\qqu6587u4EF6\images\IconTexto_WebDev_009.jpg"));
		shell.setSize(872, 447);
		shell.setText("u8BB0u4E8Bu672C");
		shell.setLayout(new FillLayout(SWT.VERTICAL));
		
		Menu menu = new Menu(shell, SWT.BAR);
		shell.setMenuBar(menu);
		
		MenuItem mntmNewSubmenu = new MenuItem(menu, SWT.CASCADE);
		mntmNewSubmenu.setText("u83DCu5355");
		
		Menu menu_1 = new Menu(mntmNewSubmenu);
		mntmNewSubmenu.setMenu(menu_1);
		
		MenuItem mntmNewItem = new MenuItem(menu_1, SWT.NONE);
		mntmNewItem.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				FileDialog fd=new FileDialog(shell,SWT.NONE);
				String s=fd.open();
				System.out.println(s);
				
				try {
					ObjectInputStream ois=new ObjectInputStream(new FileInputStream(new File(s)));
					Object obj=ois.readObject();
					if(obj instanceof List) {
						List li=(List)obj;
						int i=0;
						for(stu stu:li) {
							stu=li.get(i);
							//System.out.println(stu.getId()+"t"+stu.getName()+"t"+stu.getAge());
							text.insert(stu.getId()+"t"+stu.getName()+"t"+stu.getAge()+"n");
							i++;
							if(i==30) {
								break;
							}
						}
					}
				} catch (ClassNotFoundException  | IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});
		mntmNewItem.setText("u6587u4EF6");
		
		MenuItem mntmNewItem_1 = new MenuItem(menu_1, SWT.NONE);
		mntmNewItem_1.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				text.setText("");
			}
		});
		mntmNewItem_1.setText("u65B0u5EFA");
		
		MenuItem menuItem_2 = new MenuItem(menu_1, SWT.NONE);
		menuItem_2.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				FileDialog fd=new FileDialog(shell,SWT.SAVE);
				fd.setFilterExtensions(new String[] {".txt",".docx",".pdf",".jpg"});
				String s=fd.open();
				
				//System.out.println(s);
				try {
					BufferedWriter br=new BufferedWriter(new FileWriter(new File(s)));
					String values=text.getText();
					
					br.write(values);
					br.flush();
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});
		menuItem_2.setText("u53E6u5B58u4E3A");
		
		MenuItem mntmNewItem_2 = new MenuItem(menu_1, SWT.NONE);
		mntmNewItem_2.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				boolean r=MessageDialog.openConfirm(shell,"退出","确定退出吗");
				if(r)
					shell.close();
			}
		});
		mntmNewItem_2.setText("u9000u51FA");
		
		MenuItem menuItem = new MenuItem(menu, SWT.CASCADE);
		menuItem.setText("u8BBEu7F6E");
		
		Menu menu_2 = new Menu(menuItem);
		menuItem.setMenu(menu_2);
		
		MenuItem menuItem_1 = new MenuItem(menu_2, SWT.NONE);
		menuItem_1.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				FontDialog fd=new FontDialog(shell,SWT.NONE);
				FontData fontdata=fd.open();
				Font font=new Font(shell.getDisplay(),fontdata);
				Color co=new Color(shell.getDisplay(),fd.getRGB().red,fd.getRGB().green,fd.getRGB().blue);
				
				text.setFont(font);
				text.setForeground(co);
			}
		});
		menuItem_1.setText("u5B57u4F53");
		
		SashForm sashForm = new SashForm(shell, SWT.VERTICAL);
		
		text = new Text(sashForm, SWT.BORDER | SWT.WRAP | SWT.H_SCROLL | SWT.V_SCROLL | SWT.CANCEL | SWT.MULTI);
		
		Label lblNewLabel = new Label(sashForm, SWT.NONE);
		
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		lblNewLabel.setText("当前时间为:"+sdf.format(new Date()));
		sashForm.setWeights(new int[] {1, 1});

	}
}

6.总结

        自己做的一个小软件,不太难

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/998687.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号