RSS Forums RSS

Clean up my code

Please support our Java advertiser: Programming Forums
Reply
Posts: 156
Reputation: HLA91 is an unknown quantity at this point 
Solved Threads: 2
HLA91 HLA91 is offline Offline
Junior Poster

Clean up my code

  #1  
Aug 26th, 2008
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).

  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
You know your a geek, if you introduce your wife as "mylady@home.wife"
AddThis Social Bookmark Button
Reply With Quote  
Posts: 4,111
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 458
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Clean up my code

  #2  
Aug 26th, 2008
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
Reply With Quote  
Posts: 947
Reputation: Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough 
Solved Threads: 106
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Posting Shark

Re: Clean up my code

  #3  
Aug 26th, 2008
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.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.



Views: 419 | Replies: 2 | Currently Viewing: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 11:03 pm.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC