Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Java problem

Options
  • 23-11-2004 1:08pm
    #1
    Registered Users Posts: 937 ✭✭✭


    Im havin trouble makin a basic window with java...see code below..any help/suggestions greatly appreciated

    main.java
    class ShowAFrame 
    {
       
        public static void main(String args[])
        {
          new SimpleFrame();
        }
        
    }
    

    SimpleFrame.java
    import java.awt.FlowLayout;
    import javax.swing.JFrame;
    import javax.swing.JButton;
    
    
    class SimpleFrame extends JFrame 
    
    {
        public SimpleFrame()
        {
            setTitle("Don't click the button!");
            setLayout(new FlowLayout());
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            add(new JButton("Panic"));
            setSize(300,100);
            setVisible(true);
        }
        
    }
    

    there all in a package (using netbeans 4)


Comments

  • Registered Users Posts: 2,671 ✭✭✭Darwin


    In main add:

    SimpleFrame myFrame = new SimpleFrame();
    myFrame.show();

    I haven't bothered to run your code, but I presume your code compiles and the Frame is not showing.


  • Registered Users Posts: 261 ✭✭HaVoC


    put:
    SimpleFrame f = new SimpleFrame();
    f.pack();
    f.setVisible(true);

    in your main method you have to run pack() and setVisible(true) for the window to actually appear.


  • Registered Users Posts: 937 ✭✭✭Diddy Kong


    nah, they dont seem to help very much, i still get the errors
    errors:
    java.lang.NoClassDefFoundError: ShowAFrame/main
    Exception in thread "main" 
    Java Result: 1
    


  • Registered Users Posts: 17,727 ✭✭✭✭Sherifu


    Shouldn't main.java be called ShowAFrame.java?

    RKM.


  • Registered Users Posts: 2,671 ✭✭✭Darwin


    put:
    SimpleFrame f = new SimpleFrame();
    f.pack();
    f.setVisible(true);

    in your main method you have to run pack() and setVisible(true) for the window to actually appear

    setVisible has been set to true in the constructor already.

    Shouldn't main.java be called ShowAFrame.java?

    Of course..didn't spot this.


  • Advertisement
  • Closed Accounts Posts: 92 ✭✭tempest


    getContentPane().add(new JButton("Panic"));


  • Registered Users Posts: 2,671 ✭✭✭Darwin




  • Registered Users Posts: 261 ✭✭HaVoC


    Darwin wrote:
    setVisible has been set to true in the constructor already.




    Of course. didn’t spot this.

    oh right didn’t spot that just .pack() so in the constructor before .setVisible(true);
    .show() has been deprecated by .setVisible(true);


  • Registered Users Posts: 937 ✭✭✭Diddy Kong


    cheers lads


  • Registered Users Posts: 27,163 ✭✭✭✭GreeBo


    rmulryan wrote:
    Shouldn't main.java be called ShowAFrame.java?

    RKM.
    yep
    :rolleyes:


  • Advertisement
  • Closed Accounts Posts: 18 moonser


    does anyone know where i can download the complete java api file so i dont have to use the book please.


  • Registered Users Posts: 27,163 ✭✭✭✭GreeBo


    moonser wrote:
    does anyone know where i can download the complete java api file so i dont have to use the book please.
    1.4.2 JavaDoc


  • Closed Accounts Posts: 18 moonser


    cheers man


  • Registered Users Posts: 27,163 ✭✭✭✭GreeBo


    np.


Advertisement