java 文本编辑器_Java编写的文本编辑器(菜鸟作品)
//这是主窗体文件 Wordwin.java
import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import java.awt.*;
import java.io.*;
import java.awt.event.*;
public class Wordwin extends JFrame implements DocumentListener {
/**
*
*/
private static final long serialVersionUID = 1L;
JMenuBar menubar=new JMenuBar();
JMenu file=new JMenu("文件");
JMenu edit=new JMenu("编辑");
JMenu geshi=new JMenu("格式");
JMenu look =new JMenu("查看");
JMenu help =new JMenu("帮助");
JTextArea wordArea=new JTextArea();
JScrollPane imgScrollPane = new JScrollPane(wordArea);
String [] str1={"新建","打开","保存","另存为","页面设置","打印","退出"};
String [] str2={"剪切","复制","粘贴","查找","替换"};
String [] str3={"自动换行","字体"};
Font f1=new Font("隶书",Font.PLAIN,15);
Search d1=new Search();
Font1 z1=new Font1();
Change c1=new Change();
int flag=0;
String source="";
public static void main(String[] args) {
JFrame wordwin=new Wordwin();
wordwin.setVisible(true);
}
public Wordwin(){
c1.set(wordArea);
z1.set(wordArea);
setTitle("文本编辑器");
Toolkit kit = Toolkit.getDefaultToolkit();
Dimension screenSize = kit.getScreenSize();//获取屏幕分辨率
setSize(screenSize.width/2,screenSize.height/2);//大小
setLocation(screenSize.width/4,screenSize.height/4);//位置
add(imgScrollPane,BorderLayout.CENTER);
setJMenuBar(menubar);
file.setFont(f1);
edit.setFont(f1);
geshi.setFont(f1);
look.setFont(f1);
help.setFont(f1);
menubar.add(file);
menubar.add(edit);
menubar.add(geshi);
menubar.add(look);
menubar.add(help);
wordArea.getDocument().addDocumentListener(this);
for(int i=0;i
JMenuItem item1= new JMenuItem(str1[i]);
item1.addActionListener(new MyActionListener1());
item1.setFont(f1);
file.add(item1);
}
for(int i=0;i
JMenuItem item2= new JMenuItem(str2[i]);
item2.addActionListener(new MyActionListener1());
item2.setFont(f1);
edit.add(item2);
}
for(int i=0;i
JMenuItem item3= new JMenuItem(str3[i]);
item3.addActionListener(new MyActionListener1());
item3.setFont(f1);
geshi.add(item3);
}
}
public void changedUpdate(DocumentEvent e) {
flag=1;
}
public void insertUpdate(DocumentEvent e) {
flag=1;
}
public void removeUpdate(DocumentEvent e) {
flag=1;
}
void open(){
FileDialog filedialog=new FileDialog(this,"打开",FileDialog.LOAD);
filedialog.setVisible(true);
String path=filedialog.getDirectory();
String name=filedialog.getFile();
if(path!=null && name!=null){
FileInputStream file = null;
try {
file = new FileInputStream(path+name);
} catch (FileNotFoundException e) {
}
InputStreamReader put =new InputStreamReader(file);
BufferedReader in=new BufferedReader(put);
String temp="";
String now = null;
try {
now = (String)in.readLine();
} catch (IOException e) {
e.printStackTrace();
}
while(now!=null){
temp+=now+"\r\n";
try {
now=(String)in.readLine();
} catch (IOException e) {
e.printStackTrace();
}
}
wordArea.setText(temp);
}
}
void save(){
FileDialog filedialog=new FileDialog(this,"保存",FileDialog.SAVE);
filedialog.setVisible(true);
if(filedialog.getDirectory()!=null && filedialog.getFile()!=null){
OutputStreamWriter out = null;
try {
out = new OutputStreamWriter(new FileOutputStream(filedialog.getDirectory()+filedialog.getFile()));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
out.write(wordArea.getText());
} catch (IOException e) {
e.printStackTrace();
}
flag=0;
try {
out.close();
source=wordArea.getText();
} catch (IOException e) {
e.printStackTrace();
}
}
}
void newfile(){
if(flag==0){
wordArea.setText("");
}
if(flag==1){
int m= JOptionPane.showConfirmDialog(this,"是否保存该文件");
if(m==0){
save();
wordArea.setText("");
}
if(m==1){
//System.exit(0);
wordArea.setText("");
flag=0;
}
}
}
void exit(){
if(flag==0){
System.exit(0);
}
if(flag==1){
int m= JOptionPane.showConfirmDialog(this,"是否保存该文件");
if(m==0){
save();
}
if(m==1){
System.exit(0);
}
}
}
class MyActionListener1 implements ActionListener{
public void actionPerformed(ActionEvent e) {
if(e.getSource()instanceof JMenuItem){
if(e.getActionCommand()=="剪切"){
wordArea.cut();
}
if(e.getActionCommand()=="复制"){
wordArea.copy();
}
if(e.getActionCommand()=="粘贴"){
wordArea.paste();
}
if(e.getActionCommand()=="查找"){
d1.setVisible(true);
}
if(e.getActionCommand()=="字体"){
z1.setVisible(true);
}
if(e.getActionCommand()=="替换"){
c1.setVisible(true);
}
if(e.getActionCommand()=="打开"){
open();
}
if(e.getActionCommand()=="保存"){
save();
}
if(e.getActionCommand()=="新建"){
newfile();
}
if(e.getActionCommand()=="退出"){
exit();
}
}
}
}
}
//这是查找替换的实现Change.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class Change extends JDialog{
/**
*
*/
private static final long serialVersionUID = 1L;
JLabel l1=new JLabel("查找内容");
JLabel l2=new JLabel("替换为");
JTextField t1=new JTextField(10);
JTextField t2=new JTextField(10);
JButton b1=new JButton("替换");
JButton b2=new JButton("全部替换");
JButton b3=new JButton("取消");
JTextArea a1=new JTextArea();
Font f1=new Font("隶书",Font.PLAIN,15);
//int m;
void set(JTextArea n){
a1=n;
}
public Change(){
setTitle("替换");
setSize(500,300);
setLocation(200,300);
setLayout(new FlowLayout());
l1.setFont(f1);
l2.setFont(f1);
t1.setFont(f1);
t2.setFont(f1);
b1.setFont(f1);
b2.setFont(f1);
b3.setFont(f1);
add(l1);
add(t1);
add(l2);
add(t2);
add(b1);
add(b2);
add(b3);
b1.addActionListener(new MyActionListener3());
b2.addActionListener(new MyActionListener3());
b3.addActionListener(new MyActionListener3());
}
class MyActionListener3 implements ActionListener{
public void actionPerformed(ActionEvent e2) {
int m;
String source=a1.getText();
String find=t1.getText();
String change=t2.getText();
if(e2.getActionCommand()=="取消"){
setVisible(false);
t1.setText("");
t2.setText("");
}
if(e2.getActionCommand()=="替换"){
m=source.indexOf(find,0);
String s1=source.substring(0,m);
String s2=source.substring(m+find.length());
source=s1+change+s2;
if(m==-1){
JOptionPane.showMessageDialog(null,"对不起,没找到您要找的内容!");
}else{
a1.setText(source);
}
}
if(e2.getActionCommand()=="全部替换"){
m=-change.length();
while(true){
m=source.indexOf(find,m+change.length());
if(m==-1)
break;
else{
String s1=source.substring(0,m);
String s2=source.substring(m+find.length());
source=s1+change+s2;
}
}
a1.setText(source);
}
}
}
}
//这是改变字体的Font1.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Font1 extends JDialog implements ItemListener{
/**
*
*/
private static final long serialVersionUID = 1L;
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
JPanel panel3 = new JPanel();
JComboBox comboBox1 = new JComboBox();
JComboBox comboBox2 = new JComboBox();
JComboBox comboBox3 = new JComboBox();
JLabel lab1=new JLabel("字体:");
JLabel lab2=new JLabel("字形:");
JLabel lab3=new JLabel("字号:");
String name=new String("宋体");
Font f1=new Font("隶书",Font.PLAIN,15);
int style=1;
int size=12;
String []array2=new String[]{"常 规","倾 斜","加 粗"};
String []array3=new String[]{"14","15","15","16","17","18"};
GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment();
String [] fontName=ge.getAvailableFontFamilyNames();
JButton b1=new JButton("确定");
JButton b2=new JButton("颜色");
JTextArea a1=new JTextArea();
void set(JTextArea n){
a1=n;
}
public Font1(){
setTitle("字体");
setSize(500,600);
setLayout(new FlowLayout());
//panel1.setLocation(100, 200);
lab1.setFont(f1);
lab2.setFont(f1);
comboBox1.setModel(new DefaultComboBoxModel(fontName));
comboBox1.setFont(f1);
for(int i=1;i
//comboBox1.setSelectedIndex(i);
comboBox1.setSelectedItem(fontName);
//comboBox1.addItem(fontName);
}
comboBox2.setModel(new DefaultComboBoxModel(array2));
comboBox2.setFont(f1);
for(int i=1;i
//comboBox2.setSelectedIndex(i);
comboBox2.setSelectedItem(array2);
//comboBox2.addItem(array2);
}
comboBox3.setModel(new DefaultComboBoxModel(array3));
comboBox3.setFont(f1);
for(int i=1;i
//comboBox2.setSelectedIndex(i);
comboBox2.setSelectedItem(array3);
//comboBox3.addItem(array3);
}
panel1.add(lab1);
panel1.add(comboBox1);
panel2.add(lab2);
panel2.add(comboBox2);
panel3.add(lab3);
panel3.add(comboBox3);
panel3.add(b1);
panel3.add(b2);
b2.addActionListener(new MyActionListener3());
comboBox1.addItemListener(this);
comboBox2.addItemListener(this);
comboBox3.addItemListener(this);
b1.addActionListener(new MyActionListener3());
add(panel1);
add(panel2);
add(panel3);
}
public void itemStateChanged(ItemEvent e) {
if(e.getSource()==comboBox1){
name=comboBox1.getSelectedItem().toString();
}
if(e.getSource()==comboBox2){
String s1=comboBox2.getSelectedItem().toString();
if(s1.equals("加粗")){
style=Font.BOLD;
}
if(s1.equals("倾斜")){
style=Font.ITALIC;
}
if(s1.equals("常规")){
style=Font.PLAIN;
}
}
if(e.getSource()==comboBox3){
String s2=comboBox3.getSelectedItem().toString();
size=Integer.parseInt(s2);
}
}
class MyActionListener3 implements ActionListener{
public void actionPerformed(ActionEvent e2) {
Font f=new Font(name,style,size);
a1.setFont(f);
if(e2.getActionCommand()=="颜色"){
setcolor();
}
}
}
void setcolor(){
Colorfontcolor=JColorChooser.showDialog(this,"字体颜色选择",a1.getForeground());
a1.setForeground(fontcolor);
}
}
//这是查找窗口,对不起,查找功能没实现Search.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Search extends JDialog{
/**
*
*/
private static final long serialVersionUID = 1L;
JLabel l1=new JLabel("查找内容");
JTextField t1=new JTextField(10);
JButton b1=new JButton("查找下一个");
JButton b2=new JButton("取消");
Font f1=new Font("隶书",Font.PLAIN,15);
public Search(){
setTitle("查找");
setSize(300,200);
setLayout(new FlowLayout());
b1.setFont(f1);
b2.setFont(f1);
l1.setFont(f1);
add(l1);
add(t1);
add(b1);
add(b2);
b2.addActionListener(new MyActionListener2());
}
class MyActionListener2 implements ActionListener{
public void actionPerformed(ActionEvent e1) {
setVisible(false);
}
}
}
运行截图:
java 文本编辑器_Java编写的文本编辑器(菜鸟作品)相关推荐
- java word文本框_Java 读取Word文本框中的文本、图片、表格
Word可插入文本框,文本框中可嵌入文本.图片.表格等内容.对文档中的已有文本框,也可以读取其中的内容.本文以Java程序代码来展示如何读取文本框,包括读取文本框中的文本.图片以及表格等. [程序环境 ...
- java制造病毒_java编写病毒的可行性分析
java编写病毒的可行性分析 最近心情十分郁闷,查阅一些病毒的资料消遣一下,居然发现这样的论调讲 java语言不可能编写病毒,在此特地反驳一下. 1 可执行 论调1:java需要依赖jre,无法在无j ...
- java 读取 文本块_Java 13:文本块
java 读取 文本块 Java 13已交付了期待已久的多行字符串或Text Blocks . 您不再需要连接跨越多行的字符串或转义特殊字符,这确实提高了代码的可读性. 文本块是一种预览语言功能 ,这 ...
- java unit包_Java接入UNIT文本对话处理源码详解
应邀一位网友的想法,想实现调用UNIT接口,实现文字对话功能,特整理一下内容分享给大家. 此功能对于大神来说非常简单,但是对于新手理解代码处理逻辑,并且如何解析UNIT返回参数的处理,还是有一定的帮助 ...
- java word 水印_Java 添加Word文本水印、图片水印
水印是一种常用于各种文档的声明.防伪手段,一般可设置文字水印或者加载图片作为水印.以下内容将分享通过Java编程给Word文档添加水印效果的方法,即 文本水印图片水印使用工具:Free Spire.D ...
- java jtextfield 密码_java中的文本输入框JTextField与JTextArea、密码输入框
文本输入框包括两种,单行文本输入框(JTextField) 和多行文本输入框 (JTextArea).密码输入框则只有一种(JPasswordField).JPasswordField 是 JText ...
- java程序步骤_java编写程序的步骤是什么?java编写程序步骤实例讲解
写java编程就是一步一步的来,这样才会写好一个编程,之后也才能正常的运行,那么java编写程序的步骤是什么?今天我们就来给大家讲解一下这方面的内容.大家可以参考以下文章! 1.编写源文件 使用文本编 ...
- java 定时删除_Java编写定时删除文件程序
Java编写定时删除文件程序 /*Java教程:http://www.javaweb.cc*/ import java.io.File; import java.util.Calendar; impo ...
- java 固定电话_Java 编写过滤手机号码或者固定电话的工具类
以下是分享自己编写的用于过滤手机号码.固定电话.黑名单的工具类TelCheckUtils, import java.util.HashSet; import java.util.Set; import ...
最新文章
- 阿里云K8S容器服务的使用
- 牲畜体表信息的三维重建
- 2019中国大学生程序设计竞赛(CCPC)-网络选拔赛-第七题Shuffle Card
- BZOJ.4180.字符串计数(后缀自动机 二分 矩阵快速幂/倍增Floyd)
- (二叉树的动态创建与bfs)树的层次遍历
- Swift coreAnimation 加计时器写的游戏《飞机大战》
- 爬虫404如何跳过_网络推广外包——网络推广外包专员如何从站内优化中提升网站收录...
- ABAQUS 有限元仿真分析软件模块介绍
- C++Windows连点器制作
- 苹果手机代数_苹果一共几代?
- (黎活明老师讲学)Android学习(一)---从网络获取图片
- Exp外贸/出口英文商城系统在国际电商贸易中的角色扮演
- 【成神之路】Redis相关面试题
- 英语语法回顾7——状语从句特殊用法
- 2022第十四届环泰山T60线上大徒步活动线下启动仪式圆满结束
- Adrealm:区块链的“快慢之道”|金色财经独家专访
- 浅析高层建筑消防安全难点 提出防控措施意见
- <Input />输入框及input的相关属性
- 2021高考北京大峪中学成绩查询,最新披露:京城30所中学高考成绩
- 执行git命令时,显示failed to push some refs to的解决方法
热门文章
- res-----不同图片实现动画效果(帧动画)
- 极米投屏h2服务器响应失败,排位赛总失败? 这样玩《王者荣耀》让你战绩飙升...
- 4.6 行高列宽的设置和跨列居中 [原创Excel教程]
- Kruskal算法与Prim算法
- 星露谷物语json_星露谷物语时间如何调节?
- 各地图平台的坐标拾取系统
- 我爱优雅的代码,我爱搞笑的诗词
- Mac上Pycharm激活
- MySQL 第三章3.1 数据库查询(where)
- python向上取整的方法_python 取整的两种方法,python向上取整的方法,问题简介: 要把一...