Results 1 to 2 of 2
- 11-22-2010, 05:18 PM #1
Member
- Join Date
- Oct 2010
- Posts
- 7
- Rep Power
- 0
need help with few parts in java program please!
Hello all, I am currently writting this program that is supposed to be just like notepad. It is going to have a File drop down menu with 4 selections: New, Open, Save, and Exit. What i have so far is written all the code out and need help with the open and save. The new button clears the text area and I have that, the exit button is supposed to exit the program and I have that done. I have the save part mostly done but I have to make the extension for the file a .cit file (that is what the instructor of the class said) and I think i have to use a tokenizer but I'm not exactly sure how to do that. The open button is supposed to open a file with a .cit extension but even when I try to save one without an extension and then attempt to open it, it still won't open. Anyways this is what i have so far, the program and then the test program. Thanks for the help in advance.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
/**
* @(#)MemoPad.java
*
* @
* @version 1.00 10/15/2010
*/
public class MemoPad extends JFrame implements ActionListener {
//creats all variables to be used in the program and sets them to private
private JMenuBar menuBar = new JMenuBar();
private JMenu fileMenu = new JMenu("File");
private JMenuItem new1 = new JMenuItem("New");
private JMenuItem open = new JMenuItem("Open");
private JMenuItem save = new JMenuItem("Save");
private JMenuItem exit = new JMenuItem("Exit");
private JTextArea display;
private static Scanner inFile;
public String input1, input2;
private static PrintWriter pwOut = new PrintWriter (System.out, true);
private static PrintWriter pwErr = new PrintWriter (System.err, true);
private static PrintWriter outFile;
/**
* Constructs MemoPad and displays the menu items
*
*/
public MemoPad(String title) {
super(title);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//displays the panel and creats 1 column 1 row
JPanel displayPanel = new JPanel (new GridLayout(1,1,50,50));
display = new JTextArea(); //sets display to a new JTextArea
display.setLineWrap(true); //makes text area wrap text
//adds menu item new1 to the JPanel and sets it to center and adds an action listener
fileMenu.add(new1);
menuBar.add(fileMenu);
setJMenuBar(menuBar);
displayPanel.add(display);
add(displayPanel, BorderLayout.CENTER);
new1.addActionListener(this);
//adds menu item open to the JPanel and sets it to center
fileMenu.add(open);
menuBar.add(fileMenu);
setJMenuBar(menuBar);
displayPanel.add(display);
add(displayPanel, BorderLayout.CENTER);
open.addActionListener(this);
//adds the menu item save to the JPanel and sets it to center
fileMenu.add(save);
menuBar.add(fileMenu);
setJMenuBar(menuBar);
displayPanel.add(display);
add(displayPanel, BorderLayout.CENTER);
save.addActionListener(this);
//adds a separator
fileMenu.addSeparator();
//adds the menu item exit to the JPanel and sets it to center and then adds an action listener
fileMenu.add(exit);
menuBar.add(fileMenu);
setJMenuBar(menuBar);
displayPanel.add(display);
add(displayPanel, BorderLayout.CENTER);
exit.addActionListener(this);
}
/**
*
* The action listener method which closes the program if the exit menu item is clicked
* Or clears the JTextArea when the new menu item is clicked
* @param exit the menu item clicked
* @param new1 the menu item clicked
*/
public void actionPerformed (ActionEvent ae) {
if(ae.getSource() == exit) {
System.exit(0);
}else if(ae.getSource() == new1) {
display.setText(" ");
}else if(ae.getSource() == open){
input1 = JOptionPane.showInputDialog(null,"Please enter a file name");
try{
inFile = new Scanner (new FileInputStream(input1));
}catch (FileNotFoundException fnfe) {
pwErr.println("Error opening file");
}
while(inFile.hasNextLine()) {
String output = inFile.nextLine();
pwOut.println(output);
}
}else if(ae.getSource() == save){
input2 = JOptionPane.showInputDialog(null,"Please enter a file name");
try{
outFile = new PrintWriter (new FileOutputStream(input2, true));
}catch (FileNotFoundException fnfe) {
pwErr.println("Error writing to file");
}
JOptionPane.showMessageDialog(null,"Memo saved in filename " + input2);
}
}
/**
* Method to add a separator after the save menu item and before the exit menu item
*
*/
public void addSeparator() {
}
}
TEST PROGRAM:
import java.awt.*;
/**
* @(#)TestMemoPad.java
*
* @
* @version 1.00 10/15/2010
*/
public class TestMemoPad {
private final static int WIDTH = 600;
private final static int HEIGHT = 300;
/**
* @param args the command line arguments
*/
public static void main (String [] args) {
MemoPad screen = new MemoPad("Memo Pad");
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int screenWidth = screenSize.width;
int screenHeight = screenSize.height;
screen.setSize(WIDTH,HEIGHT);
Dimension frameSize = screen.getSize();
int x = (screenWidth - frameSize.width)/2;
int y = (screenHeight - frameSize.height)/2;
screen.setLocation(x,y);
screen.setVisible(true);
}
}
- 11-22-2010, 05:26 PM #2
Break your problem down into smaller pieces and post an SSCCE about a specific part. If you're having trouble saving a file, post an SSCCE that does just that. Loading a file is a different piece. The GUI is yet another piece. Keep them separate until you have them all working by themselves perfectly.
Lesson: Basic I/O (The Java™ Tutorials > Essential Classes)
Similar Threads
-
how to read only parts of textfile by java
By smn in forum SWT / JFaceReplies: 2Last Post: 10-08-2010, 03:31 AM -
Cutting parts of an image and reordering them
By pedjacar in forum Java 2DReplies: 2Last Post: 10-16-2008, 09:46 AM -
Breaking huge text into multiple parts.Please help..
By waNnY in forum New To JavaReplies: 2Last Post: 02-18-2008, 04:24 AM -
Selecting parts of an image
By shaungoater in forum Java 2DReplies: 1Last Post: 12-15-2007, 10:06 PM -
Jtree - making parts editable
By kmarie in forum AWT / SwingReplies: 1Last Post: 07-27-2007, 02:34 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks