Divyansh_Chourey

NotepadV1 Project

Sep 1st, 2024 (edited)
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.76 KB | Source Code | 0 0
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import java.nio.file.Files;
  4. import java.nio.file.Path;
  5.  
  6. class NotepadV1Main{
  7.     Frame mainFrame;
  8.     int winWidth = 1000;
  9.     int winHeight = 700;
  10.     TextArea tArea;
  11.     Font font, menuFont;
  12.  
  13.     NotepadV1Main(){
  14.         menuFont = new Font("Arial", 0, 14);
  15.         font = new Font("Arial", 0, 16);
  16.  
  17.         mainFrame = new Frame("Notepad");
  18.         mainFrame.setSize(winWidth, winHeight);
  19.         mainFrame.setVisible(true);
  20.         mainFrame.setLayout(new BorderLayout());
  21.         mainFrame.addWindowListener(new WindowListener() {
  22.             @Override
  23.             public void windowOpened(WindowEvent e) {
  24.                 System.out.println("Main window opened");
  25.             }
  26.             @Override
  27.             public void windowClosing(WindowEvent e) {
  28.                 System.out.println("Main window closing");
  29.                 mainFrame.dispose();
  30.             }
  31.             @Override
  32.             public void windowClosed(WindowEvent e) {
  33.                 System.out.println("Main window closed");
  34.             }
  35.             @Override
  36.             public void windowIconified(WindowEvent e) {
  37.                 System.out.println("Main window iconified");
  38.             }
  39.             @Override
  40.             public void windowDeiconified(WindowEvent e) {
  41.                 System.out.println("Main window deiconified");
  42.             }
  43.             @Override
  44.             public void windowActivated(WindowEvent e) {
  45.                 System.out.println("Main window activated");
  46.             }
  47.             @Override
  48.             public void windowDeactivated(WindowEvent e) {
  49.                 System.out.println("Main window deactivated");
  50.             }
  51.         });
  52.         addTextArea();
  53.         addMenuArea();
  54.     }
  55.  
  56.     void addTextArea(){
  57.         tArea = new TextArea("text here");
  58.         tArea.setFont(font);
  59.         mainFrame.add(tArea);
  60.     }
  61.  
  62.     void addMenuArea(){
  63.         MenuBar mb = new MenuBar();
  64.         mb.setFont(menuFont);
  65.         Menu menu1 = new Menu("File");
  66.         Menu menu2 = new Menu("View");
  67.         Menu menu3 = new Menu("Help");
  68.  
  69.         mb.add(menu1);
  70.         mb.add(menu2);
  71.         mb.add(menu3);
  72.  
  73.         menu1.addActionListener(new ActionListener() {
  74.             @Override
  75.             public void actionPerformed(ActionEvent e) {
  76.                 System.out.println(e.getActionCommand()+" Clicked");
  77.                 if(e.getActionCommand().equals("New Window")){
  78.                     new NotepadV1Main();
  79.                 } else if(e.getActionCommand().equals("New")){
  80.                     tArea.setText(" ");
  81.                 } else if(e.getActionCommand().equals("Exit")){
  82.                     mainFrame.dispose();
  83.                 }
  84.             }
  85.         });
  86.         menu2.addActionListener(new ActionListener() {
  87.             @Override
  88.             public void actionPerformed(ActionEvent e) {
  89.                 System.out.println(e.getActionCommand()+" Clicked");
  90.                 if(e.getActionCommand().equals("Font")){
  91.                     fontAction();
  92.                 }
  93.             }
  94.         });
  95.         menu3.addActionListener(new ActionListener() {
  96.             @Override
  97.             public void actionPerformed(ActionEvent e) {
  98.                 System.out.println(e.getActionCommand()+" Clicked");
  99.             }
  100.         });
  101.  
  102.         MenuItem newWindowItem = new MenuItem("New Window");
  103.         menu1.add(newWindowItem);
  104.         MenuItem newItem = new MenuItem("New");
  105.         menu1.add(newItem);
  106.  
  107.         Menu subMenu1 = new Menu("Save");
  108.         subMenu1.addActionListener(new ActionListener() {
  109.             @Override
  110.             public void actionPerformed(ActionEvent e) {
  111.                 if(e.getActionCommand().equals("To same folder")){
  112.                     String name = "file.txt";
  113.                     Path filepath = Path.of(name);
  114.                     try{
  115.                         Files.writeString(filepath, tArea.getText());
  116.                         System.out.println("File saved successfully");
  117.                     } catch (Exception excp){
  118.                         System.out.println(excp);
  119.                     }
  120.                 } else if(e.getActionCommand().equals("To specific folder")){
  121.                     saveSpecificMethod();
  122.                 }
  123.             }
  124.         });
  125.         menu1.add(subMenu1);
  126.         MenuItem saveItem = new MenuItem("To same folder");
  127.         subMenu1.add(saveItem);
  128.         MenuItem saveSpecificItem = new MenuItem("To specific folder");
  129.         subMenu1.add(saveSpecificItem);
  130.  
  131. //        MenuItem openItem = new MenuItem("Open");
  132. //        menu1.add(openItem);
  133.         menu1.addSeparator();
  134.         MenuItem exitItem = new MenuItem("Exit");
  135.         menu1.add(exitItem);
  136.  
  137. //        MenuItem colorItem = new MenuItem("Color");
  138. //        menu2.add(colorItem);
  139.         MenuItem fontItem = new MenuItem("Font");
  140.         menu2.add(fontItem);
  141.  
  142.         MenuItem helpItem = new MenuItem("View help");
  143.         menu3.add(helpItem);
  144.         MenuItem aboutItem = new MenuItem("About author");
  145.         menu3.add(aboutItem);
  146.  
  147.         mainFrame.setMenuBar(mb);
  148.     }
  149.  
  150.     void saveSpecificMethod(){
  151.  
  152.         Frame saveFrame = new Frame("Save File");
  153.         saveFrame.setSize(450, 180);
  154.         saveFrame.setVisible(true);
  155.         saveFrame.setLayout(null);
  156.  
  157.         saveFrame.addWindowListener(new WindowListener() {
  158.             @Override
  159.             public void windowOpened(WindowEvent e) {
  160.                 System.out.println("Save window opened");
  161.             }
  162.             @Override
  163.             public void windowClosing(WindowEvent e) {
  164.                 System.out.println("Save window closing");
  165.                 saveFrame.dispose();
  166.             }
  167.             @Override
  168.             public void windowClosed(WindowEvent e) {
  169.                 System.out.println("Save window closed");
  170.             }
  171.             @Override
  172.             public void windowIconified(WindowEvent e) {
  173.                 System.out.println("Save window iconified");
  174.             }
  175.             @Override
  176.             public void windowDeiconified(WindowEvent e) {
  177.                 System.out.println("Save window deiconified");
  178.             }
  179.             @Override
  180.             public void windowActivated(WindowEvent e) {
  181.                 System.out.println("Save window activated");
  182.             }
  183.             @Override
  184.             public void windowDeactivated(WindowEvent e) {
  185.                 System.out.println("Save window deactivated");
  186.             }
  187.         });
  188.  
  189.         Label l = new Label("Enter file location here: ");
  190.         l.setBounds(20, 50, 180, 30);
  191.         l.setFont(menuFont);
  192.         saveFrame.add(l);
  193.  
  194.         TextField location = new TextField();
  195.         location.setBounds(200, 50, 200, 25);
  196.         location.setFont(menuFont);
  197.         saveFrame.add(location);
  198.  
  199.         Button saveBtn = new Button("Save");
  200.         saveBtn.setBounds(200, 100, 50, 30);
  201.         saveBtn.setFont(menuFont);
  202.         saveFrame.add(saveBtn);
  203.         saveBtn.addActionListener(new ActionListener() {
  204.             @Override
  205.             public void actionPerformed(ActionEvent e) {
  206.                 String name = location.getText();
  207.                 Path filepath = Path.of(name);
  208.                 try{
  209.                     Files.writeString(filepath, tArea.getText());
  210.                     System.out.println("File saved successfully");
  211.                 } catch (Exception excp){
  212.                     System.out.println(excp);
  213.                 }
  214.                 saveFrame.dispose();
  215.             }
  216.         });
  217.     }
  218.  
  219.     void fontAction(){
  220. //        tArea.setFont(new Font("Dialog", 0, 16));
  221.         Frame fontFrame = new Frame("Font");
  222.         fontFrame.setSize(400, 200);
  223.         fontFrame.setVisible(true);
  224.         fontFrame.setLayout(null);
  225.         fontFrame.addWindowListener(new WindowListener() {
  226.             @Override
  227.             public void windowOpened(WindowEvent e) {
  228.                 System.out.println("Font frame opened");
  229.             }
  230.             @Override
  231.             public void windowClosing(WindowEvent e) {
  232.                 System.out.println("Font frame closing");
  233.                 fontFrame.dispose();
  234.             }
  235.             @Override
  236.             public void windowClosed(WindowEvent e) {
  237.                 System.out.println("Font frame closed");
  238.             }
  239.             @Override
  240.             public void windowIconified(WindowEvent e) {
  241.                 System.out.println("Font frame iconified");
  242.             }
  243.             @Override
  244.             public void windowDeiconified(WindowEvent e) {
  245.                 System.out.println("Font frame deiconified");
  246.             }
  247.             @Override
  248.             public void windowActivated(WindowEvent e) {
  249.                 System.out.println("Font frame activated");
  250.             }
  251.             @Override
  252.             public void windowDeactivated(WindowEvent e) {
  253.                 System.out.println("Font frame deactivated");
  254.             }
  255.         });
  256.         //Serif, SansSerif, Monospaced, Dialog, and DialogInput
  257.         String seleced = "";
  258.         Choice fontChoice = new Choice();
  259.         fontChoice.setBounds(150, 50, 100, 25);
  260.         fontChoice.setFont(menuFont);
  261.         fontChoice.add("Serif");
  262.         fontChoice.add("SansSerif");
  263.         fontChoice.add("Monospaced");
  264.         fontChoice.add("Dialog");
  265.         fontChoice.add("DialogInput");
  266.         fontFrame.add(fontChoice);
  267.  
  268.         fontChoice.addItemListener(new ItemListener() {
  269.             @Override
  270.             public void itemStateChanged(ItemEvent e) {
  271.                 System.out.println(e.getItem());
  272.                 tArea.setFont((Font) e.getItem());
  273.             }
  274.         });
  275.  
  276.         Label l1 = new Label("Select Font: ");
  277.         l1.setBounds(50, 50, 100, 25);
  278.         l1.setFont(menuFont);
  279.         fontFrame.add(l1);
  280.  
  281.         Label l2 = new Label("Enter Size: ");
  282.         l2.setBounds(50, 90, 100, 25);
  283.         l2.setFont(menuFont);
  284.         fontFrame.add(l2);
  285.  
  286.         TextField sizeField = new TextField();
  287.         sizeField.setBounds(150, 90, 50, 23);
  288.         sizeField.setFont(menuFont);
  289.         fontFrame.add(sizeField);
  290.  
  291.         Button fontSubmit = new Button("Submit");
  292.         fontSubmit.setBounds(50, 130, 60, 30);
  293.         fontSubmit.setFont(menuFont);
  294.         fontSubmit.addActionListener(new ActionListener() {
  295.             @Override
  296.             public void actionPerformed(ActionEvent e) {
  297. //                tArea.setFont(seleced, 1, sizeField.getText());
  298.             }
  299.         });
  300.         fontFrame.add(fontSubmit);
  301.  
  302.         Button fontCancel = new Button("Cancel");
  303.         fontCancel.setBounds(200, 130, 60, 30);
  304.         fontCancel.setFont(menuFont);
  305.         fontFrame.add(fontCancel);
  306.  
  307.  
  308.     }
  309. }
  310.  
  311. public class NotepadV1 {
  312.     public static void main(String[] args) {
  313.         new NotepadV1Main();
  314.     }
  315. }
  316.  
Advertisement
Add Comment
Please, Sign In to add comment