Results 1 to 2 of 2
  1. #1
    ocean is offline Senior Member
    Join Date
    Sep 2009
    Location
    Sweden/Borås
    Posts
    107
    Rep Power
    0

    Default Is this a "good" way to handle classes?

    Hi, Im trying to learn how classes and method´s really work. After reading and trying i come up with.
    Java Code:
    import javax.swing.JFrame;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JTextField;
    
    public class test1 {
    	 	
    	private JTextField createText(){
    		JTextField aTextField;
    		aTextField = new JTextField();
    		return aTextField;
    	}
          
    	private JMenuBar createMenu() {
    		JMenuBar aMenuBar;
        	        JMenu aMenu;
        	  
        	        aMenuBar = new JMenuBar();
        	        aMenu = new JMenu("Arkiv 1");
             	aMenuBar.add(aMenu);
        	        return aMenuBar;
    		}
    	
    	private static void gui() {
    			JFrame aFrame = new JFrame("gui");
    			aFrame.setVisible(true);
    			
    			test1 menu = new test1();
    			aFrame.setJMenuBar(menu.createMenu());
    			
    			test1 text = new test1();
    			aFrame.add(text.createText());
    						
    			aFrame.setSize(200, 200);
    			aFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	   }
    	
    	public static void main(String[] args) {
    	    gui();
    		}	
    	}
    Would like some critique.
    Last edited by ocean; 09-30-2009 at 01:19 PM.

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    10

    Default

    Quote Originally Posted by ocean View Post
    ...Would like some critique.
    You realize that
    Java Code:
    private JTextField createText(){
    		JTextField aTextField;
    		aTextField = new JTextField();
    		return aTextField;
    }
    can be collapsed into

    Java Code:
    private JTextField createText(){
    	return new JTextField();
    }
    Which should get you into thinking about whether you really need that method at all.

    If you are learning about methods don't bring Swing into it. Swing is a different beast altogether which you would need to learn later after understanding the methods basics.

Similar Threads

  1. Java, Military Format using "/" and "%" Operator!!
    By sk8rsam77 in forum New To Java
    Replies: 11
    Last Post: 02-26-2010, 04:03 AM
  2. getDisplayLanguage returns "en" not "English"
    By DD70 in forum New To Java
    Replies: 6
    Last Post: 08-12-2009, 11:22 PM
  3. Replies: 2
    Last Post: 01-24-2009, 07:56 PM
  4. Making A Set Of Classes "Importable"
    By JDCAce in forum Advanced Java
    Replies: 4
    Last Post: 12-05-2008, 10:11 AM
  5. Replies: 1
    Last Post: 10-20-2008, 07:35 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
  •