×

java文本编辑器 编辑器

java文本编辑器(JAVA 文本编辑器)

admin admin 发表于2022-09-04 08:52:41 浏览101 评论0

抢沙发发表评论

本文目录

JAVA 文本编辑器


不知道你的编辑器控件是不是用的JTextArea,如果是的话,有getSelectionStart()和getSelectedEnd()来获取起始地址和结束地址,至于字数,getSelectedText()得到字符串,就可以得到字数了吧。
运行这段代码:
public class MyText2 extends JFrame {
private JTextArea editorArea;
private JLabel lb_StatusBar;
public MyText2() {
super();

Container container = getContentPane();
editorArea = new JTextArea();
editorArea.setLineWrap(true);
editorArea.addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent e) {
if(editorArea.getSelectedText() != null){
lb_StatusBar.setText(editorArea.getSelectionStart() + “ “ + editorArea.getSelectionEnd() + “ Length:“ + editorArea.getSelectedText().length()); //获取起始位置和结束位置,获取选取的字符串长度
}
}
});
JScrollPane scrollPane = new JScrollPane(editorArea);
container.add(scrollPane);

addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e){
dispose();
}
});
setTitle(“MyText“);
setSize(600, 400);
setVisible(true);
final JPanel statusBar = new JPanel();
final FlowLayout flowLayout = new FlowLayout();
flowLayout.setAlignment(FlowLayout.LEFT);
statusBar.setLayout(flowLayout);
getContentPane().add(statusBar, BorderLayout.SOUTH);
lb_StatusBar = new JLabel();
lb_StatusBar.setText(““);
statusBar.add(lb_StatusBar);
}
public static void main(String args) {
MyText2 app = new MyText2();
}
}

如何用java编写一个简单的文本编辑器


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
public class f1 extends Frame implements ActionListener
{
private MenuBar menubar=new MenuBar();
private Menu filemenu=new Menu(“文件“);
private Menu editmenu=new Menu(“编辑“);
private Menu formmenu=new Menu(“格式“);
private MenuItem itemf=new MenuItem;
private MenuItem iteme=new MenuItem;
private MenuItem items=new MenuItem;
private TextArea tf=new TextArea();
public int a=0,b=0,c=0,style=Font.PLAIN,size=15;
public String s1=“red:“+a+“ “+“green:“+b+“ “+“blue“+c,
s2=“宋体“;
public String sz1={“10“,“16“,“24“,“30“,“32“,“36“},
sz2={“宋体“,“黑体“,“幼圆“,“隶书“,“行楷“,“Arial“,“Georgia“},
sz3={“粗体“,“倾斜“,“常规“,“粗斜“};
JDialog dialog=new JDialog(this,“字体“,true);
Container cp=dialog.getContentPane();
JLabel lb=new JLabel;
JLabel lb1=new JLabel(s1,JLabel.LEFT);
JButton b1=new JButton(“确定“),
b2=new JButton(“取消“);
JComboBox jc1=new JComboBox(),
jc2=new JComboBox(),
jc3=new JComboBox();
JScrollBar jb1=new JScrollBar(JScrollBar.HORIZONTAL,10,5,0,260);
JScrollBar jb2=new JScrollBar(JScrollBar.HORIZONTAL,10,5,0,260);
JScrollBar jb3=new JScrollBar(JScrollBar.HORIZONTAL,10,5,0,260);

Java文本编辑器 查找与替换功能如何实现


最简单的就是将文本内容作使用String处理
只替换一次 : String.replace(“需要替换的字符串“,“替换的字符串“)
替换所有匹配字符 : String.replaceAll(“需要替换的字符串“,“替换的字符串“)
replcaeAll支持使用正则表达式。
-java文本编辑器

java文本编辑器FileEditor


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
public class f1 extends Frame implements ActionListener
{
private MenuBar menubar=new MenuBar();
private Menu filemenu=new Menu(“文件“);
private Menu editmenu=new Menu(“编辑“);
private Menu formmenu=new Menu(“格式“);
private MenuItem itemf=new MenuItem;
private MenuItem iteme=new MenuItem;
private MenuItem items=new MenuItem;
private TextArea tf=new TextArea();
public int a=0,b=0,c=0,style=Font.PLAIN,size=15;
public String s1=“red:“+a+“ “+“green:“+b+“ “+“blue“+c,
s2=“宋体“;
public
-编辑器

java编译文本编辑器


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class EditorJFrame extends JFrame implements ActionListener, ItemListener, MouseListener
{
private JTextField text_size; //字号文本行
private JCheckBox checkbox_bold, checkbox_italic; //粗体、斜体复选框
private JButton button_cut, button_copy, button_paste; //剪切、复制、粘贴按钮
private JTextArea textarea; //文本区
private JPopupMenu popupmenu; //快捷菜单
private JDialog dialog; //出错提示对话框
private JLabel label_dialog; //对话框中的标签

public EditorJFrame()
{
super(“文本编辑器“); //默认BorderLayout布局

this.setSize(500,300);
this.setLocation(300,240);
this.setDefaultCloseOperation(EXIT_ON_CLOSE); //单击窗口关闭按钮时,结束程序运行
textarea = new JTextArea(“TextArea“);
textarea.addMouseListener(this); //为文本区注册鼠标事件监听器
this.add(textarea); //文本区添加到框架的中部
JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT)); //面板为流布局,左对齐
this.add(panel,“North“); //面板添加到框架的北部
text_size = new JTextField(“12“,10);
panel.add(text_size);
text_size.addActionListener(this); //注册文本行的单击事件监听器
checkbox_bold = new JCheckBox(“粗体“); //复选框
panel.add(checkbox_bold);
checkbox_bold.addItemListener(this); //注册复选框的选择事件监听器

checkbox_italic = new JCheckBox(“斜体“);
panel.add(checkbox_italic);
checkbox_italic.addItemListener(this);
this.addmyMenu(); //调用自定义方法,添加菜单
this.setVisible(true);
}
private void addmyMenu() //添加主菜单、快捷菜单、对话框
{
JMenuBar menubar = new JMenuBar(); //菜单栏
this.setJMenuBar(menubar); //框架上添加菜单栏
JMenu menu_file = new JMenu(“文件“); //菜单
menubar.add(menu_file); //菜单栏中加入菜单
menu_file.add(new JMenuItem(“打开“)); //生成菜单项并加入到菜单
menu_file.add(new JMenuItem(“保存“));
menu_file.addSeparator(); //加分隔线

JMenuItem menuitem_exit = new JMenuItem(“退出“);
menu_file.add(menuitem_exit);
menuitem_exit.addActionListener(this); //为菜单项注册单击事件监听器

JMenu menu_edit = new JMenu(“编辑“);
menubar.add(menu_edit);
JMenu menu_style = new JMenu(“字形“);
menu_style.add(new JCheckBoxMenuItem(“粗体“)); //复选菜单项
menu_style.add(new JCheckBoxMenuItem(“斜体“));
menu_edit.add(menu_style); //菜单加入到菜单中成为二级菜单
JMenu menu_color = new JMenu(“颜色“);
menu_edit.add(menu_color);
ButtonGroup buttongroup = new ButtonGroup(); //按钮组
JRadioButtonMenuItem rbmi_red = new JRadioButtonMenuItem(“红“,true); //单选菜单项
buttongroup.add(rbmi_red); //单选菜单项添加到按钮组
menu_color.add(rbmi_red); //单选菜单项添加到菜单

JRadioButtonMenuItem rbmi_green = new JRadioButtonMenuItem(“绿“,true);
buttongroup.add(rbmi_green);
menu_color.add(rbmi_green);

JRadioButtonMenuItem rbmi_blue = new JRadioButtonMenuItem(“蓝“,true);
buttongroup.add(rbmi_blue);
menu_color.add(rbmi_blue);
menubar.add(new JMenu(“帮助“));

popupmenu = new JPopupMenu(); //弹出式菜单对象
JMenuItem menuitem_cut = new JMenuItem(“剪切“);
menuitem_cut.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,InputEvent.CTRL_MASK));//设置快捷键Ctrl+X
popupmenu.add(menuitem_cut); //加入剪切菜单项
menuitem_cut.addActionListener(this);

JMenuItem menuitem_copy = new JMenuItem(“复制“);
menuitem_copy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,InputEvent.CTRL_MASK));//设置快捷键Ctrl+C
popupmenu.add(menuitem_copy);
menuitem_copy.addActionListener(this);

JMenuItem menuitem_paste = new JMenuItem(“粘贴“);
menuitem_paste.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,InputEvent.CTRL_MASK));//设置快捷键Ctrl+V
popupmenu.add(menuitem_paste);
menuitem_paste.addActionListener(this);

textarea.add(popupmenu); //文本区添加快捷菜单
dialog = new JDialog(this,“提示“);
dialog.setSize(240,80);
label_dialog = new JLabel(““,JLabel.CENTER);
dialog.add(label_dialog);
dialog.setDefaultCloseOperation(HIDE_ON_CLOSE); //单击对话框的关闭按钮时,隐藏对话框而不结束程序运行
}

public void actionPerformed(ActionEvent e) //单击事件处理程序
{
if(e.getActionCommand()==“退出“) //不能用switch(int)语句
System.exit(0); //单击菜单项时结束程序
if(e.getActionCommand()==“剪切“)
textarea.cut(); //将选中文本剪切送系统剪贴板
if(e.getActionCommand()==“复制“)
textarea.copy();
if(e.getActionCommand()==“粘贴“)
textarea.paste();
if(e.getSource()==text_size) //单击文本行时,改变字号
{
int size=0;
try
{
size = Integer.parseInt(text_size.getText());
if (size《=0 || size》72)
throw new Exception(“SizeException“); //抛出异常对象
java.awt.Font font = textarea.getFont();
textarea.setFont(new Font(font.getName(),font.getStyle(),size));
}
catch(NumberFormatException nfe)
{
label_dialog.setText(“\““+text_size.getText()+“\“ 不能转换成整数,请重新输入!“);
dialog.setLocation(this.getX()+100,this.getY()+100);
dialog.setVisible(true);
}
catch(Exception ex)
{
if (ex.getMessage()==“SizeException“) //捕获自己抛出的异常对象
{
label_dialog.setText(size+“ 字号不合适,请重新输入!“);
dialog.setLocation(this.getX()+100,this.getY()+100);
dialog.setVisible(true);
}
}
finally{}
}
}

public void itemStateChanged(ItemEvent e) //复选框选择事件处理程序
{ //实现ItemListener接口中的方法
Font font = textarea.getFont();
int style = font.getStyle();
if (e.getSource()==checkbox_bold)
style = style ^ 1; //整数的位运算,异或^

if (e.getSource()==checkbox_italic)
style = style ^ 2;
textarea.setFont(new Font(font.getName(),style,font.getSize()));
}
public void mouseClicked(MouseEvent mec) //单击鼠标时触发
{ //实现MouseListener接口中的方法
if (mec.getModifiers()==mec.BUTTON3_MASK) //单击的是鼠标右键
popupmenu.show(textarea,mec.getX(),mec.getY());//在鼠标单击处显示快捷菜单
}
public void mousePressed(MouseEvent mep) { }
public void mouseReleased(MouseEvent mer) { }
public void mouseEntered(MouseEvent mee) { }
public void mouseExited(MouseEvent mex) { }
public void mouseDragged(MouseEvent med) { }
public static void main(String arg)
{
new EditorJFrame();
}
}
-java文本编辑器

能编译运行java的简单文本编辑器


import java.awt.BorderLayout;
public class WordProcessSystem extends JFrame{
private JFileChooser chooser;
private JTextArea textArea;
private File file;
private String string = ““;
private Font font;
public WordProcessSystem(){
super();
setTitle(“文字处理系统“);
setBounds(100, 100,600, 500);
getContentPane().setLayout(new BorderLayout());
getContentPane().setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu(“文件“);
JMenuItem menuItem = new JMenuItem(“新建“);
menuItem.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String str1 = JOptionPane.showInputDialog(“请输入新建文件名:“);
try {
file = new File(“E:\\“, str1 + “.txt“);
if(file.exists()){
JOptionPane.showMessageDialog(null, “文件名已存在!“);
}
else{
file.createNewFile();
}
} catch (Exception e1) {
// TODO: handle exception
}
}
});
chooser = new JFileChooser();
chooser.setCurrentDirectory(new File(“.“));
JMenuItem menuItem1 = new JMenuItem(“打开“);
menuItem1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
int result = chooser.showOpenDialog(null);
if(result == JFileChooser.APPROVE_OPTION){
String str = chooser.getSelectedFile().toString();
try {
file = new File(str);
readFromFile(file);
textArea.setText(string);
} catch (Exception e1) {
// TODO: handle exception
}
}
}
});
JMenuItem menuItem2 = new JMenuItem(“退出“);
menuItem2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.exit(0);
}
});
JMenuItem menuItem3 = new JMenuItem(“保存“);
menuItem3.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String string = textArea.getText();
try {
writeToFile(file, string);
} catch (Exception e1) {
// TODO: handle exception
}
}
});
menuItem3.addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent e){
if(e.getKeyChar() == KeyEvent.VK_C){
// if(e.getKeyChar() == KeyEvent.VK_S){
String string = textArea.getText();
try {
writeToFile(file, string);
} catch (Exception e1) {
// TODO: handle exception
}
// }
}
}
});
JMenuItem menuItem4 = new JMenuItem(“另存为“);
menuItem4.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String s1 = JOptionPane.showInputDialog(“请输入保存文件路径和文件名:“);
file = new File(s1);
System.out.println(file.getPath());
try {
writeToFile(file, string);
System.out.println(“aaaa“);
} catch (Exception e1) {
// TODO: handle exception
}
}

});

menu.add(menuItem);
menu.add(menuItem1);
menu.add(menuItem2);
menu.add(menuItem3);
menu.add(menuItem4);
JMenu menu2 = new JMenu(“格式“);
final JMenu menu3 = new JMenu(“字体“);
String fontString = {“宋体“,“楷体“,“隶书“};
final JMenuItem menu3_1 = new JMenuItem[fontString.length];
for(int i = 0;i 《 fontString.length;i++){
String changeString = ““;
menu3_1[i] = new JMenuItem(fontString[i]);
menu3_1[i].setActionCommand(changeString + i);
menu3_1[i].addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method
switch (e.getActionCommand().charAt(0)) {
case ’0’:
font = new Font(“宋体“,Font.PLAIN,20);
textArea.setFont(font);
break;
case ’1’:
font = new Font(“楷体“,Font.PLAIN,18);
textArea.setFont(font);
break;
case ’2’:
font = new Font(“隶书“,Font.BOLD,18);
textArea.setFont(font);
break;
default:
break;
}
}});
menu3.add(menu3_1[i]);
}

JMenu menu4 = new JMenu(“字号“);
String fontBig = {“10“,“15“,“20“,“25“,“30“,“35“,“40“};
final JMenuItem menu4_1 = new JMenuItem[fontBig.length];
for(int i = 0;i 《 fontBig.length;i++){
String changeString = ““;
menu4_1[i] = new JMenuItem(fontBig[i]);
menu4_1[i].setActionCommand(changeString + i);
menu4_1[i].addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method
switch (e.getActionCommand().charAt(0)) {
case ’0’:
font = new Font(menu3.getText(),Font.PLAIN,10);
textArea.setFont(font);
break;
case ’1’:
font = new Font(menu3.getText(),Font.PLAIN,15);
textArea.setFont(font);
break;
case ’2’:
font = new Font(menu3.getText(),Font.PLAIN,20);
textArea.setFont(font);
break;
case ’3’:
font = new Font(menu3.getText(),Font.PLAIN,25);
textArea.setFont(font);
break;
case ’4’:
font = new Font(menu3.getText(),Font.PLAIN,30);
textArea.setFont(font);
break;
case ’5’:
font = new Font(menu3.getText(),Font.PLAIN,35);
textArea.setFont(font);
break;
case ’6’:
font = new Font(menu3.getText(),Font.PLAIN,40);
textArea.setFont(font);
break;
default:
break;
}
}});
menu4.add(menu4_1[i]);
}
menu2.add(menu3);
menu2.add(menu4);
menuBar.add(menu);
menuBar.add(menu2);
setJMenuBar(menuBar);
textArea = new JTextArea();
textArea.setVisible(true);
// textArea.setFont(new Font(“宋体“,Font.BOLD,20));
add(textArea);
JScrollPane scrollPane = new JScrollPane(textArea);
scrollPane.setSize(600, 410);
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
add(scrollPane);
}
public void readFromFile(File f) throws IOException{
FileReader fReader = new FileReader(file);
BufferedReader bReader = new BufferedReader(fReader);
char data = new char;
int length = 0;
while((length = fReader.read(data)) 》 0){
string += new String(data,0,length);
}
// string = new String(““);
// while(true){
// String s1 = bReader.readLine();
// if(s1 == null){
// break;
// }
// string += (s1 + “\n“);
bReader.close();
fReader.close();
}
public void writeToFile(File file,String string) throws IOException{
if(!file.exists()){
file.createNewFile();
}
FileWriter fWriter = new FileWriter(file);
fWriter.write(string);
fWriter.close();
}
public static void main(String args) {
WordProcessSystem wordProcessSystem = new WordProcessSystem();
wordProcessSystem.setVisible(true);
}
}
-编辑器

对于初学者java编译器用文本编辑器还是用eclipse更好


强烈建议你使用文本编辑器,因为文本编辑器可以更好的去掌握Java基础的知识,一些细节的问题也更容易发现。
我想一开始写HelloWorld 的时候都出现过一些问题吧,使用文本编辑器出现了异常,然后去找原因,再出异常再找原因,经验就是这么一点点积累起来的。一个好的程序员首先是处理bug的能力。
等使用文本编辑器时间长了以后,敲代码顺手了,感觉使用文本编辑器有些不从心了,此时可以使用 eclipse 一类的工作去写代码了,此时你的 debug 的能力已经很好了。希望可以帮到你
-java文本编辑器

java编的简单文本编辑器!!急求!!!不要网上随便搜的复制粘贴的!!!


import java.awt.*;
import java.awt.event.*;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
public class EditorJFrame extends JFrame implements ActionListener, MouseListener
{
private JComboBox combox_name, combox_size; //字体、字号组合框
private JRadioButton radiob_color; //颜色单选按钮
private JTextArea text; //文本区
private Color color; //text的当前文本色
private JPopupMenu popupmenu; //快捷菜单
private JButton bopen,bsave; //保存和打开
private JFileChooser fchooser;
private Object file;
public EditorJFrame()
{
super(“文本编辑器“); //默认BorderLayout布局
Dimension dim = getToolkit().getScreenSize(); //获得屏幕分辨率
this.setBounds(dim.width/4,dim.height/4,dim.width/2,dim.height/4); //窗口居中

text = new JTextArea(“Welcome“);
text.addMouseListener(this); //文本区注册鼠标事件监听器
this.getContentPane().add(new JScrollPane(text)); //文本区添加到滚动窗格,滚动窗格添加到框架内容窗格中部
JToolBar toolbar=new JToolBar(); //创建工具栏,默认水平方向
this.getContentPane().add(toolbar,“North“); //工具栏添加到框架内容窗格北部
JButton bopen=new JButton(“打开“,new ImageIcon(“open.txt“));//添加
bopen.addActionListener(this);
toolbar.add(bopen);
JButton bsave=new JButton(“保存“,new ImageIcon(“save.txt“));
bsave.addActionListener(this);
toolbar.add(bsave);
//this.file=null;

String sizestr={“20“,“30“,“40“,“50“,“60“,“70“};
combox_size = new JComboBox(sizestr); //字号组合框
combox_size.addActionListener(this); //组合框注册单击事件监听器
toolbar.add(combox_size);
GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment();
String fontsName=ge.getAvailableFontFamilyNames(); //获得系统字体
combox_name = new JComboBox(fontsName); //组合框显示系统字体
combox_name.addActionListener(this); //组合框注册单击事件监听器
toolbar.add(combox_name);

String colorstr={“红“,“绿“,“蓝“,“黑“};
ButtonGroup bgroup_color = new ButtonGroup(); //按钮组
radiob_color = new JRadioButton[colorstr.length]; //颜色单选按钮数组
for (int i=0; i《radiob_color.length; i++)
{
radiob_color[i]=new JRadioButton(colorstr[i]); //颜色单选按钮
radiob_color[i].addActionListener(this);
bgroup_color.add(radiob_color[i]); //单选按钮添加到按钮组
toolbar.add(radiob_color[i]); //单选按钮添加到工具栏
}
radiob_color.setSelected(true); //设置单选按钮的选中状态
this.addmyMenu(); //调用自定义方法,添加菜单
this.setVisible(true);
}
private void addmyMenu() //添加主菜单、快捷菜单、对话框
{
JMenuBar menubar = new JMenuBar(); //菜单栏
this.setJMenuBar(menubar); //框架上添加菜单栏
String menustr={“文件“,“编辑“};
JMenu menu=new JMenu[menustr.length];
for (int i=0; i《menustr.length; i++)
{
menu[i] = new JMenu(menustr[i]); //菜单
menubar.add(menu[i]); //菜单栏中加入菜单
}
JMenuItem menuitem_exit = new JMenuItem(“新建“);
menu.add(menuitem_exit);
menuitem_exit.addActionListener(this);
JMenuItem menuitem_exit1 = new JMenuItem(“打开“);
menu.add(menuitem_exit1); //生成菜单项并加入到菜单
menuitem_exit1.addActionListener(this);
JMenuItem menuitem_exit2 = new JMenuItem(“保存“);
menu.add(menuitem_exit2);
menuitem_exit2.addActionListener(this);
JMenuItem menuitem_exit3 = new JMenuItem(“剪切“);
menu.add(menuitem_exit3); //生成菜单项并加入到菜单
menuitem_exit3.addActionListener(this);
JMenuItem menuitem_exit4 = new JMenuItem(“复制“);
menu.add(menuitem_exit4); //生成菜单项并加入到菜单
menuitem_exit4.addActionListener(this);
JMenuItem menuitem_exit5 = new JMenuItem(“粘贴“);
menu.add(menuitem_exit5); //生成菜单项并加入到菜单
menuitem_exit5.addActionListener(this);
}

public void actionPerformed(ActionEvent e) //单击事件处理方法
{
if(e.getActionCommand().equals(“新建“))
{
this.file=null;

this.text.setText(““);
}
if( e.getActionCommand().equals(“打开“))//打开文件时操作
{
try {
JFileChooser choosefile=new JFileChooser(“d:\\“);
FileNameExtensionFilter filefilter=new FileNameExtensionFilter(“.txt“,“txt“);
choosefile.setFileFilter(filefilter);//文件过滤器
int re=choosefile.showOpenDialog(null);
if(re==JFileChooser.APPROVE_OPTION)
{
String ioRecord=““;
File file=choosefile.getSelectedFile();
if(filefilter.accept(file))
{
FileReader readfile=new FileReader(file);
BufferedReader readfile_ln=new BufferedReader(readfile);
ioRecord = readfile_ln.readLine();
String s=““;
while(ioRecord!=null){
s+=ioRecord+“\n“;
ioRecord=readfile_ln.readLine();
}
text.setText(s);
}else JOptionPane.showMessageDialog(null, “选择文件类型不正确“);
}
} catch (FileNotFoundException q) {q.printStackTrace();}
catch(IOException q){ q.printStackTrace(); }
}
if (e.getSource() instanceof JRadioButton) //选择一个颜色复选框
{
if (e.getSource()==radiob_color)
color = new Color(255,0,0);
if (e.getSource()==radiob_color)
color = new Color(0,255,0);
if (e.getSource()==radiob_color)
color = new Color(0,0,255);
if (e.getSource()==radiob_color)
color = new Color(0,0,0);
text.setForeground(color); //设置文本区颜色
return;
}
if (e.getSource() instanceof JMenuItem) //单击菜单项
{
if (e.getActionCommand()==“剪切“)
text.cut(); //将选中文本剪切送系统剪贴板
if (e.getActionCommand()==“复制“)
text.copy(); //将选中文本复制送系统剪贴板
if (e.getActionCommand()==“粘贴“)
text.paste(); //将剪贴板的文本粘贴在当前位置
return;
}
if (e.getActionCommand()==“保存“)
{
//保存非空文件,不显示保存文件对话框
try{
FileWriter out=new FileWriter(“d:\\list.txt“);
out.write(text.getText());
out.close();
JOptionPane.showMessageDialog(null,“文件已经保存!“);
System.exit(0);
}
catch(Exception e1){ JOptionPane.showMessageDialog(null, “文件 IO 异常!“); }
}

if (e.getSource() instanceof JComboBox || e.getSource() instanceof JCheckBox )
{ //组合框、复选框
int size=0;
String fontname = (String)combox_name.getSelectedItem();//获得字体名
size = Integer.parseInt((String)combox_size.getSelectedItem());//获得字号
java.awt.Font font = text.getFont(); //获得文本区的当前字体对象
int style = font.getStyle(); //获得字形
text.setFont(new Font(fontname, style, size)); //设置文本区字体
}
}
//保存

public void mouseClicked(MouseEvent mec) //鼠标事件处理方法,实现MouseListener接口
{
if (mec.getModifiers()==MouseEvent.BUTTON3_MASK) //单击的是鼠标右键
popupmenu.show(text,mec.getX(),mec.getY()); //在鼠标单击处显示快捷菜单
}
public void mousePressed(MouseEvent mep) {}
public void mouseReleased(MouseEvent mer) {}
public void mouseEntered(MouseEvent mee) {}
public void mouseExited(MouseEvent mex) {}
public void mouseDragged(MouseEvent med) {}
public static void main(String arg)
{
new EditorJFrame();
}
}
-编辑器