再看了论坛里一个兄弟的程序后,自己试着弄了几段下来,自己也弄了弄,啥都不懂
import javax.swing.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.RandomAccessFile;
public class TextEdit {
public static void main(String args[]){
new Windows();//调用Windows构造函数
}
}
class Windows extends JFrame{
JTextArea textarea= new JTextArea();
MenuBar myBar= new MenuBar(textarea);
public Windows(){
super("");
setLayout(new BorderLayout());
add("Center", textarea);
add(myBar);
setSize(500,500);
setVisible(true);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
});
}
//set up GUIs
class MenuBar extends JMenuBar{
JMenu fileMenu,editMenu,formatMenu,aboutMenu;
JMenuItem newitem,openitem,saveitem,exititem;
JMenuItem cutitem,copyitem,pasteitem,selectallitem;
JMenuItem bgitem,fontitem;
JTextArea textarea;
String placeHolder=" ";
Vector recentCuts=new Vector();
Handler handler= new Handler();
public MenuBar(JTextArea textarea){
this.textarea=textarea;
//set up file menu and its menu items
JMenu fileMenu= new JMenu("文件");
fileMenu.setMnemonic('F');
//set menu items
JMenuItem newitem= new JMenuItem("新建");
newitem.setMnemonic('N');
newitem.addActionListener(handler);
fileMenu.add(newitem);
JMenuItem openitem= new JMenuItem("打开..");
openitem.setMnemonic('O');
openitem.addActionListener(handler);
fileMenu.add(openitem);
JMenuItem saveitem= new JMenuItem("保存..");
saveitem.setMnemonic('S');
saveitem.addActionListener(handler);
fileMenu.add(saveitem);
JMenuItem exititem= new JMenuItem("退出");
exititem.setMnemonic('X');
exititem.addActionListener(handler);
fileMenu.add(exititem);
//set up exit Menu and its Menu items
JMenu editMenu= new JMenu("编辑");
editMenu.setMnemonic('E');
JMenuItem cutitem= new JMenuItem("剪切");
cutitem.setMnemonic('X');
cutitem.addActionListener(handler);
editMenu.add(cutitem);
JMenuItem copyitem= new JMenuItem("复制");
copyitem.setMnemonic('C');
copyitem.addActionListener(handler);
editMenu.add(copyitem);
JMenuItem pasteitem= new JMenuItem("粘贴");
pasteitem.setMnemonic('V');
pasteitem.addActionListener(handler);
editMenu.add(pasteitem);
JMenuItem selectallitem= new JMenuItem("全选");
selectallitem.setMnemonic('A');
selectallitem.addActionListener(handler);
editMenu.add(selectallitem);
//set up format menu and its items
JMenu formatMenu= new JMenu("格式");
formatMenu.setMnemonic('O');
JMenuItem bgitem= new JMenuItem("背景色");
bgitem.setMnemonic('B');
bgitem.addActionListener(handler);
formatMenu.add(bgitem);
JMenuItem fontitem= new JMenuItem("字体");
fontitem.setMnemonic('F');
fontitem.addActionListener(handler);
formatMenu.add(fontitem);
//set up about menu and its items
JMenu aboutMenu= new JMenu("关于");
aboutMenu.setMnemonic('A');
aboutMenu.addActionListener(handler);
JMenuBar bar= new JMenuBar();
setJMenuBar(bar);
bar.add(fileMenu);
bar.add(editMenu);
bar.add(formatMenu);
bar.add(aboutMenu);
}
class Handler implements ActionListener{
public void actionPerformed(ActionEvent e){
if(!(e.getSource() instanceof MenuItem))
return;
JMenuItem m=(JMenuItem)e.getSource();
if(m==exititem)
{
System.exit(0);
}
else if(m==cutitem)
{
placeHolder=textarea.getSelectedText();
textarea.replaceRange("",
textarea.getSelectionStart(),
textarea.getSelectionEnd());
addRecentCut(placeHolder);
}
else if (m==copyitem)
{
placeHolder=textarea.getSelectedText();
}
else if (m==pasteitem)
{
textarea.insert(placeHolder,textarea.getCaretPosition());
}
else if (m==selectallitem)
{
textarea.selectAll();
}
else if (m==openitem)
{
Frame f=new Frame("FileDialog");
FileDialog d;
d=new FileDialog(f,"FileDialog");
d.setVisible(true);
String filename=d.getFile();
File openf=new File(filename);
String s="";
try
{
RandomAccessFile fr=new RandomAccessFile(d.getDirectory()+filename,"r");
byte[] buffer = new byte[4096];
int len;
while((len=fr.read(buffer))!=-1)
{
s = new String(buffer, 0, len);
textarea.append(s+"\n");
}
fr.close();
}catch(Exception er){}
}
else if(m==saveitem)
{
Frame f=new Frame("FileDialog");
FileDialog d;
d=new FileDialog(f,"FileDialog");
d.setVisible(true);
String filename=d.getFile();
// File openf=new File(filename);
try
{
RandomAccessFile fw=new RandomAccessFile(d.getDirectory()+filename,"rw");
String s=textarea.getText();
fw.writeChars(s);
fw.close();
}catch(Exception er){}
}
}
private void addRecentCut(String cut)
{
recentCuts.insertElementAt(cut,0);
}
}
}
}
[此贴子已经被作者于2006-12-31 1:13:26编辑过]
----------------解决方案--------------------------------------------------------
初学就不要做这些,学好基础在来做!
----------------解决方案--------------------------------------------------------
哪不会弄?..
----------------解决方案--------------------------------------------------------
希望初学者能从基础学起,不要好高骛远
----------------解决方案--------------------------------------------------------
收到.
----------------解决方案--------------------------------------------------------
没有说你,你不能算是初学者,你的水平已经不错了
----------------解决方案--------------------------------------------------------
...我是初学者..不过..我在努力中..
----------------解决方案--------------------------------------------------------
这是我们老师布置的作业 我还没看书呢 都不知道怎么弄 现在还
----------------解决方案--------------------------------------------------------
雨水MM加油
----------------解决方案--------------------------------------------------------
你跟他同一学校?..
----------------解决方案--------------------------------------------------------