Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Java problem

  • 23-11-2004 01:08PM
    #1
    Registered Users, Registered Users 2 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, Registered Users 2 Posts: 2,753 ✭✭✭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, Registered Users 2 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, Registered Users 2 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, Registered Users 2 Posts: 17,727 ✭✭✭✭Sherifu


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

    RKM.


  • Registered Users, Registered Users 2 Posts: 2,753 ✭✭✭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, Registered Users 2 Posts: 2,753 ✭✭✭Darwin




  • Registered Users, Registered Users 2 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, Registered Users 2 Posts: 937 ✭✭✭Diddy Kong


    cheers lads


  • Registered Users, Registered Users 2 Posts: 27,517 ✭✭✭✭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, Registered Users 2 Posts: 27,517 ✭✭✭✭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, Registered Users 2 Posts: 27,517 ✭✭✭✭GreeBo


    np.


Advertisement