Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.*;
- import java.util.Scanner;
- import java.io.*;
- public class Simplenotepad extends JFrame implements ActionListener {
- // private TextArea textArea = new TextArea("", 0, 0,
- // TextArea.SCROLLBARS_VERTICAL_ONLY);
- private JTextArea textArea = new JTextArea(10, 15);
- private JScrollPane scrollPane = new JScrollPane(textArea);
- private MenuBar menuBar = new MenuBar(); // first, create a MenuBar item
- private Font f = new Font("Verdana", Font.PLAIN, 10);
- private Menu file = new Menu();
- private MenuItem open = new MenuItem();
- private MenuItem save = new MenuItem();
- private MenuItem exit = new MenuItem();
- private Menu format = new Menu();
- private MenuItem wrap = new MenuItem();
- private MenuItem noWrap = new MenuItem();
- private Menu font = new Menu();
- private MenuItem s10 = new MenuItem();
- private MenuItem s12 = new MenuItem();
- private MenuItem s14 = new MenuItem();
- private MenuItem s16 = new MenuItem();
- private MenuItem s18 = new MenuItem();
- private MenuItem s20 = new MenuItem();
- private Menu edit = new Menu();
- private MenuItem clear = new MenuItem();
- private Menu help = new Menu();
- private MenuItem about = new MenuItem();
- private String saveTest = "";
- public Simplenotepad() {
- this.setSize(250, 250);
- this.textArea.setFont(f);
- this.setTitle("A Simple Notepad");
- this.textArea.setLineWrap(true);
- this.scrollPane.add(textArea);
- setDefaultCloseOperation(EXIT_ON_CLOSE);
- this.getContentPane().setLayout(new BorderLayout());
- this.getContentPane().add(textArea);
- this.getContentPane().add(scrollPane, BorderLayout.EAST);
- this.scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
- this.setMenuBar(this.menuBar);
- this.menuBar.add(this.file);
- this.menuBar.add(this.format);
- this.menuBar.add(this.edit);
- this.menuBar.add(this.help);
- this.file.setLabel("File");
- this.open.setLabel("Open");
- this.save.setLabel("Save");
- this.exit.setLabel("Exit");
- this.format.setLabel("Format");
- this.wrap.setLabel("Text-wrap");
- this.noWrap.setLabel("No text wrap");
- this.font.setLabel("Font size");
- this.s10.setLabel("10");
- this.s12.setLabel("12");
- this.s14.setLabel("14");
- this.s16.setLabel("16");
- this.s18.setLabel("18");
- this.s20.setLabel("20");
- this.edit.setLabel("Edit");
- this.clear.setLabel("Clear");
- this.help.setLabel("Help");
- this.about.setLabel("About");
- this.open.addActionListener(this);
- this.save.addActionListener(this);
- this.exit.addActionListener(this);
- this.wrap.addActionListener(this);
- this.noWrap.addActionListener(this);
- this.font.addActionListener(this);
- this.s10.addActionListener(this);
- this.s12.addActionListener(this);
- this.s14.addActionListener(this);
- this.s16.addActionListener(this);
- this.s18.addActionListener(this);
- this.s20.addActionListener(this);
- this.clear.addActionListener(this);
- this.about.addActionListener(this);
- this.file.add(this.open);
- this.file.add(this.save);
- this.file.add(this.exit);
- this.help.add(this.about);
- this.edit.add(this.clear);
- this.format.add(this.wrap);
- this.format.add(this.noWrap);
- this.format.add(this.font);
- this.font.add(this.s10);
- this.font.add(this.s12);
- this.font.add(this.s14);
- this.font.add(this.s16);
- this.font.add(this.s18);
- this.font.add(this.s20);
- }
- public void actionPerformed(ActionEvent e) {
- if (e.getSource() == this.about) {
- JOptionPane
- .showMessageDialog(null,
- "Denne applikasjonen er laget av MEG \n Dette er en enkel tekstbehandler");
- } else if (e.getSource() == this.open) {
- JFileChooser openFile = new JFileChooser();
- int filePath = openFile.showOpenDialog(this);
- if (filePath == JFileChooser.APPROVE_OPTION) {
- this.textArea.setText(""); // clears the current text when
- // opening a new file
- try {
- Scanner scan = new Scanner(new FileReader(openFile
- .getSelectedFile().getPath()));
- while (scan.hasNext())
- this.textArea.append(scan.nextLine() + "\n\r");
- scan.close();
- } catch (Exception ex) {
- System.out.println(ex.getMessage());
- }
- }
- } else if (e.getSource() == this.save) {
- JFileChooser saveFile = new JFileChooser();
- int filePath = saveFile.showSaveDialog(this);
- if (filePath == JFileChooser.APPROVE_OPTION) {
- try {
- BufferedWriter writer = new BufferedWriter(new FileWriter(
- saveFile.getSelectedFile().getPath()));
- writer.write(this.textArea.getText());
- writer.close();
- saveTest = this.textArea.getText();
- } catch (Exception ex) {
- System.out.println(ex.getMessage());
- }
- }
- } else if (e.getSource() == this.noWrap) {
- textArea.setLineWrap(false);
- } else if (e.getSource() == this.wrap) {
- textArea.setLineWrap(true);
- } else if (e.getSource() == this.s10) {
- this.f = new Font("Verdana", Font.PLAIN, 10);
- textArea.setFont(f);
- } else if (e.getSource() == this.s12) {
- this.f = new Font("Verdana", Font.PLAIN, 12);
- textArea.setFont(f);
- } else if (e.getSource() == this.s14) {
- this.f = new Font("Verdana", Font.PLAIN, 14);
- textArea.setFont(f);
- } else if (e.getSource() == this.s16) {
- this.f = new Font("Verdana", Font.PLAIN, 16);
- textArea.setFont(f);
- } else if (e.getSource() == this.s18) {
- this.f = new Font("Verdana", Font.PLAIN, 18);
- textArea.setFont(f);
- } else if (e.getSource() == this.s20) {
- this.f = new Font("Verdana", Font.PLAIN, 20);
- textArea.setFont(f);
- } else if (e.getSource() == this.exit) {
- if (saveTest == this.textArea.getText()) {
- this.dispose();
- } else {
- int choice = JOptionPane.showOptionDialog(null,
- "Vill du lagre før du lukker applikasjonen=", "Exit",
- JOptionPane.YES_NO_CANCEL_OPTION,
- JOptionPane.INFORMATION_MESSAGE, null, null, null);
- if (choice == 0) {
- // Yes was choosen
- JFileChooser saveFile = new JFileChooser();
- int filePath = saveFile.showSaveDialog(this);
- if (filePath == JFileChooser.APPROVE_OPTION) {
- try {
- BufferedWriter writer = new BufferedWriter(
- new FileWriter(saveFile.getSelectedFile()
- .getPath()));
- writer.write(this.textArea.getText());
- writer.close();
- saveTest = this.textArea.getText();
- } catch (Exception ex) {
- System.out.println(ex.getMessage());
- }
- }
- } else if (choice == 1) {
- // No was choosen
- this.dispose();
- }
- }
- } else if (e.getSource() == this.clear) {
- int choice = JOptionPane.showOptionDialog(null,
- "Vill du lagre før du lukker applikasjonen=", "Exit",
- JOptionPane.YES_NO_CANCEL_OPTION,
- JOptionPane.INFORMATION_MESSAGE, null, null, null);
- if (choice == 0) {
- JFileChooser saveFile = new JFileChooser();
- int filePath = saveFile.showSaveDialog(this);
- if (filePath == JFileChooser.APPROVE_OPTION) {
- try {
- BufferedWriter writer = new BufferedWriter(
- new FileWriter(saveFile.getSelectedFile()
- .getPath()));
- writer.write(this.textArea.getText());
- writer.close();
- saveTest = this.textArea.getText();
- } catch (Exception ex) {
- System.out.println(ex.getMessage());
- }
- }
- } else if (choice == 1) {
- this.textArea.setText("");
- }
- }
- }
- public static void main(String args[]) {
- Simplenotepad app = new Simplenotepad();
- app.setVisible(true);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment