谁知道这个文本编辑器用JAVA怎么写~急!!!
最近要设计一个文本编辑器。它上面是菜单条,菜单条下面是一个多行文本框。菜单条包含二个菜单,File、Util,其中,File菜单又包括菜单项open,save。当选择op...
最近要设计一个文本编辑器。它上面是菜单条,菜单条下面是一个多行文本框。菜单条包含二个菜单,File、Util, 其中,File菜单又包括菜单项open, save。当选择open时,会弹出文件对话框,让用户选择指定的文件(文本文件),然后将文件的内容显示在多行文本框中。当选择save 时,弹出文件对话框,在其中输入文件名后将多行文本框的内容存入该文件。Util菜单又包括count和reverse。当选择count时,能统计文本框中的字符数,并弹出一个对话框显示统计结果。当选择reverse时,能将文本框中的每一行文字颠倒。
该文本编辑器也可设计成Java Application或Applet
哪位大虾知道一定要帮帮忙啊!! 展开
该文本编辑器也可设计成Java Application或Applet
哪位大虾知道一定要帮帮忙啊!! 展开
1个回答
展开全部
呵,刚好有个程序很似,有打开和保存,每二个功能也挺简单的,你自己写吧,我是个懒人.
package myclass;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import com.sun.tools.hat.internal.parser.Reader;
public class MyTxt extends JFrame implements ActionListener
{
JTextArea ta;
JMenuItem open,save,myexit,open1,save1;
JMenuItem copy,palse;
JMenuBar mb;
JPopupMenu popm;
JScrollPane sp;
public void createmenu()
{
mb=new JMenuBar();
JMenu m1=new JMenu("文件");
JMenu m2=new JMenu("编辑");
JMenu m3=new JMenu("字体");
mb.add(m1);mb.add(m2);m2.add(m3);
open=new JMenuItem("打开");
//ImageIcon ii = new ImageIcon("a.gif");
//open.setIcon(new ImageIcon("b.gif"));
open.addActionListener(this);
save=new JMenuItem("保存");
save.addActionListener(this);
myexit=new JMenuItem("退出");
myexit.addActionListener(this);
m1.add(open);m1.add(save);m1.add(myexit);
copy=new JMenuItem("复制");
palse=new JMenuItem("粘贴");
m2.add(copy);m2.add(palse);
popm=new JPopupMenu ();
open1=new JMenuItem("打开");
open1.addActionListener(this);
save1=new JMenuItem("保存");
save1.addActionListener(this);
popm.add(open1);popm.add(save1);
ta.add(popm);
ta.addMouseListener(new MyMouseEvent(this));
}
class MyMouseEvent extends MouseAdapter
{
MyTxt myparentFrame;
MyMouseEvent ( MyTxt m)
{
myparentFrame=m;
}
public void mouseReleased(MouseEvent e)
{
if(e.isPopupTrigger())
myparentFrame.popm.show((Component)e.getSource(),e.getX(),e.getY()); }
}
public MyTxt ()
{
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
dispose();
System.exit(0);
}
});
ta=new JTextArea(10,30);
add(ta,BorderLayout.CENTER);
Panel p1=new Panel();
add(p1,BorderLayout.SOUTH);
setVisible(true);
setSize(500,500);
//pack();
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();//获取屏幕大小
int w = getSize().width;
int h = getSize().height;
int x = (dim.width-w)/2;
int y = (dim.height-h)/2;
setLocation(x,y);
createmenu();
setJMenuBar(mb);
sp = new JScrollPane(ta);
getContentPane().add(sp);
}
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand()=="打开")
try
{
openfile();
}
catch(IOException ex){}
if(e.getActionCommand()=="保存")
{
try {
savefile();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
if(e.getActionCommand()=="退出")
{
dispose();
System.exit(0);
}
}
public void savefile() throws IOException
{
FileDialog fd=new FileDialog(this,"保存",FileDialog.SAVE);
fd.setVisible(true);
FileWriter fw=new FileWriter( fd.getDirectory()+fd.getFile());
for(int i=0;i<ta.getText().length();i++)
{
fw.write(ta.getText().charAt(i));
}
fw.close();
}
public void openfile() throws IOException
{
FileDialog fd=new FileDialog(this,"打开",FileDialog.LOAD);
fd.setVisible(true);
FileReader fr=new FileReader( fd.getDirectory()+fd.getFile());
int n=0;
int row=10;
while((n=fr.read())!=-1)
{
ta.append(""+(char)n);
row--;
if(row==0)
{
ta.append("\n");
row=20;
}
}
fr.close();
}
public static void main(String []args)
{
MyTxt tw=new MyTxt();
}
}
package myclass;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import com.sun.tools.hat.internal.parser.Reader;
public class MyTxt extends JFrame implements ActionListener
{
JTextArea ta;
JMenuItem open,save,myexit,open1,save1;
JMenuItem copy,palse;
JMenuBar mb;
JPopupMenu popm;
JScrollPane sp;
public void createmenu()
{
mb=new JMenuBar();
JMenu m1=new JMenu("文件");
JMenu m2=new JMenu("编辑");
JMenu m3=new JMenu("字体");
mb.add(m1);mb.add(m2);m2.add(m3);
open=new JMenuItem("打开");
//ImageIcon ii = new ImageIcon("a.gif");
//open.setIcon(new ImageIcon("b.gif"));
open.addActionListener(this);
save=new JMenuItem("保存");
save.addActionListener(this);
myexit=new JMenuItem("退出");
myexit.addActionListener(this);
m1.add(open);m1.add(save);m1.add(myexit);
copy=new JMenuItem("复制");
palse=new JMenuItem("粘贴");
m2.add(copy);m2.add(palse);
popm=new JPopupMenu ();
open1=new JMenuItem("打开");
open1.addActionListener(this);
save1=new JMenuItem("保存");
save1.addActionListener(this);
popm.add(open1);popm.add(save1);
ta.add(popm);
ta.addMouseListener(new MyMouseEvent(this));
}
class MyMouseEvent extends MouseAdapter
{
MyTxt myparentFrame;
MyMouseEvent ( MyTxt m)
{
myparentFrame=m;
}
public void mouseReleased(MouseEvent e)
{
if(e.isPopupTrigger())
myparentFrame.popm.show((Component)e.getSource(),e.getX(),e.getY()); }
}
public MyTxt ()
{
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
dispose();
System.exit(0);
}
});
ta=new JTextArea(10,30);
add(ta,BorderLayout.CENTER);
Panel p1=new Panel();
add(p1,BorderLayout.SOUTH);
setVisible(true);
setSize(500,500);
//pack();
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();//获取屏幕大小
int w = getSize().width;
int h = getSize().height;
int x = (dim.width-w)/2;
int y = (dim.height-h)/2;
setLocation(x,y);
createmenu();
setJMenuBar(mb);
sp = new JScrollPane(ta);
getContentPane().add(sp);
}
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand()=="打开")
try
{
openfile();
}
catch(IOException ex){}
if(e.getActionCommand()=="保存")
{
try {
savefile();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
if(e.getActionCommand()=="退出")
{
dispose();
System.exit(0);
}
}
public void savefile() throws IOException
{
FileDialog fd=new FileDialog(this,"保存",FileDialog.SAVE);
fd.setVisible(true);
FileWriter fw=new FileWriter( fd.getDirectory()+fd.getFile());
for(int i=0;i<ta.getText().length();i++)
{
fw.write(ta.getText().charAt(i));
}
fw.close();
}
public void openfile() throws IOException
{
FileDialog fd=new FileDialog(this,"打开",FileDialog.LOAD);
fd.setVisible(true);
FileReader fr=new FileReader( fd.getDirectory()+fd.getFile());
int n=0;
int row=10;
while((n=fr.read())!=-1)
{
ta.append(""+(char)n);
row--;
if(row==0)
{
ta.append("\n");
row=20;
}
}
fr.close();
}
public static void main(String []args)
{
MyTxt tw=new MyTxt();
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询