Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By Fubarable

Thread: Beginner GUI question

  1. #1
    stuckonjava is offline Senior Member
    Join Date
    Jan 2012
    Posts
    151
    Rep Power
    3

    Default Beginner GUI question

    Hi,

    I just started GUI and have a quick few questions, could someone explain to when building a GUI why some programs start with public class test extends JFrame and others don't use this extend and just start the class straight away.

    I don't know what the difference between these are.


    Thanks very much

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,237
    Rep Power
    19

    Default Re: Beginner GUI question

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    stuckonjava is offline Senior Member
    Join Date
    Jan 2012
    Posts
    151
    Rep Power
    3

    Default Re: Beginner GUI question

    for eg

    import java.awt.*;
    import javax.swing.*;

    public class Exercise1 extends JFrame {

    public Exercise1() {

    this.setSize(640, 480);
    this.setVisible(true);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );

    }

    } public static void main(String[] args) {
    }
    new Exercise1();

    And this:

    import javax.swing.*;
    public class test {

    public static void main(String[] args){

    JFrame frame = new JFrame("Test");
    frame.setVisible(true);
    frame.setSize(200,200);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);


    JLabel label = new JLabel("He;lo");
    JPanel panel = new JPanel();
    frame.add(panel);
    panel.add(label);

    JButton button = new JButton("click");
    panel.add(button);


    }

    }

    What is the difference between these and when would I use each? I understand the concept of creating a window and adding things to it etc

    Thanks

  4. #4
    stuckonjava is offline Senior Member
    Join Date
    Jan 2012
    Posts
    151
    Rep Power
    3

    Default Re: Beginner GUI question

    anyone?/

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

    Default Re: Beginner GUI question

    There are many that say that you should have no class extend another class unless the child class alters the fundamental behaviors of the parent -- i.e., that it overrides the parent's method(s). Most I think agree that programmers are better off favoring composition over inheritance, that is to create complex programs where your classes are enhanced by holding references to other classes rather than extend other classes.

    Myself, I try to avoid having classes extend top-level windows and in fact rarely have the need to do this.
    tnrh1 likes this.

Similar Threads

  1. Java Question [Beginner Question]
    By joker760 in forum New To Java
    Replies: 3
    Last Post: 12-13-2011, 04:01 PM
  2. Beginner Loop Question
    By katiebear128 in forum New To Java
    Replies: 6
    Last Post: 10-12-2011, 07:58 AM
  3. Beginner Question About Packages, etc
    By Humphrey Bogart in forum New To Java
    Replies: 15
    Last Post: 03-24-2011, 04:45 PM
  4. Beginner question about ArrayList
    By kesi in forum New To Java
    Replies: 3
    Last Post: 09-19-2009, 11:30 PM
  5. Another beginner question for AP test
    By DanK in forum New To Java
    Replies: 1
    Last Post: 04-27-2009, 05:36 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
  •