Results 1 to 5 of 5
  1. #1
    Appel is offline Member
    Join Date
    May 2012
    Posts
    5
    Rep Power
    0

    Default Alright, Now I'm catching on, but stuck on something....

    Bear with me, I'm a procedural programmer learning the JAVA way. There just a few things I don't understand, it's not about the little details, but the whole big picture, I come from the mind set of setting variables, setting up the display, and reacting (branching to a procedure) based on the user (or other input), but I'm trying to get away from this thinking, because from what I've seen Java is indeed awesome.

    Java books just ain't answering this.

    First' lets take using JFrame, very handy. The book I have shows an example:
    ===
    import javax.swing.JFrame;

    class ShowAFrame {

    public static void main(String args[]){
    JFrame MyFrame = new JFrame();
    String myTitle = "Blank Frame";
    MyFrame.setTitle(myTitle);
    MyFrame.setSize(10,10);
    MyFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CL OSE);
    MyFrame.setVisible(true);
    }
    }

    ===

    Friggin great, this make a class ShowAFrame, instantiates(?) a new copy of the class JFrame just for my usage, this includes its variables and functions. As long as I put anything in the main function it will automatically get executed.

    On one hand, I want this code to execute automatically, but its quite re-usable, so I want to make it its' own class, right? Tell me if I'm right so far.

    Next, I understand calling a class function where your expecting to change a value a a particular value, but what if you just want a class like this to do something, what's the philosophy and method for doing so.

    Am I still going to be 'hanging around in main' waiting for something to happen? Or, does Java do it differently? Hoping I'm making my questions clear, I'm sure I'm 'not seeing the forest through the trees', but the books just talk about classes to death without really a big picture (I think) So:

    A) Should this code have its' own class
    B) How should I make happen, what code would I put in main, and what code would I add to the new class to call it?

    Thanks!
    Appel

  2. #2
    Wnt2bsleepin is offline Senior Member
    Join Date
    Feb 2012
    Posts
    219
    Rep Power
    3

    Default Re: Alright, Now I'm catching on, but stuck on something....

    It's quite cool seeing all these people coming from other languages, and learning Java. Then you have me who is learning OOP Java and plans on moving on. It really helps to give you perspective. If I could take a stab at what you want, you want to be able to reuse the code section that you posted. Making a new blank Frame. You can do this is you put it in a method, and just call it.

    Java Code:
    mport javax.swing.JFrame;
    
    class ShowAFrame {
    
    public static void main(String args[]){
     makeGUI(); 
    
    }
    
    public static void makeGUI()
    {
    JFrame MyFrame = new JFrame();
    String myTitle = "Blank Frame";
    MyFrame.setTitle(myTitle);
    MyFrame.setSize(10,10);
    MyFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CL OSE);
    MyFrame.setVisible(true); 
    }
    }
    That way you can call it as many times as you want. Also, you can use [ code][/ code] tags here in your post to make it easier to read. Just remove the spaces I put.

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

    Default Re: Alright, Now I'm catching on, but stuck on something....

    Lesson: Object-Oriented Programming Concepts (The Java™ Tutorials > Learning the Java Language)

    That code (also, sorry to say Wnt2bsleepin's advancement on it), is a bad sample on more than one account. Don't even try to understand this at this stage of learning Java, but I'm mentioning it for the sake of anyone who comes across this thread and thinks that code could be a starting point for designing a GUI: All Swing components should be constructed, accessed and mutated only on the EDT.

    Additionally, the code doesn't follow the Java coding conventions.
    Code Conventions for the Java Programming Language: Contents

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

  4. #4
    Wnt2bsleepin is offline Senior Member
    Join Date
    Feb 2012
    Posts
    219
    Rep Power
    3

    Default Re: Alright, Now I'm catching on, but stuck on something....

    Makes sense, although most tutorials I have seen present the basics of GUI programming like that. It's been a while though.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default Re: Alright, Now I'm catching on, but stuck on something....

    That's because they aren't the Oracle tutorials, where every example has something like the following:
    Java Code:
        public static void main(String[] args) {
            //Schedule a job for the event-dispatching thread:
            //creating and showing this application's GUI.
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGUI();
                }
            });
        }
    To the OP, if you're just learning this stuff I really wouldn't do Swing for a while.
    It's quite complex, and until you have grasped OO code structure you'll end up with spaghetti.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Catching TAB via KeyEvent?
    By Toll in forum Advanced Java
    Replies: 6
    Last Post: 07-06-2011, 03:19 AM
  2. Help with exception catching
    By omgonoes in forum New To Java
    Replies: 5
    Last Post: 05-31-2011, 10:38 AM
  3. Help with Catching Exceptions
    By javaman1 in forum New To Java
    Replies: 4
    Last Post: 09-11-2010, 02:06 AM
  4. After catching the exception thrown
    By scoobyrox in forum New To Java
    Replies: 2
    Last Post: 09-05-2010, 02:29 PM
  5. Replies: 0
    Last Post: 01-28-2010, 02:46 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
  •