Results 1 to 3 of 3
- 09-25-2012, 09:43 PM #1
Member
- Join Date
- Sep 2012
- Posts
- 1
- Rep Power
- 0
Re: I'm new and stuck, please help :(
Hello i wrote a notepad, and i got some errors, im like new in Java, and i will be very glad if u would help me, here is the code, a part of it, and the problem in the photo pls help, thanks!

and here is the hole code
Java Code:/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package notepad; import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.Scanner; import java.io.*; /** * * @author AllStar */ public class notepad1 { // the main method, for actually creating our notepad and setting it to visible. public class main{ public static void main(String args[]) { Notepad app = new Notepad(); app.setVisible(true); } } public class Notepad extends JFrame implements ActionListener { private TextArea textArea = new TextArea("", 0,0, TextArea.SCROLLBARS_VERTICAL_ONLY); private MenuBar menuBar = new MenuBar(); // first, create a MenuBar item private Menu file = new Menu(); // our File menu // what's going in File? let's see... private MenuItem openFile = new MenuItem(); // an open option private MenuItem saveFile = new MenuItem(); // a save option private MenuItem close = new MenuItem(); // and a close option! public Notepad() { this.setSize(500, 300); // set the initial size of the window this.setTitle(" Notepad "); // set the title of the window setDefaultCloseOperation(EXIT_ON_CLOSE); // set the default close operation (exit when it gets closed) this.textArea.setFont(new Font("Times New Roman", Font.BOLD, 12)); // set a default font for the TextArea // this is why we didn't have to worry about the size of the TextArea! this.getContentPane().setLayout(new BorderLayout()); // the BorderLayout bit makes it fill it automatically this.getContentPane().add(textArea); // add our menu bar into the GUI this.setMenuBar(this.menuBar); this.menuBar.add(this.file); // we'll configure this later // first off, the design of the menuBar itself. Pretty simple, all we need to do // is add a couple of menus, which will be populated later on this.file.setLabel("File"); // now it's time to work with the menu. I'm only going to add a basic File menu // but you could add more! // now we can start working on the content of the menu~ this gets a little repetitive, // so please bare with me! // time for the repetitive stuff. let's add the "Open" option this.openFile.setLabel("Open"); // set the label of the menu item this.openFile.addActionListener(this); // add an action listener (so we know when it's been clicked this.openFile.setShortcut(new MenuShortcut(KeyEvent.VK_O, false)); // set a keyboard shortcut this.file.add(this.openFile); // add it to the "File" menu // and the save... this.saveFile.setLabel("Save"); this.saveFile.addActionListener(this); this.saveFile.setShortcut(new MenuShortcut(KeyEvent.VK_S, false)); this.file.add(this.saveFile); // and finally, the close option this.close.setLabel("Close"); // along with our "CTRL+F4" shortcut to close the window, we also have // the default closer, as stated at the beginning of this tutorial. // this means that we actually have TWO shortcuts to close: // 1) the default close operation (example, Alt+F4 on Windows) // 2) CTRL+F4, which we are about to define now: (this one will appear in the label) this.close.setShortcut(new MenuShortcut(KeyEvent.VK_F4, false)); this.close.addActionListener(this); this.file.add(this.close); } public void actionPerformed (ActionEvent e) { // if the source of the event was our "close" option if (e.getSource() == this.close) this.dispose(); // dispose all resources and close the application // if the source was the "open" option else if (e.getSource() == this.openFile) { JFileChooser open = new JFileChooser(); // open up a file chooser (a dialog for the user to browse files to open) int option = open.showOpenDialog(this); // get the option that the user selected (approve or cancel) // NOTE: because we are OPENing a file, we call showOpenDialog~ // if the user clicked OK, we have "APPROVE_OPTION" // so we want to open the file if (option == JFileChooser.APPROVE_OPTION) { this.textArea.setText(""); // clear the TextArea before applying the file contents try { // create a scanner to read the file (getSelectedFile().getPath() will get the path to the file) Scanner scan = new Scanner(new FileReader(open.getSelectedFile().getPath())); while (scan.hasNext()) // while there's still something to read this.textArea.append(scan.nextLine() + "\n"); // append the line to the TextArea } catch (Exception ex) { // catch any exceptions, and... // ...write to the debug console System.out.println(ex.getMessage()); } } } // and lastly, if the source of the event was the "save" option else if (e.getSource() == this.saveFile) { JFileChooser save = new JFileChooser(); // again, open a file chooser int option = save.showSaveDialog(this); // similar to the open file, only this time we call // showSaveDialog instead of showOpenDialog // if the user clicked OK (and not cancel) if (option == JFileChooser.APPROVE_OPTION) { try { // create a buffered writer to write to a file BufferedWriter out = new BufferedWriter(new FileWriter(save.getSelectedFile().getPath())); out.write(this.textArea.getText()); // write the contents of the TextArea to the file out.close(); // close the file stream } catch (Exception ex) { // again, catch any exceptions and... // ...write to the debug console System.out.println(ex.getMessage()); } } } } } }Last edited by Norm; 09-25-2012 at 10:16 PM. Reason: Moved to New / Added code tags
- 09-25-2012, 10:20 PM #2
Re: I'm new and stuck, please help :(
Why are you trying to use inner classes? Get rid of the inner classes and use just one class: Notepad. Put the main() method in the NotePad class.
See tutorial on how to define classes: Classes (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
Nested Classes (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
The error message gives clues on how to solve the problem.If you don't understand my response, don't ignore it, ask a question.
- 09-25-2012, 10:38 PM #3
Re: I'm new and stuck, please help :(
Please go through the Forum Rules -- particularly the third paragraph.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
Similar Threads
-
Could someone help me with this please. I am a little stuck
By Dave013 in forum New To JavaReplies: 4Last Post: 11-13-2011, 03:58 PM -
Stuck, need help please
By Johnny2009 in forum New To JavaReplies: 3Last Post: 11-03-2011, 10:25 PM -
I'm stuck help!!!
By nobody58 in forum Advanced JavaReplies: 2Last Post: 03-18-2010, 02:52 PM -
Im on my last lab!!!! And im stuck...:(
By clanboru15 in forum New To JavaReplies: 5Last Post: 03-13-2009, 01:44 AM -
Stuck in need of help!
By Zombie_Leg! in forum New To JavaReplies: 1Last Post: 09-23-2008, 02:22 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks