swing应用-简单记事本
实现文件创建,打开,简单界面设置
恶搞一下,下面进入正题,show you the code
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.PrintStream;
import java.util.Scanner;
import java.awt.event.WindowAdapter ;import javax.swing.JLabel ;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class MyMenu {private JFrame frame = null ;private String fileIcon = "d:"+File.separator+"icon"+File.separator+"file.png" ;private String newIcon = "d:"+File.separator+"icon"+File.separator+"new.png" ;private String openIcon = "d:"+File.separator+"icon"+File.separator+"open.png" ;private String helpIcon = "d:"+File.separator+"icon"+File.separator+"help.png" ;private String saveIcon = "d:"+File.separator+"icon"+File.separator+"save.png" ;/** 设置字体图标路径*/private String fontIcon = "d:"+File.separator+"icon"+File.separator+"font.png" ; private String yaIcon = "d:"+File.separator+"icon"+File.separator+"yahei.png" ;private String kaiIcon = "d:"+File.separator+"icon"+File.separator+"kaiti.png" ;private String songIcon = "d:"+File.separator+"icon"+File.separator+"songti.png" ;/** 设置背景图标路径*/private String bIcon = "d:"+File.separator+"icon"+File.separator+"background.png" ;private String yellowIcon = "d:"+File.separator+"icon"+File.separator+"yellow.png" ;private String orangeIcon = "d:"+File.separator+"icon"+File.separator+"orange.png" ;private String greenIcon = "d:"+File.separator+"icon"+File.separator+"green.png" ;private String blackIcon = "d:"+File.separator+"icon"+File.separator+"black.png" ;private String whiteIcon = "d:"+File.separator+"icon"+File.separator+"white.png" ;private JTextArea text = null ;public MyMenu() {this.frame = new JFrame("手撸版本记事本") ;this.text = new JTextArea(20,20) ;Container con = this.frame.getContentPane() ;con.add(new JScrollPane(text)) ;//con.add(text) ;JMenu file = new JMenu("文件") ;file.setIcon(new ImageIcon(fileIcon)) ;JMenu help = new JMenu("帮助") ;help.setIcon(new ImageIcon(helpIcon)) ;JMenu font = new JMenu("字体") ;font.setIcon(new ImageIcon(fontIcon));JMenuBar menubar = new JMenuBar() ;menubar.add(file) ;menubar.add(help) ;menubar.add(font) ;//设置字体按钮以及响应事件JMenuItem songti = new JMenuItem("宋体",new ImageIcon(songIcon)) ;JMenuItem kaiti = new JMenuItem("楷体",new ImageIcon(kaiIcon)) ;JMenuItem yahei = new JMenuItem("雅黑",new ImageIcon(yaIcon)) ;font.add(songti) ;font.add(kaiti) ;font.add(yahei) ;kaiti.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {Font f = new Font("楷体",Font.BOLD,15) ;text.setFont(f);}}) ;songti.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {Font f = new Font("新宋体",Font.PLAIN,15) ;text.setFont(f);}}) ;yahei.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {Font f = new Font("微软雅黑",Font.BOLD,15) ;text.setFont(f);}});/*设置背景颜色* 及其响应事件*/JMenu bcolor = new JMenu("背景色") ;bcolor.setIcon(new ImageIcon(bIcon)) ;JMenuItem byellow =new JMenuItem("黄色",new ImageIcon(yellowIcon)) ;JMenuItem borange = new JMenuItem("橙色",new ImageIcon(orangeIcon)) ;JMenuItem bgreen = new JMenuItem("绿色",new ImageIcon(greenIcon)) ;JMenuItem bblack = new JMenuItem("黑色",new ImageIcon(blackIcon)) ;JMenuItem bwhite = new JMenuItem("白色",new ImageIcon(whiteIcon)) ;menubar.add(bcolor) ;bcolor.add(bwhite) ;bcolor.add(byellow) ;bcolor.add(borange) ;bcolor.add(bgreen) ;bcolor.add(bblack) ;byellow.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {text.setBackground(Color.yellow);}}) ;borange.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {text.setBackground(Color.orange);}}) ;bgreen.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {text.setBackground(Color.green);}}) ;bblack.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {text.setBackground(Color.black);JOptionPane.showMessageDialog(null, "什么都看不见了吧","逗比",JOptionPane.ERROR_MESSAGE);}}) ;bwhite.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {text.setBackground(Color.white);JOptionPane.showMessageDialog(null, "你一定是刚才设置成黑色背景了","逗比",JOptionPane.WARNING_MESSAGE);}});/*接下来开始设置file按钮**/JMenuItem openItem = new JMenuItem("打开",new ImageIcon(openIcon)) ;JMenuItem newItem = new JMenuItem("新建",new ImageIcon(newIcon)) ;JMenuItem saveItem = new JMenuItem("保存",new ImageIcon(saveIcon)) ;JMenuItem helpItem = new JMenuItem("联机帮助",new ImageIcon(helpIcon)) ;openItem.addActionListener(new ActionListener() { //打开文件选项public void actionPerformed(ActionEvent e) {JFileChooser jfc = new JFileChooser() ;File file = null ; //等待打开文件对象实例化jfc.setApproveButtonText("确定");jfc.setDialogTitle("逗比要打开文件了,呵呵"); //设置打开文件对话框标题int result = jfc.showOpenDialog(frame) ; //记录打开操作if(result == JFileChooser.APPROVE_OPTION) {file = jfc.getSelectedFile() ; //text.setText("选择的文件为"+file.getName()+"\n") ;text.append("内容: ") ;} else if(result ==JFileChooser.CANCEL_OPTION) {text.setText("没有选择任何文件") ; } else {text.setText("没有选择任何文件") ;}if(file != null) {try {Scanner sc = new Scanner(file) ;sc.useDelimiter("\n") ;while(sc.hasNext()) {text.append(sc.next());text.append("\n");}sc.close() ;} catch(Exception el){}}}});saveItem.addActionListener(new ActionListener() { //响应保存按钮public void actionPerformed(ActionEvent e) {File file = null ;JFileChooser jfc = new JFileChooser() ;int result = jfc.showSaveDialog(frame) ;if(result == JFileChooser.APPROVE_OPTION) {file = jfc.getSelectedFile() ;} else if(result == JFileChooser.CANCEL_OPTION) {JOptionPane.showMessageDialog(null,"没有选择存储文件","逗比!",JOptionPane.ERROR_MESSAGE) ; } else {JOptionPane.showMessageDialog(null,"逗比,操作错误!","猴子的救兵!",JOptionPane.WARNING_MESSAGE) ;}if(file != null) {try {PrintStream out = new PrintStream(file) ;out.println(text.getText()) ;JOptionPane.showMessageDialog(null,"文件保存成功","反馈",JOptionPane.WARNING_MESSAGE);out.close() ;}catch(Exception ell) {}}}});helpItem.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {Font font = new Font("楷体",Font.BOLD,20) ;JLabel labelqq = new JLabel("加帅哥qq:653866417") ;labelqq.setFont(font) ;JOptionPane.showMessageDialog(null, labelqq,"哎呦我去!",JOptionPane.WARNING_MESSAGE);}}) ;/**讲所有下拉菜单加入file**/file.add(openItem) ;file.add(newItem) ;file.add(saveItem) ;help.add (helpItem) ;frame.setJMenuBar(menubar);frame.setSize(500,400);frame.setLocation(200,200);frame.setVisible(true);frame.addWindowListener(new WindowAdapter() {public void windowClosing(ActionEvent e) {System.exit(1) ;}});}}class Tester {public static void main(String args[]) {new MyMenu() ;}
}
swing应用-简单记事本相关推荐
- 简单记事本及目录树形图的Java实现
用Java实现一个简单的记事本,使其可以通过给定文件的绝对路径打开文件,当该文件名是目录时,则打开一个目录并浏览该目录下的文件结构,即目录树形图. 实现原理也不难,如下: 1.判断给定的绝对路径是否为 ...
- 记事本写python怎么运行-Python开发简单记事本
摘要: 本文是使用Python,结合Tkinter开发简单记事本. 本文的操作环境:ubuntu,Python2.7,采用的是Pycharm进行代码编辑,个人很喜欢它的代码自动补齐功能. 最近很想对p ...
- 用python编写最简单的记事本_Python使用字典实现的简单记事本功能示例
本文实例讲述了Python使用字典实现的简单记事本功能.分享给大家供大家参考,具体如下: from time import sleep, strftime user = "Cytus&quo ...
- Java简单记事本设计实验报告_java记事本实验报告
期末考查大作业任务书 课程名称: Java 语言程序设计设计实验 题 目: 简单记事本得设计与实现 学 院: 管理学院 系: 信息管理系 专业班级: 学 号: 学生姓名: 起讫日期: 2015-09- ...
- python语言的记事本在哪_用python语言编写一个简单记事本
看了一点python的基础教程,忍不住手就痒了,找来一个题目练一下喽. 题目:编写一个功能简单的记事本. 编写记事本就要用到GUI的功能,最常用的当然是wxpython,那么我们就用这个来写一个记事本 ...
- Java Swing的简单学习
计算机软件实习记录(第一周)--Java Swing的简单学习 Swing的认识 1)Swing是一个用于开发Java应用程序用户界面的开发工具包(GUI工具包),是JAVA基础类的一部分. 2)它包 ...
- Android 开发简单记事本程序(附源码)
简单介绍一下功能:简单记事本只能添加文字内容,首页用ListView显示所有保存的事项和保存的时间,添加页面添加内容,点击首页的ListView可以查看内容,删除内容. 先看一下运行效果: 先建立数据 ...
- 【Python小案例教程1】Python开发简单记事本
####前言: 本文是使用Python,结合Tkinter开发简单记事本. ####基本环境配置: Python版本:2.7 编辑器:pycharm ####实现效果: ####主要思路: 我们可以看 ...
- android简单记事本
android简单记事本 近段学习数据存储写个小程序,简单记事本,实现点击查看删除,添加保存.闲话不多说直接步入正题. 一.上图看着更直观. 1.这是该程序的主界面.上边是一个Button,下边是Li ...
最新文章
- ZigBee On Windows Mobile—利用CF卡接口外扩
- python编程面试题
- SPOJ1812(后缀自动机求n个串的最长公共子串)
- 其他手机有机会用鸿蒙,即将发布的荣耀50系列手机可以有机会用鸿蒙系统吗?...
- 谈一下我对如何设计微服务接口的理解和思考
- python28 excel读取模块xlrd
- 浓郁东方民俗剪纸风新年春节海报,psd分层模板
- ceb怎么转换成word_如何将ceb文件转成word_ceb文件转成word的详细步骤-系统城
- 32位和64位CPU操作系统的区别
- 中央空调系统运行原理以及相关设备介绍
- java程序设计特点_Java编程语言的特点
- CP 15 协处理器
- CF-133A - HQ9+
- 语音处理:音频信号短时平稳性分析
- 普林斯顿大学计算机科学排名,普林斯顿大学计算机科学与信息系统世界排名2020年最新排名第11(QS世界排名)...
- c语言file和fp未声明标识符,c语言定义文件指针时(FILE *fp)一定要写*fp吗、可以换成别的字母不...
- Chrome 广告屏蔽功能不影响浏览器性能
- Deepmind AMA:关于最强ALphaGo如何炼成的真心话,都在这里了!
- 分页总数计算,根据总记录数获取分页总数_ZZGoals
- 百胜图Barsetto咖啡理想生活咖啡陪伴
热门文章
- python异步socket接收_Python简易聊天工具-基于异步Socket通信
- 淘宝开网店的详细教程
- 河北欧格教育:商家开店铺怎么运营
- 计算道路曲线要素的小程序
- shell脚本--重启nohup后台运行的程序
- 微博如何做副业?微博上能做兼职吗?如何用微博兼职
- 小米路由器MINI 潘多拉迅雷远程/xunlei安装教程
- 农业生产智能巡检机器人系统
- java匿名类构造方法_Android(Java) | 你知道吗?Java匿名内部类其实是有“名字”和构造方法的!...
- 青岛农商银行计算机防病毒应用培训,农信银资金清算中心——青岛农商银行举办2020年新入职员工培训班...