| | |
Clean up my code
![]() |
•
•
Join Date: Oct 2006
Posts: 179
Reputation:
Solved Threads: 2
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).
Many thanks
HLA91
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)
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
You know your a geek, if you introduce your wife as "mylady@home.wife"
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
http://java.sun.com/docs/codeconv/ht...nvTOC.doc.html
![]() |
Similar Threads
- Clean Previous Next Script for MySQL results (PHP)
- Pixelated Images in IE6 with clean install? (Windows NT / 2000 / XP)
- A little help tweaking the my code (Python)
- please tell me what is wrong with the code? (PHP)
- Cleaning up my code. (ColdFusion)
- Why won't this code work? (VB.NET)
- Making installs through the source (*nix Software)
Other Threads in the Java Forum
- Previous Thread: Type mismatch: Cannot convert from void to double
- Next Thread: Trying to draw text at an angle
| Thread Tools | Search this Thread |
account android api applet application array arrays automation bidirectional binary birt bluetooth class classes client code columns component constructor database designadrawingapplicationusingjavajslider draw eclipse error errors exception expand fractal game givemetehcodez graphics gui guidancer homework html ide image inetaddress inheritance integer intellij j2me java javamicroeditionuseofmotionsensor javaprojects jlabel jme jni jpanel jtextfield jtree julia linux list loop map method methods midlethttpconnection mobile mobiledevelopmentcreatejar monitoring myaggfun netbeans newbie nullpointerexception open-source oracle plazmic print problem program project property recursion ria scanner search server set sharepoint smart sms smsspam sort sourcelabs splash sql sqlite static string subclass support swing testautomation threads tree unlimited webservices windows






