DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   Java (http://www.daniweb.com/forums/forum9.html)
-   -   Clean up my code (http://www.daniweb.com/forums/thread142452.html)

HLA91 Aug 26th, 2008 6:17 pm
Clean up my code
 
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

Ezzaral Aug 26th, 2008 6:54 pm
Re: Clean up my code
 
Well, the most noticeable thing right off the bat is wildly inconsistent naming and formatting conventions. Yes, they actually matter.
http://java.sun.com/docs/codeconv/ht...nvTOC.doc.html

Alex Edwards Aug 26th, 2008 7:39 pm
Re: Clean up my code
 
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.


All times are GMT -4. The time now is 11:03 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC