| | |
Clean up my code
![]() |
•
•
Join Date: Oct 2006
Posts: 172
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 |
-xlint 911 actionlistener addressbook android applet application array automation bi binary blackberry block bluetooth character class client code compile compiler component consumer csv database desktop developmenthelp eclipse error fractal ftp game gameprogramming givemetehcodez graphics gui health html image j2me j2seprojects japplet java javaarraylist javac javaee javaprojects jetbrains jni jpanel jtable julia learningresources lego linked linux mac main method mobile netbeans notdisplaying number objects online oriented printf problem program projects qt recursion replaydirector reporting researchinmotion rotatetext rsa scanner screen se server set singleton sms sort sql string swing system test textfields threads time title tree tutorial-sample ubuntu update variablebinding windows xor






