-4

I've got Java code to simply create a notepad project and I'm having trouble getting it to run. It seems the code generates warnings. I'm using NetBeans IDE 8.01 with Java. When I try to run the program I get the following in a box: firstprojectnote.FirstProjectNote wasn´t found in FirstProjectNote project. select main class <no main class found> I think it doesn't have a category for Main? Thanks for the help.

package firstprojectnote;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Scanner;
import java.io.*;

public class FirstProjectNote extends JFrame implements ActionListener {
    public class notepad {
        public void main(String args[]) {
            app.setVisible(true);
        }
    }

    private TextArea textArea = new TextArea("", 0, 0, TextArea.SCROLLBARS_VERTICAL_ONLY);
    private MenuBar menuBar = new MenuBar();
    private Menu file = new Menu();
    private MenuItem openFile = new MenuItem();
    private MenuItem saveFile = new MenuItem();
    private MenuItem close = new MenuItem();


    public FirstProjectNote() {
        this.setSize(500, 300);
        this.setTitle("Java Notepad Tutorial");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.textArea.setFont(new Font("Century Gothic", Font.BOLD, 12));
        this.getContentPane().setLayout(new BorderLayout());
        this.getContentPane().add(textArea);
        this.setMenuBar(this.menuBar);
        this.menuBar.add(this.file);
        this.file.setLabel("File");
        this.openFile.setLabel("Open");
        this.openFile.addActionListener(this);
        this.openFile.setShortcut(new MenuShortcut(KeyEvent.VK_O, false));
        this.file.add(this.openFile);
        this.saveFile.setLabel("Save");
        this.saveFile.addActionListener(this);
        this.saveFile.setShortcut(new MenuShortcut(KeyEvent.VK_S, false));
        this.file.add(this.saveFile);
        this.close.setLabel("Close");
        this.close.setShortcut(new MenuShortcut(KeyEvent.VK_F4, false));
        this.close.addActionListener(this);
        this.file.add(this.close);
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == this.close)
            this.dispose();
        else if (e.getSource() == this.openFile) {
            JFileChooser open = new JFileChooser();
            int option = open.showOpenDialog(this);
            if (option == JFileChooser.APPROVE_OPTION) {
                this.textArea.setText("");
                try {
                    Scanner scan = new Scanner(new
                            FileReader(open.getSelectedFile().getPath()));
                    while (scan.hasNext())
                        this.textArea.append(scan.nextLine() + "\n");
                } catch (Exception ex) {
                    System.out.println(ex.getMessage());
                }
            }
        } else if (e.getSource() == this.saveFile) {
            JFileChooser save = new JFileChooser();
            int option = save.showSaveDialog(this);
            if (option == JFileChooser.APPROVE_OPTION) {
                try {
                    BufferedWriter out = new BufferedWriter(new
                            FileWriter(save.getSelectedFile().getPath()));
                    out.write(this.textArea.getText());
                    out.close();
                } catch (Exception ex) {
                    System.out.println(ex.getMessage());
                }
            }
        }
    }
}
rgettman
  • 176,041
  • 30
  • 275
  • 357

1 Answers1

1

I'm sure your main method needs to be static.

    public static void main(String args[]) {
        app.setVisible(true);
    }
  • thanks for the answer, this seems to create an error "illegal static declaration statis only allowed in constant declarations." so I took it off – NAME12345 Dec 23 '14 at 21:57
  • Let me have a look at it for you. Is the only thing that changed in the code the static declaration? – MarkHorwell Dec 24 '14 at 05:22
  • Sorry that took a while. Christmas kinda got in my way. Have you tries making the inner class static as well. See the following ` public static class notepad { public static void main(String args[]) { app.setVisible(true); } }'. I believe the issue here is that the static method hosted within a none static nested class. Give it a go and let me know if there is still an issue. – MarkHorwell Dec 26 '14 at 05:33