4 Replies - 4384 Views - Last Post: 01 January 2013 - 03:45 PM Rate Topic: -----

#1 jpat27  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 01-January 13

Method to create JFrame

Posted 01 January 2013 - 03:37 PM

I want to create a method so that I can call it whenever I want to create a JFrame, and not have to type out all of the frame. stuff. Would this simplified code work?
import javax.swing.*;

class FrameMethod {
    public static void main(String args][]){
        new frame;
    }
    
    public static frame
    {
        JFrame frame = new JFrame("Frame");
        frame.setSize(500, 400);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setAlwaysOnTop(true);
        frame.setVisible(true);
    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: Method to create JFrame

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10662
  • View blog
  • Posts: 39,590
  • Joined: 27-December 08

Re: Method to create JFrame

Posted 01 January 2013 - 03:40 PM

No. Your code won't even compile. Make sure to test your code, and you can determine if it will work or not.
Was This Post Helpful? 0
  • +
  • -

#3 jpat27  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 01-January 13

Re: Method to create JFrame

Posted 01 January 2013 - 03:41 PM

Is there any other way to do what I originally proposed?
Was This Post Helpful? 0
  • +
  • -

#4 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Method to create JFrame

Posted 01 January 2013 - 03:42 PM

Did you even test that? What that frame is? a class or a method?
You can simply have a class inherited from a JFrame and then whenever you want to create a frame, then create an object of it. All those things can be done in its constructor which is called automatically when you create an object...
Was This Post Helpful? 2
  • +
  • -

#5 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10662
  • View blog
  • Posts: 39,590
  • Joined: 27-December 08

Re: Method to create JFrame

Posted 01 January 2013 - 03:45 PM

An inheritance model is another way of accomplishing modularizing a JFrame. I have a tutorial on Swing for Beginners.
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1