Results 1 to 6 of 6
  1. #1
    abrium is offline Member
    Join Date
    Jul 2010
    Posts
    8
    Rep Power
    0

    Default New to Java and GUI

    Hey everyone,

    I am new to Java and am trying to get a GUI to work and it is seeming to be more work that it is worth. This is what I have so far, can anyone tell me why when the JFrame pops up it is just all black?

    Java Code:
    package langInterp;
    
    
    import java.awt.BorderLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JPanel;
    import javax.swing.JFrame;
    import javax.swing.JTextField;
    import javax.swing.JButton;
    import javax.swing.JLabel;
    
    
    
    
    
    
    //Start of the Class for the front end.
    @SuppressWarnings("serial")
    public class frontEnd extends JPanel{
    	
    
    frontEnd(){
    	BorderLayout border = new BorderLayout();
    	JFrame frame = new JFrame();
    	frame.setLayout(border);
    	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	frame.setVisible(true);
    	frame.setSize(500,250);
    	frame.setLocation(450,450);
    	
    		JTextField textInput = new JTextField("", 25);
    	JButton btnGo = new JButton("Enter");
    	btnGo.addActionListener(new ActionListener() {
    		public void actionPerformed (ActionEvent e){
    			
    		}
    	});
    	
    		JLabel dispResult = new JLabel("");
    	frame.add(textInput, BorderLayout.NORTH);
    	frame.add(btnGo, BorderLayout.SOUTH);
    	frame.add(dispResult, BorderLayout.CENTER);
    	
    	
    }
    	
    
    
    	
    	
    	
    
    	public static void main(String[] args){
    javax.swing.SwingUtilities.invokeLater(new Runnable(){
    	public void run(){
    		new frontEnd();
    	}
    });
    	}
    }
    Thanks everyone. I have done .Net for two years and never done any real OOP it is all very new to me.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    You appear to be adding your components after calling setVisible(true). I suggest that you add your components first, call pack() on the JFrame, then set its location, then setVisible(true).

    Most importantly, go through the Sun Swing tutorials to learn all about Swing GUI's. Luck!

  3. #3
    abrium is offline Member
    Join Date
    Jul 2010
    Posts
    8
    Rep Power
    0

    Default

    damn, good ol' sequential execution. That isn't even a Java issue, thats me being retarded. Thanks a lot.

  4. #4
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    As a rule of thumb, setVisible(true) should always be the last call made to a Frame (or JFrame) of any sort during its setup. (Calls later to add/remove components, or resize the frame, do not count as setup) There may be exceptions, but I haven't found one yet :)
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  5. #5
    abrium is offline Member
    Join Date
    Jul 2010
    Posts
    8
    Rep Power
    0

    Default Thanks guys

    Thanks a lot. How do I mark this badboy as solved?

  6. #6
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    Should be an option under thread tools in the toolbar beneath the post reply button (at the top of the thread, not the one at the bottom.)
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

Posting Permissions

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