943,724 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 697
  • Java RSS
Aug 26th, 2008
0

Clean up my code

Expand Post »
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).

java Syntax (Toggle Plain Text)
  1. import javax.swing.*;
  2. import java.io.*;
  3. import java.awt.*;
  4. import java.awt.event.*;
  5.  
  6.  
  7. public class FrostEditor extends JFrame
  8. {
  9. private JTextArea textArea;
  10. private Action _openAction = new OpenAction();
  11. private Action _saveAction = new SaveAction();
  12. private Action _aboutAction = new AboutAction();
  13.  
  14.  
  15. private JFileChooser _fileChooser = new JFileChooser();
  16.  
  17.  
  18. public FrostEditor()
  19. {
  20.  
  21.  
  22. //Arrange the Editing Pane
  23. textArea = new JTextArea(20, 50);
  24. textArea.setEditable(true);
  25. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  26. JScrollPane scrollingText = new JScrollPane(textArea);
  27.  
  28. //Set all the panel to size and add the components
  29. JPanel content = new JPanel();
  30. content.setLayout(new BorderLayout());
  31. content.add(textArea, BorderLayout.CENTER);
  32. setContentPane(content);
  33.  
  34. //Set the file menu bits
  35. JMenuBar menu = new JMenuBar();
  36. JMenu file = new JMenu("File");
  37. menu.add(file);
  38. file.add(_openAction);
  39. file.add(_saveAction);
  40.  
  41. //set the help menu bits
  42. JMenu helpMenu = menu.add(new JMenu("Help"));
  43. helpMenu.add(_aboutAction);
  44.  
  45.  
  46. // Set the menuBar for the JFrame
  47. setJMenuBar(menu);
  48. setTitle("Frost Editor");
  49. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  50. pack();
  51.  
  52.  
  53.  
  54.  
  55.  
  56. }
  57.  
  58. public static void main(String[] args)
  59. {
  60.  
  61. FrostEditor editor = new FrostEditor();
  62. editor.setLocationRelativeTo(null); // Center window.
  63. editor.setVisible(true);
  64.  
  65. }
  66.  
  67. class OpenAction extends AbstractAction {
  68. //============================================= constructor
  69. public OpenAction() {
  70. super("Open...");
  71. putValue(MNEMONIC_KEY, new Integer('O'));
  72. }
  73.  
  74. //========================================= actionPerformed
  75. public void actionPerformed(ActionEvent e) {
  76. int retval = _fileChooser.showOpenDialog(FrostEditor.this);
  77. if (retval == JFileChooser.APPROVE_OPTION) {
  78. File f = _fileChooser.getSelectedFile();
  79. try {
  80. FileReader reader = new FileReader(f);
  81. textArea.read(reader, ""); // Use TextComponent read
  82. } catch (IOException ioex) {
  83. System.out.println(e);
  84. System.exit(1);
  85. }
  86. }
  87. }
  88. }
  89.  
  90.  
  91.  
  92.  
  93. //////////////////////////////////////////////////// inner class SaveAction
  94. class SaveAction extends AbstractAction {
  95. //============================================= constructor
  96. SaveAction() {
  97. super("Save...");
  98. putValue(MNEMONIC_KEY, new Integer('S'));
  99. }
  100.  
  101. //========================================= actionPerformed
  102. public void actionPerformed(ActionEvent e) {
  103. int retval = _fileChooser.showSaveDialog(FrostEditor.this);
  104. if (retval == JFileChooser.APPROVE_OPTION) {
  105. File f = _fileChooser.getSelectedFile();
  106. try {
  107. FileWriter writer = new FileWriter(f);
  108. textArea.write(writer); // Use TextComponent write
  109. } catch (IOException ioex) {
  110. JOptionPane.showMessageDialog(FrostEditor.this, ioex);
  111. System.exit(1);
  112. }
  113. }
  114. }
  115. }
  116.  
  117.  
  118.  
  119. class AboutAction extends AbstractAction
  120. {
  121. public AboutAction()
  122. {
  123. super("About");
  124. }
  125.  
  126.  
  127.  
  128. public void actionPerformed(ActionEvent e)
  129. {
  130. JFrame About = new JFrame();
  131. JLabel label = new JLabel("Author: Harry Angell");
  132. JLabel label2 = new JLabel("Released under the MIT license");
  133. JLabel label3 = new JLabel ("http://frost-editor.bountysource.com");
  134.  
  135. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  136. About.setTitle("Frost Editor V1.10");
  137. About.setVisible(true);
  138. About.setSize(250, 120);
  139. About.getContentPane().setLayout(new FlowLayout());
  140. About.getContentPane().add(label);
  141. About.getContentPane().add(label2);
  142. About.getContentPane().add(label3);
  143.  
  144.  
  145. //set the frame's location on screen
  146. Toolkit toolkit = Toolkit.getDefaultToolkit();
  147. Dimension screenSize = toolkit.getScreenSize();
  148.  
  149. int x = (screenSize.width - About.getWidth()) / 2;
  150. int y = (screenSize.height - About.getHeight()) / 2;
  151.  
  152. About.setLocation(x, y);
  153.  
  154. }
  155.  
  156.  
  157.  
  158.  
  159. }
  160.  
  161.  
  162.  
  163.  
  164. }



Many thanks

HLA91
Similar Threads
Reputation Points: 7
Solved Threads: 2
Junior Poster
HLA91 is offline Offline
177 posts
since Oct 2006
Aug 26th, 2008
0

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
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 838
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
Aug 26th, 2008
0

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.
Reputation Points: 392
Solved Threads: 108
Posting Shark
Alex Edwards is offline Offline
971 posts
since Jun 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Type mismatch: Cannot convert from void to double
Next Thread in Java Forum Timeline: Trying to draw text at an angle





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC