Assignment This lab is designed to have students become fami
Solution
Solution:
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.logging.*;
import javax.swing.*;
public class NotPad extends JPanel implements ActionListener
{
protected JTextArea textArea;
JFileChooser fileChoose;
public NotPad()
{
super(new BorderLayout());
fileChoose = new JFileChooser();
textArea = new JTextArea(5, 25);
textArea.setEditable(true);
JScrollPane scrollPane = new JScrollPane(textArea);
setPreferredSize(new Dimension(300, 500));
add(scrollPane, BorderLayout.CENTER);
JPanel panel = new JPanel();
add(panel, BorderLayout.SOUTH);
JButton SaveButton = new JButton(\"Save\");
JButton ResetButton = new JButton(\"Reset\");
SaveButton.setActionCommand(\"save\");
ResetButton.setActionCommand(\"reset\");
SaveButton.addActionListener(this);
ResetButton.addActionListener(this);
QuitButton.addActionListener(this);
panel.add(SaveButton);
panel.add(ResetButton);
}
private static void GUI()
{
JFrame frame = new JFrame(\"Sample Color Word\");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new NotPad());
frame.setSize(350, 250);
         frame.pack();
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if (\"save\".equals(e.getActionCommand())) {
if (textArea.getText().equals(\"\")) {
JOptionPane.showMessageDialog(this,\"Please Enter Some Text\",\"No Text Error!\", JOptionPane.ERROR_MESSAGE);
} else {
int returnVal = fileChoose.showSaveDialog(NotPad.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
FileWriter fileWriter = null;
try {
String string = textArea.getText();
StringReader stringReader = new StringReader(string);
BufferedReader bufferedReader = new BufferedReader(stringReader);
File file = fileChoose.getSelectedFile();
System.out.println(file.getName());
fileWriter = new FileWriter(file);
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
for (String line = bufferedReader.readLine();
line != null;
line = bufferedReader.readLine())
{
bufferedWriter.write(line);
bufferedWriter.newLine();
}
bufferedReader.close();
bufferedWriter.close();
} catch (IOException ex) {
Logger.getLogger(NotPad.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
fileWriter.close();
} catch (IOException ex) {
Logger.getLogger(NotPad.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
} else if (\"reset\".equals(e.getActionCommand())) {
textArea.setText(\"\");
} else {
System.exit(0);
}
}
//Main Function
public static void main(String[] args) {
//Set Look and Feel to Nimbus
try
{
UIManager.setLookAndFeel(\"com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel\");
}
catch(Exception e){
}
//creating and showing this application\'s GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
GUI();
}
});
}
}




