09-01-2016, 04:33 PM
import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
class FontDlg extends JDialog implements ActionListener,ListSelectionListener,ChangeListener,ItemListene
public class WordPad extends JFrame {
private CenterPanel centerPanel;
private EastPanel eastPanel;
private JTextArea input;
private SpellCheckerDialog spellCheckDialog;
private JFrame SpellCheckerFrame;
private Frame frame;
private WordPad me = this;
public WordPad() {
this.setTitle("WordPad");
this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
this.setSize(700, 480);
centerPanel = new CenterPanel();
eastPanel = new EastPanel();
this.setLayout(new BorderLayout(3, 3));
this.add(eastPanel, BorderLayout.EAST);
this.add(centerPanel, BorderLayout.CENTER);
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
int option;
option = JOptionPane.showConfirmDialog(centerPanel, "Do You Wish to Save your WordDocumet?", "Save?", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
if (option == 0) {
me.saveFile();
System.exit(0);
} else if (option == 1) {
System.exit(0);
} else if (option == 2);
}
});
this.setVisible(true);
}
private class CenterPanel extends JPanel {
JScrollPane myScrollPane;
public CenterPanel() {
input = new JTextArea(30, 40);
myScrollPane = new JScrollPane(input);
input.requestFocus();
super.add(myScrollPane);
}
}
private class EastPanel extends JPanel implements ActionListener {
private JPanel topPanel;
private JButton clearBtn;
private JButton spellCheckButton;
private JButton saveBtn;
EastPanel() {
topPanel = new JPanel();
topPanel.setLayout(new GridLayout(2, 2, 10, 10));
clearBtn = new JButton("Clear");
spellCheckButton = new JButton("Spell Check");
saveBtn = new JButton("Save");
topPanel.add(clearBtn);
topPanel.add(spellCheckButton);
topPanel.add(saveBtn);
this.add(topPanel);
clearBtn.addActionListener(this);
saveBtn.addActionListener(this);
spellCheckButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String newText = spellCheckDialog.showDialog(input.getText());
spellCheckDialog.dispose();
if (newText != null) {
input.setText(newText);
JOptionPane.showMessageDialog(centerPanel, "Spell Check completed sucesfully");
} else;
}
});
frame = JOptionPane.getFrameForComponent(SpellCheckerFrame);
spellCheckDialog = new SpellCheckerDialog(frame);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == clearBtn) {
clear();
} else if (e.getSource() == saveBtn) {
save();
}
}
private void clear() {
input.setText("");
input.requestFocus();
}
private void save() {
me.saveFile();
}
}
private void saveFile() {
JFileChooser fileChooser = new JFileChooser();
File myFile;
int i = fileChooser.showSaveDialog(input);
if (i == JFileChooser.APPROVE_OPTION) {
myFile = fileChooser.getSelectedFile();
PrintWriter writter = null;
try {
writter = new PrintWriter(myFile);
writter.println(input.getText());
} catch (FileNotFoundException ex) {
Logger.getLogger(SpellChecker.class.getName()).log(Level.SEVERE, null, ex);
} finally {
writter.close();
}
}
}
}