Java记事本代码
import java.awt.Color;
import java.awt.FileDialog; import java.awt.FlowLayout; import java.awt.Font;
import java.awt.GridLayout; import java.awt.Toolkit;
import java.awt.Dimension; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*;
import java.util.*;
import java.text.SimpleDateFormat; import java.io.*;
public class My_Text {
public static void main(String args[]){
String s="我的记事本 ";
myText mainText=new myText(s);//实例化记事本对象
mainText.init();
mainText.setVisible(true);
}
}
//myText
class myText extends JFrame implements ActionListener{
String s;
JFrame frame=new JFrame();
JTextArea myTextArea=new JTextArea();
JMenuBar menubar=new JMenuBar();//菜单栏
String openedPath=null;
boolean opened=false;
boolean reworked=false;
JMenu file=new JMenu("文件(F)");
JMenu editor=new JMenu("编辑(E)");
JMenu format=new JMenu("
格式
pdf格式笔记格式下载页码格式下载公文格式下载简报格式下载
(O)");
JMenu help=new JMenu("帮助(H)");
JMenuItem newFile=new JMenuItem("新建(N)");
JMenuItem open=new JMenuItem("打开(O)...");
JMenuItem save=new JMenuItem("保存(S)");
JMenuItem saveAs=new JMenuItem("另存为(A)...");
JMenuItem exit=new JMenuItem("退出(X)");
JMenuItem cut=new JMenuItem("剪切(T)");
JMenuItem copy=new JMenuItem("复制(C)...");
JMenuItem paste=new JMenuItem("粘贴(P)");
JMenuItem find=new JMenuItem("查找(F)...");
JMenuItem findNext=new JMenuItem("查找下一个(N)");
JMenuItem replace=new JMenuItem("替换(R)");
JMenuItem selectAll=new JMenuItem("全选(A)");
JMenuItem timeDate=new JMenuItem("时间/日期(D)");
JCheckBoxMenuItem lineWrap=new JCheckBoxMenuItem("自动换行(M)");
JMenuItem font=new JMenuItem("字体(F)...");
JMenuItem color=new JMenuItem("字体颜色(C)...");
JMenuItem helpTopic=new JMenuItem("帮助主
题
快递公司问题件快递公司问题件货款处理关于圆的周长面积重点题型关于解方程组的题及答案关于南海问题
(H)");
JMenuItem about=new JMenuItem("关于记事本(A)");
JPopupMenu popupMenu=new JPopupMenu();
JMenuItem menuMouseCut=new JMenuItem ("剪切(T)");
JMenuItem menuMouseCopy=new JMenuItem ("复制(C)");
JMenuItem menuMousePaste=new JMenuItem (("粘贴"));
JMenuItem menuMouseDel=new JMenuItem (("删除"));
JMenuItem menuMouseSelectAll=new JMenuItem (("全选"));
//放的位置
public void init(){
//菜单栏
menubar.add(file);
menubar.add(editor);
menubar.add(format);
menubar.add(help);
file.add(newFile);//文件
file.add(open);
file.add(save);
file.add(saveAs);
file.addSeparator();
file.add(exit);
editor.addSeparator();//编辑
editor.add(cut);
editor.add(copy);
editor.add(paste);
editor.addSeparator();
editor.add(find);
editor.add(findNext);
editor.add(replace);
editor.addSeparator();
editor.add(selectAll);
editor.add(timeDate);
format.add(lineWrap);//格式
format.add(font);
format.add(color);
help.add(about);//帮助
help.add(helpTopic);
setJMenuBar(menubar);
//获得
内容
财务内部控制制度的内容财务内部控制制度的内容人员招聘与配置的内容项目成本控制的内容消防安全演练内容
窗格,并把文本域放到滚动组件中
getContentPane().add(new JScrollPane(myTextArea));
}
//构造
方法
快递客服问题件处理详细方法山木方法pdf计算方法pdf华与华方法下载八字理论方法下载
myText(String s){
super(s);
this.s=s;
setTitle(s);
int x,y;
Dimension size=Toolkit.getDefaultToolkit().getScreenSize();
x=(size.width-600)/2;
y=(size.height-400)/2;
setSize(600,400);
setLocation(x,y);
setMinimumSize(new Dimension(250,150));
setDefaultCloseOperation(EXIT_ON_CLOSE);
newFile.addActionListener(this);
open.addActionListener(this);
save.addActionListener(this);
saveAs.addActionListener(this);
exit.addActionListener(this);
cut.addActionListener(this);
copy.addActionListener(this);
paste.addActionListener(this);
find.addActionListener(this);
findNext.addActionListener(this);
replace.addActionListener(this);
selectAll.addActionListener(this);
timeDate.addActionListener(this);
lineWrap.addActionListener(this);
font.addActionListener(this);
color.addActionListener(this);
about.addActionListener(this);
helpTopic.addActionListener(this);
}
//添加组件的事件处理
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
Object source=e.getSource();//事件源
if(source==newFile)
{
myTextArea.setText("");
frame.setTitle(s);
}
else if(source==open)
{
FileDialog openFile=new FileDialog(frame,"打开",FileDialog.LOAD);
openFile.setVisible(true);
//得到文件目录和文件存入
String filePath=openFile.getDirectory()+openFile.getFile();
try
{
//通过打开filePath来建立一个FileInputStream
FileInputStream fis=new FileInputStream(filePath);
//返回下一次对此输入流调用的方法可以不受阻塞地从此输入流读取(或跳过)
//的估计剩余字节数,不太懂
byte [] content=new byte[fis.available()];
//从此输入流中将最多 b.length 个字节的数据读入一个 byte 数组中
fis.read(content);
myTextArea.setText(new String(content));
if(openFile.getFile()!=null)
{
frame.setTitle(openFile.getFile()+s);
openedPath=filePath;
opened=true;
}
fis.close();
}
catch(Exception ex)
{
ex.printStackTrace();
}
opened=true;
}
else if(source==save||source==saveAs)
{
String savePath=openedPath;
if(savePath==null||source==saveAs)
{
FileDialog saveFile=new FileDialog(frame,"保存",FileDialog.SAVE);
saveFile.setVisible(true);
savePath=saveFile.getDirectory()+saveFile.getFile();
}
try
{
//创建一个向具有指定名称的文件中写入数据的输出文件流fos
FileOutputStream fos=new FileOutputStream(savePath);
//使用平台的默认字符集将此 String 编码为 byte 序列,
//并将结果存储到一个新的 byte 数组中
fos.write(myTextArea.getText().getBytes());
fos.close();
}
catch(Exception ex)
{
ex.printStackTrace();
}
if(source==save)
openedPath=savePath;
}
else if(source==exit)
{
//System.exit(0);
dispose();//释放内存中占有的资源
}
else if(source==cut)
{
myTextArea.cut();
}
else if(source==copy)
{
myTextArea.copy();
}
else if(source==paste)
{
myTextArea.paste();
}
else if(source==find||source==findNext)
{
JFrame find=new JFrame("查找");
JLabel label=new JLabel("查找内容");
final JTextField text1=new JTextField(10);
JButton button=new JButton("查找");
find.setLayout(new FlowLayout());
find.add(label);
find.add(text1);
find.add(button);
find.setSize(300,90);
find.setLocation(300,200);
find.setVisible(true);
find.setResizable(false);//大小不可更改
button.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String src=text1.getText();
String desc=myTextArea.getText();
if(desc.indexOf(src)==-1)
{
JFrame result=new JFrame("查找失败");
JLabel label=new JLabel("未找到查询结果~");
result.add(label);
result.setSize(300,60);
result.setLocation(300,120);
result.setVisible(true);
result.setResizable(false);
}
else
{
int mark=0;
int start=desc.indexOf(src, mark);
mark=start+1;
if(mark!=0)
{
myTextArea.setSelectionStart(start);
myTextArea.setSelectionEnd(start+src.length());
}
}
}
}
);
}
else if(source==replace)
{
JFrame replace=new JFrame("替换");
JLabel label1=new JLabel("查找内容:");
JLabel label2=new JLabel("替换为:");
final JTextField text1=new JTextField(10);
final JTextField text2=new JTextField(10);
JButton button1=new JButton("替换");
JButton button2=new JButton("全部替换");
JPanel p1=new JPanel();
JPanel p2=new JPanel();
JPanel p3=new JPanel();
p1.add(label1);
p1.add(text1);
p2.add(label2);
p2.add(text2);
p3.add(button1);
p3.add(button2);
replace.setLayout(new GridLayout(3,1));
replace.add(p1);
replace.add(p2);
replace.add(p3);
replace.setSize(300,120);
replace.setLocation(300,200);
replace.setVisible(true);
replace.setResizable(false);
button1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String find=text1.getText();
String replace=text2.getText();
String desc=myTextArea.getText();
if(desc.indexOf(find)==-1)
{
JFrame result=new JFrame("查询失败");
JLabel label=new JLabel("未找到查询结果");
result.add(label);
result.setSize(300,60);
result.setLocation(300,120);
result.setVisible(true);
result.setResizable(false);
}
else{
myTextArea.select(desc.indexOf(find),
desc.indexOf(find)+find.length());
myTextArea.replaceSelection(replace);
}
}
}
);
button2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String find=text1.getText();
String replace=text2.getText();
String desc=myTextArea.getText();
if(desc.indexOf(find)==-1){
JFrame result=new JFrame("查询失败");
JLabel label=new JLabel("未找到查询结果");
result.add(label);
result.setSize(300,60);
result.setLocation(300,120);
result.setVisible(true);
result.setResizable(false);
}
else{
myTextArea.setText(myTextArea.getText().replaceAll(find,
replace));
}
}
});
}
else if(source==selectAll)
{
myTextArea.selectAll();
}
else if(source==timeDate)
{
Date nowTime=new Date();
SimpleDateFormat times=new SimpleDateFormat("yyyy年MM月dd日
hh:mm");
myTextArea.insert(times.format(nowTime),
myTextArea.getCaretPosition());
//insert(String,position),而getCaretPosition()会返回文本插入符的位
置
}
else if(source==lineWrap)
{
//以下两个有什么区别,~
//myTextArea.setLineWrap(true);
myTextArea.setLineWrap(!myTextArea.getLineWrap());
}
else if(source==font)
{
int i;
//JPanel pp=new JPanel();
JPanel pan1=new JPanel();
JPanel pan2=new JPanel();
JPanel pan3=new JPanel();
JPanel pan4=new JPanel();
JLabel lab1=new JLabel("字体:");
JLabel lab2=new JLabel("大小:");
JLabel lab3=new JLabel("字形:");
final JFrame f4=new JFrame("字体");
final JTextField sample=new JTextField("这是文字
样本
保单样本pdf木马病毒样本下载上虞风机样本下载直线导轨样本下载电脑病毒样本下载
~");
//final JLabel lab4=new JLabel("这是文字样本~"); //标签做文字样本有随着文字大小增大的效果
JButton ok=new JButton("确定");
JButton cancel=new JButton("取消");
final JComboBox fontName=new JComboBox();
final JComboBox fontStyle=new JComboBox();
final JComboBox fontSize=new JComboBox();
String[] font={"宋体","黑体","华文行楷","Arial","BatangChe",
"Time","Utopia"};
String[] size={"8","10","12","14","16","18","20","22","24",
"26","28","32","36","48","72","96"};
String[] style={"PLAIN","BOLD","ITALIC"};
for(i=0;i
本文档为【Java记事本代码】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑,
图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
该文档来自用户分享,如有侵权行为请发邮件ishare@vip.sina.com联系网站客服,我们会及时删除。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。