Hi all

I have made this small text editor and it works fine. I would just like you too look over it and give me any tips and things, that would make the code better (more acceptable to you).

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


public class FrostEditor extends JFrame 
{
	private JTextArea textArea;
	private Action _openAction = new OpenAction();
	private Action _saveAction = new SaveAction();
	private Action _aboutAction = new AboutAction();
	
	
	private JFileChooser _fileChooser = new JFileChooser();
   
		
	public FrostEditor()
	{
		
		
		//Arrange the Editing Pane
		textArea = new JTextArea(20, 50);
		textArea.setEditable(true);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		JScrollPane scrollingText = new JScrollPane(textArea);
	
		//Set all the panel to size and add the components
		JPanel content = new JPanel();
        content.setLayout(new BorderLayout());
        content.add(textArea, BorderLayout.CENTER);
        setContentPane(content);
    
        //Set the file menu bits
		JMenuBar menu = new JMenuBar();
		JMenu file = new JMenu("File");
		menu.add(file);
		file.add(_openAction);
		file.add(_saveAction);    
			
		//set the help menu bits
		JMenu helpMenu = menu.add(new JMenu("Help"));
        helpMenu.add(_aboutAction);   
        
		
		// Set the menuBar for the JFrame
		setJMenuBar(menu);
        setTitle("Frost Editor");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
          
	
		
		
		
	}
	
	public static void main(String[] args)
	{
		
        FrostEditor editor = new FrostEditor();
        editor.setLocationRelativeTo(null); // Center window.
        editor.setVisible(true);
		
	}
	
	class OpenAction extends AbstractAction {
        //============================================= constructor
        public OpenAction() {
            super("Open...");
            putValue(MNEMONIC_KEY, new Integer('O'));
        }
        
        //========================================= actionPerformed
        public void actionPerformed(ActionEvent e) {
            int retval = _fileChooser.showOpenDialog(FrostEditor.this);
            if (retval == JFileChooser.APPROVE_OPTION) {
                File f = _fileChooser.getSelectedFile();
                try {
                    FileReader reader = new FileReader(f);
                    textArea.read(reader, "");  // Use TextComponent read
                } catch (IOException ioex) {
                    System.out.println(e);
                    System.exit(1);
                }
            }
        }
    }
    
    
    
    
    //////////////////////////////////////////////////// inner class SaveAction
    class SaveAction extends AbstractAction {
        //============================================= constructor
        SaveAction() {
            super("Save...");
            putValue(MNEMONIC_KEY, new Integer('S'));
        }
        
        //========================================= actionPerformed
        public void actionPerformed(ActionEvent e) {
            int retval = _fileChooser.showSaveDialog(FrostEditor.this);
            if (retval == JFileChooser.APPROVE_OPTION) {
                File f = _fileChooser.getSelectedFile();
                try {
                    FileWriter writer = new FileWriter(f);
                    textArea.write(writer);  // Use TextComponent write
                } catch (IOException ioex) {
                    JOptionPane.showMessageDialog(FrostEditor.this, ioex);
                    System.exit(1);
                }
            }
        }
    }
    
    
    
    class AboutAction extends AbstractAction
    {
    	public AboutAction()
    	{
    		super("About");
    	}
    		
    		
            
            public void actionPerformed(ActionEvent e)
            {
            	JFrame About = new JFrame();
    			JLabel label = new JLabel("Author: Harry Angell");
    			JLabel label2 = new JLabel("Released under the MIT license");
    			JLabel label3 = new JLabel ("http://frost-editor.bountysource.com");
            	
    			setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            	About.setTitle("Frost Editor V1.10");
            	About.setVisible(true);
            	About.setSize(250, 120);
            	About.getContentPane().setLayout(new FlowLayout());
            	About.getContentPane().add(label);
            	About.getContentPane().add(label2);
            	About.getContentPane().add(label3);
            	
            	
            	//set the frame's location on screen
            	Toolkit toolkit = Toolkit.getDefaultToolkit();
				Dimension screenSize = toolkit.getScreenSize(); 
					
            	int x = (screenSize.width - About.getWidth()) / 2;
				int y = (screenSize.height - About.getHeight()) / 2; 
					
				About.setLocation(x, y); 
            	
            }
            
    		
    		
    	
    }



	
}

Many thanks

HLA91

Challenge: Implement a Dictionary/Thesaurus API, word-checking and a word-suggestion (after 3 characters) sub-menu below the current location of the user's cursor.

This article has been dead for over six months. Start a new discussion instead.