Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import java.awt.event.*;
- import java.nio.file.Files;
- import java.nio.file.Path;
- class NotepadV1Main{
- Frame mainFrame;
- int winWidth = 1000;
- int winHeight = 700;
- TextArea tArea;
- Font font, menuFont;
- NotepadV1Main(){
- menuFont = new Font("Arial", 0, 14);
- font = new Font("Arial", 0, 16);
- mainFrame = new Frame("Notepad");
- mainFrame.setSize(winWidth, winHeight);
- mainFrame.setVisible(true);
- mainFrame.setLayout(new BorderLayout());
- mainFrame.addWindowListener(new WindowListener() {
- @Override
- public void windowOpened(WindowEvent e) {
- System.out.println("Main window opened");
- }
- @Override
- public void windowClosing(WindowEvent e) {
- System.out.println("Main window closing");
- mainFrame.dispose();
- }
- @Override
- public void windowClosed(WindowEvent e) {
- System.out.println("Main window closed");
- }
- @Override
- public void windowIconified(WindowEvent e) {
- System.out.println("Main window iconified");
- }
- @Override
- public void windowDeiconified(WindowEvent e) {
- System.out.println("Main window deiconified");
- }
- @Override
- public void windowActivated(WindowEvent e) {
- System.out.println("Main window activated");
- }
- @Override
- public void windowDeactivated(WindowEvent e) {
- System.out.println("Main window deactivated");
- }
- });
- addTextArea();
- addMenuArea();
- }
- void addTextArea(){
- tArea = new TextArea("text here");
- tArea.setFont(font);
- mainFrame.add(tArea);
- }
- void addMenuArea(){
- MenuBar mb = new MenuBar();
- mb.setFont(menuFont);
- Menu menu1 = new Menu("File");
- Menu menu2 = new Menu("View");
- Menu menu3 = new Menu("Help");
- mb.add(menu1);
- mb.add(menu2);
- mb.add(menu3);
- menu1.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- System.out.println(e.getActionCommand()+" Clicked");
- if(e.getActionCommand().equals("New Window")){
- new NotepadV1Main();
- } else if(e.getActionCommand().equals("New")){
- tArea.setText(" ");
- } else if(e.getActionCommand().equals("Exit")){
- mainFrame.dispose();
- }
- }
- });
- menu2.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- System.out.println(e.getActionCommand()+" Clicked");
- if(e.getActionCommand().equals("Font")){
- fontAction();
- }
- }
- });
- menu3.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- System.out.println(e.getActionCommand()+" Clicked");
- }
- });
- MenuItem newWindowItem = new MenuItem("New Window");
- menu1.add(newWindowItem);
- MenuItem newItem = new MenuItem("New");
- menu1.add(newItem);
- Menu subMenu1 = new Menu("Save");
- subMenu1.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- if(e.getActionCommand().equals("To same folder")){
- String name = "file.txt";
- Path filepath = Path.of(name);
- try{
- Files.writeString(filepath, tArea.getText());
- System.out.println("File saved successfully");
- } catch (Exception excp){
- System.out.println(excp);
- }
- } else if(e.getActionCommand().equals("To specific folder")){
- saveSpecificMethod();
- }
- }
- });
- menu1.add(subMenu1);
- MenuItem saveItem = new MenuItem("To same folder");
- subMenu1.add(saveItem);
- MenuItem saveSpecificItem = new MenuItem("To specific folder");
- subMenu1.add(saveSpecificItem);
- // MenuItem openItem = new MenuItem("Open");
- // menu1.add(openItem);
- menu1.addSeparator();
- MenuItem exitItem = new MenuItem("Exit");
- menu1.add(exitItem);
- // MenuItem colorItem = new MenuItem("Color");
- // menu2.add(colorItem);
- MenuItem fontItem = new MenuItem("Font");
- menu2.add(fontItem);
- MenuItem helpItem = new MenuItem("View help");
- menu3.add(helpItem);
- MenuItem aboutItem = new MenuItem("About author");
- menu3.add(aboutItem);
- mainFrame.setMenuBar(mb);
- }
- void saveSpecificMethod(){
- Frame saveFrame = new Frame("Save File");
- saveFrame.setSize(450, 180);
- saveFrame.setVisible(true);
- saveFrame.setLayout(null);
- saveFrame.addWindowListener(new WindowListener() {
- @Override
- public void windowOpened(WindowEvent e) {
- System.out.println("Save window opened");
- }
- @Override
- public void windowClosing(WindowEvent e) {
- System.out.println("Save window closing");
- saveFrame.dispose();
- }
- @Override
- public void windowClosed(WindowEvent e) {
- System.out.println("Save window closed");
- }
- @Override
- public void windowIconified(WindowEvent e) {
- System.out.println("Save window iconified");
- }
- @Override
- public void windowDeiconified(WindowEvent e) {
- System.out.println("Save window deiconified");
- }
- @Override
- public void windowActivated(WindowEvent e) {
- System.out.println("Save window activated");
- }
- @Override
- public void windowDeactivated(WindowEvent e) {
- System.out.println("Save window deactivated");
- }
- });
- Label l = new Label("Enter file location here: ");
- l.setBounds(20, 50, 180, 30);
- l.setFont(menuFont);
- saveFrame.add(l);
- TextField location = new TextField();
- location.setBounds(200, 50, 200, 25);
- location.setFont(menuFont);
- saveFrame.add(location);
- Button saveBtn = new Button("Save");
- saveBtn.setBounds(200, 100, 50, 30);
- saveBtn.setFont(menuFont);
- saveFrame.add(saveBtn);
- saveBtn.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- String name = location.getText();
- Path filepath = Path.of(name);
- try{
- Files.writeString(filepath, tArea.getText());
- System.out.println("File saved successfully");
- } catch (Exception excp){
- System.out.println(excp);
- }
- saveFrame.dispose();
- }
- });
- }
- void fontAction(){
- // tArea.setFont(new Font("Dialog", 0, 16));
- Frame fontFrame = new Frame("Font");
- fontFrame.setSize(400, 200);
- fontFrame.setVisible(true);
- fontFrame.setLayout(null);
- fontFrame.addWindowListener(new WindowListener() {
- @Override
- public void windowOpened(WindowEvent e) {
- System.out.println("Font frame opened");
- }
- @Override
- public void windowClosing(WindowEvent e) {
- System.out.println("Font frame closing");
- fontFrame.dispose();
- }
- @Override
- public void windowClosed(WindowEvent e) {
- System.out.println("Font frame closed");
- }
- @Override
- public void windowIconified(WindowEvent e) {
- System.out.println("Font frame iconified");
- }
- @Override
- public void windowDeiconified(WindowEvent e) {
- System.out.println("Font frame deiconified");
- }
- @Override
- public void windowActivated(WindowEvent e) {
- System.out.println("Font frame activated");
- }
- @Override
- public void windowDeactivated(WindowEvent e) {
- System.out.println("Font frame deactivated");
- }
- });
- //Serif, SansSerif, Monospaced, Dialog, and DialogInput
- String seleced = "";
- Choice fontChoice = new Choice();
- fontChoice.setBounds(150, 50, 100, 25);
- fontChoice.setFont(menuFont);
- fontChoice.add("Serif");
- fontChoice.add("SansSerif");
- fontChoice.add("Monospaced");
- fontChoice.add("Dialog");
- fontChoice.add("DialogInput");
- fontFrame.add(fontChoice);
- fontChoice.addItemListener(new ItemListener() {
- @Override
- public void itemStateChanged(ItemEvent e) {
- System.out.println(e.getItem());
- tArea.setFont((Font) e.getItem());
- }
- });
- Label l1 = new Label("Select Font: ");
- l1.setBounds(50, 50, 100, 25);
- l1.setFont(menuFont);
- fontFrame.add(l1);
- Label l2 = new Label("Enter Size: ");
- l2.setBounds(50, 90, 100, 25);
- l2.setFont(menuFont);
- fontFrame.add(l2);
- TextField sizeField = new TextField();
- sizeField.setBounds(150, 90, 50, 23);
- sizeField.setFont(menuFont);
- fontFrame.add(sizeField);
- Button fontSubmit = new Button("Submit");
- fontSubmit.setBounds(50, 130, 60, 30);
- fontSubmit.setFont(menuFont);
- fontSubmit.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- // tArea.setFont(seleced, 1, sizeField.getText());
- }
- });
- fontFrame.add(fontSubmit);
- Button fontCancel = new Button("Cancel");
- fontCancel.setBounds(200, 130, 60, 30);
- fontCancel.setFont(menuFont);
- fontFrame.add(fontCancel);
- }
- }
- public class NotepadV1 {
- public static void main(String[] args) {
- new NotepadV1Main();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment