Results 1 to 6 of 6

Thread: Syntax correct?

  1. #1
    dbashby's Avatar
    dbashby is offline Senior Member
    Join Date
    Mar 2009
    Posts
    223
    Rep Power
    11

    Question Syntax correct?

    I am starting to rewrite some code from a previous app, I was advised to use the correct syntax etc.


    Could I have a programmer just take a look at this code and give me an opinion please

    Thanks in advance.

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    
    // Creates a simple frame to add all the other components
    
    // Create a simple GUI window
    public class MainWindow {
    
       private static void createWindow() {
    
          JFrame frame = new JFrame();
    
    	  frame.setTitle("Project Frame");
    	  frame.setSize(800,600);
    	  frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
    	  frame.setVisible(true);
    
    	  // center on screen start
    		int widthWindow = frame.getWidth();
    		int heightWindow = frame.getHeight();
    
    		Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
    		int X = (screen.width / 2) - (widthWindow / 2); // Center horizontally.
    		int Y = (screen.height / 2) - (heightWindow / 2); // Center vertically.
    
    		frame.setBounds(X, Y, widthWindow, heightWindow);
    
    	  // center on screen end
    
    	  // start of icon code for frame icon
    
    	     	Image icon = Toolkit.getDefaultToolkit().getImage("icon.gif");
    	     	Label lblicon = new Label("",Label.LEFT);
    	     	frame.add(lblicon);
    	     	frame.setIconImage(icon);
    
    	  // end of icon code
    
       }
    
       public static void main(String[] args) {
    
          createWindow();
    
       }
    }

  2. #2
    mrmatt1111's Avatar
    mrmatt1111 is offline Senior Member
    Join Date
    Aug 2009
    Location
    San Jose, CA, USA
    Posts
    320
    Rep Power
    11

    Default

    It compiles, thus is syntactically correct... what would you like an opinion on?
    Last edited by mrmatt1111; 09-21-2009 at 10:49 PM.
    My Hobby Project: LegacyClone

  3. #3
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    11

    Default

    probably coding conventions. you shouldn't indent when there isn't a change in scope.

  4. #4
    dbashby's Avatar
    dbashby is offline Senior Member
    Join Date
    Mar 2009
    Posts
    223
    Rep Power
    11

    Question

    But otherwise ok?

    Also when adding the panels I should use another public class yes?

    and the same for the components such as textfields, radiobuttons, labels and buttons or as this is part of the gui should I add them all within the MainWindow class?

    Sorry but I still havent got my head round java yet so advice would be helful.

    Thanks in advance

  5. #5
    dbashby's Avatar
    dbashby is offline Senior Member
    Join Date
    Mar 2009
    Posts
    223
    Rep Power
    11

    Default

    It compiles, thus is syntactically correct... what would you like an opinion on?
    Just to say whether I was heading in the right direction as far as what is expected of a programmer.

  6. #6
    devunion is offline Member
    Join Date
    Jun 2008
    Posts
    56
    Rep Power
    0

    Default

    1. Use frame.setLocationRelativeTo(null) instead of your "center on screen code".
    2. It can be optimized if you need to call createWindow() more than one time.
    Try Controls4J - Advanced Swing Components.

Similar Threads

  1. Is this the correct Output?
    By Teny in forum New To Java
    Replies: 17
    Last Post: 04-13-2009, 12:52 PM
  2. Output correct grammar
    By JordashTalon in forum New To Java
    Replies: 2
    Last Post: 03-06-2009, 12:22 AM
  3. Please suggest me the correct approach!
    By rjuyal in forum Advanced Java
    Replies: 6
    Last Post: 05-05-2008, 02:54 PM
  4. Is my Pseudocode correct?
    By Clemenza1983 in forum New To Java
    Replies: 0
    Last Post: 01-29-2008, 04:07 AM
  5. To correct forum
    By Jman in forum Introductions
    Replies: 3
    Last Post: 01-18-2008, 02:33 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •