Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: defaultCloseOperation doesn't work plus JButttons don't show up

  1. #1
    Junior Member
    Join Date
    Jul 2013
    Posts
    24
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default defaultCloseOperation doesn't work plus JButttons don't show up

    So I've got the following code:

    import java.awt.*;
    import javax.swing.*;
     
     
    public class testframe extends JFrame{
     
      private JButton play;
      private JButton highscore;
      private JButton close;
     
     public static void main(String[] args) {
     
      Frame frame = new Frame("Test");
      frame.setSize(600,600);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setLayout(null);
      frame.setVisible(true);
     }
     
     public testframe(String title) {
      super(title);
     
      play = new JButton("Play");
      play.setBounds(120, 40, 160, 40);
      add(play);
     
      highscore = new JButton("Highscores");
      highscore.setBounds(120, 120, 160, 40);
      add(highscore);
     
     
      close = new JButton("Close");
      close.setBounds(120, 200, 160, 40);
      add(close);    
     
     
     }
     
    }

    When I start it, it gives me the following error:

    Exception in thread "main" java.lang.Error: Unresolved compilation problem:
    The method setDefaultCloseOperation(int) is undefined for the type Frame

    at testframe.main(testframe.java:15)

    Then I deleted that line to see if it's the only problem. Now the frame shows up, but there aren't any buttons. It's just an empty window.

    How do I fix this?
    Last edited by Gugubo; October 27th, 2013 at 05:34 AM. Reason: I typed online instead of only. wtf


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: defaultCloseOperation doesn't work plus JButttons don't show up

    You made common mistakes, confusing what extending does and created another object. Here are some very minor changes to your code to bring it back in line. I also changed some names to respect Java's naming conventions:
    import java.awt.*;
    import javax.swing.*;
     
    // GB: renamed the class
    public class TestFrame extends JFrame
    {
        private JButton play;
        private JButton highscore;
        private JButton close;
     
        public static void main ( String[] args )
        {
            // GB: new code
            TestFrame frame = new TestFrame( "Test" );
     
            // old code commented out
    //        Frame frame = new Frame ( "Test" );
            frame.setSize ( 600, 600 );
            frame.setDefaultCloseOperation ( JFrame.EXIT_ON_CLOSE );
            frame.setLayout ( null );
            frame.setVisible ( true );
     
        } // end method main()
     
        // GB: renamed the constructor
        public TestFrame ( String title )
        {
            super ( title );
            play = new JButton ( "Play" );
            play.setBounds ( 120, 40, 160, 40 );
            add ( play );
            highscore = new JButton ( "Highscores" );
            highscore.setBounds ( 120, 120, 160, 40 );
            add ( highscore );
            close = new JButton ( "Close" );
            close.setBounds ( 120, 200, 160, 40 );
            add ( close );
     
        } // end constructor
     
    } // end class TestFrame
    This is not the final answer and even all correct as it is, but I respect that you're learning and taking it a step at a time. A habit you should start NOW is starting Swing apps on the EDT by using SwingUtilities.invokeLater() in the main() method to start the Swing app. That'll be in my next post if you keep this one going.

Similar Threads

  1. Replies: 1
    Last Post: November 2nd, 2012, 04:16 PM
  2. Why doesn't this work!
    By Alex-Green in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 20th, 2012, 04:25 AM
  3. [SOLVED] Why doesn't my Applet show up in my browser?
    By SendBorg in forum Java Applets
    Replies: 3
    Last Post: January 29th, 2012, 08:30 AM
  4. Why doesn't this work?
    By mailman in forum What's Wrong With My Code?
    Replies: 6
    Last Post: January 9th, 2012, 11:19 PM
  5. JFrame declared as setAlwaysOnTop doesn't stay on top during slide show
    By ravindra_appikatla in forum AWT / Java Swing
    Replies: 1
    Last Post: March 30th, 2010, 09:08 AM