import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
class FourButtons extends JPanel implements ActionListener{
JTextArea t;
JButton open,clear,save,color;
FourButtons(){
super(new GridLayout(4,1,5,5));
this.t=t;
buttonInit();
add(open);add(save);add(clear);add(color);
}
void buttonInit(){
open=new JButton("Open");
clear=new JButton("Clear");
save=new JButton("Save");
color=new JButton("Color");
open.addActionListener(this);
clear.addActionListener(this);
save.addActionListener(this);
color.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
JButton ob=(JButton)e.getSource();
if(ob==clear) t.setText("");
else if(ob==color) changeColor();
else if(ob==open) open();
else save();
}
void changeColor(){
Color c=JColorChooser.showDialog(this,"Please select",Color.black);
t.setForeground(c);
}
void open(){
JFileChooser fc=new JFileChooser();
fc.showOpenDialog(this);
File file=fc.getSelectedFile();
try{
FileInputStream in=new FileInputStream(file);
byte[] b=new byte[in.available()];
in.read(b);
t.setText(new String(b));
in.close();
}
catch(Exception e){}
}
void save(){
JFileChooser fc=new JFileChooser();
fc.showSaveDialog(this);
File file=fc.getSelectedFile();
try{
FileWriter out=new FileWriter (file);
out.write(t.getText());
out.close();
}
catch(Exception e){}
}
}