我的小型applet,你看好不 import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TextEditor extends JApplet
{
private JMenuItem jmiOpen=new JMenuItem("Open");
private JMenuItem jmiSave=new JMenuItem("Save");
private JMenuItem jmiClear=new JMenuItem("Clear");
private JMenuItem jmiExit=new JMenuItem("Exit");
private JMenuItem jmiForeground=new JMenuItem("Foreground");
private JMenuItem jmiBackground=new JMenuItem("Background");
private JLabel jlblStatus=new JLabel();
private JTextArea jta=new JTextArea();
private JFileChooser jFileChooser=new JFileChooser(new File("."));
public TextEditor()
{
JMenu jMenu1=new JMenu("File");
jMenu1.add(jmiOpen);
jMenu1.add(jmiSave);
jMenu1.add(jmiClear);
jMenu1.addSeparator();
jMenu1.add(jmiExit);
JMenu jMenu2=new JMenu("Edit");
jMenu2.add(jmiForeground);
jMenu2.add(jmiBackground);
JMenuBar jMenuBar1=new JMenuBar();
jMenuBar1.add(jMenu1);
jMenuBar1.add(jMenu2);
setJMenuBar(jMenuBar1);
jmiOpen.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent evet)
{
open();
}
}
);
jmiSave.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent evet)
{
save();
}
}
);
jmiClear.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent evet)
{
jta.setText(null);
}
}
);
jmiExit.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent evet)
{
System.exit(0);
}
}
);
jmiForeground.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent evet)
{
Color selectedColor=JColorChooser.showDialog(null,"Choose Foreground color",jta.getForeground());
if(selectedColor!=null)
jta.setForeground(selectedColor);
}
}
);
jmiBackground.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent evet)
{
Color selectedColor=JColorChooser.showDialog(null,"Choose Background color",jta.getBackground());
if(selectedColor!=null)
jta.setBackground(selectedColor);
}
}
);
getContentPane().add(jlblStatus,BorderLayout.SOUTH);
getContentPane().add(new JScrollPane(jta),BorderLayout.CENTER);
}
private void open()
{
if(jFileChooser.showOpenDialog(this)==JFileChooser.APPROVE_OPTION)
open(jFileChooser.getSelectedFile());
} private void open(File file)
{
try
{
BufferedInputStream in=new BufferedInputStream(new FileInputStream(file));
byte[]b=new byte[in.available()];
in.read(b,0,b.length);
jta.append(new String(b,0,b.length));
in.close();
jlblStatus.setText(file.getName()+"Opened");
}
catch(IOException ex)
{
jlblStatus.setText("Error opening"+file.getName());
}
}
private void save()
{
if(jFileChooser.showSaveDialog(this)==JFileChooser.APPROVE_OPTION)
save(jFileChooser.getSelectedFile());
}
private void save(File file)
{
try
{
BufferedOutputStream out=new BufferedOutputStream(new FileOutputStream(file));
byte[]b=jta.getText().getBytes();
out.write(b,0,b.length);
out.close();
}
catch(IOException ex)
{
jlblStatus.setText("Error saving"+file.getName());
}
}
}
我给你