import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*; 
class MenuTest extends JFrame implements ActionListener{
	JMenuBar mb;
	JMenu file,send,config;
	JMenuItem op,ne,ex,sf,se,fc,bc;
	JTextArea t;	
	MenuTest(){
		super("ҵĵһ˵");
		this.setDefaultCloseOperation(3);
		menuInit();
		this.setJMenuBar(mb);
		this.getContentPane().add(new JScrollPane(t));
		this.setBounds(300,200,600,400);
		this.setVisible(true);		
	}
	void menuInit(){
		mb=new JMenuBar();//˵
		file=new JMenu("ļ");
		send=new JMenu("");		op=new JMenuItem("");	
	ne=new JMenuItem("½");		ex=new JMenuItem("˳");	
	se=new JMenuItem("ʼ");			sf=new JMenuItem("");
		mb.add(file);//file˵˵
		file.add(op);//Ѹ˵ŵfiel˵
		file.add(ne);  		file.add(send);		file.addSeparator();
		file.add(ex);  		send.add(se);		send.add(sf);				
		config=new JMenu("");
		fc=new JMenuItem("ǰɫ");		bc=new JMenuItem("ɫ");
		mb.add(config);		config.add(fc);		config.add(bc);
		//˵ע¼
		op.addActionListener(this);		ne.addActionListener(this);
		sf.addActionListener(this);		ex.addActionListener(this);
		fc.addActionListener(this);		bc.addActionListener(this);
		//ı
		t=new JTextArea();		t.setLineWrap(true);
	}
	//Ӧ˵¼
	public void actionPerformed(ActionEvent e){
		Object o=e.getSource();
		JFileChooser f=new JFileChooser();
		JColorChooser cc=new JColorChooser();
		if(o==ne){
			t.setText("");		}
		else if(o==op){
			f.showOpenDialog(this);//ļѡԻѡһļ
			try{
		StringBuffer s=new StringBuffer();//ָļжȡһлĶ
				FileReader in=new FileReader(f.getSelectedFile());
				while(true){           
					int b=in.read();  
					if(b==-1)break;  
					s.append((char)b); 
				}
				t.setText(s.toString());	in.close();
			}
			catch(Exception ee){}	
		}
		else if(o==sf){    //ѡС桱ñԻд
			f.showSaveDialog(this);
			try{
				FileWriter out=new FileWriter(f.getSelectedFile());
				out.write(t.getText());	out.close();
			}catch(Exception ee){}
		}
		else if(o==ex)System.exit(0);  //ѡС˳ʱӦ
		else if(o==bc){
			Color c=cc.showDialog(this,"Please select",Color.white);
			t.setBackground(c);   //ѡɫñɫ
		}
		else{
			Color c=cc.showDialog(this,"Please select",Color.black);
			t.setForeground(c);   //ѡɫǰɫ
		}
	}	
	public static void main(String[] aa){
		new MenuTest();	}
}
