How Do I Make A JFrame


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 7 of 7

Thread: How Do I Make A JFrame

  1. #1
    Join Date
    May 2004
    Posts
    2

    How Do I Make A JFrame

    Hi, Im having a little trouble building a JFrame....I Just started learning java and im having a lot of trouble figuring out what it all means.....

    Can someone please show me how to make a JFrame layout with just 2 buttons on the frame.
    Thanks in Advance

    Joe

  2. #2
    Join Date
    Mar 2004
    Posts
    635
    extend JFrame class.

    class MyFrame extends JFrame


    add your button objects to the container object(MyFrame) from within the constructor.

    //create the button object
    JButton button = new JButton("Button 1");


    //add it to the container(unsure if i typed this correctly)
    getContentPane().add(button);

  3. #3
    Join Date
    May 2004
    Posts
    2

    here is some simple code with comments

    I didn't run this code just typed it but I don't see any errors off hand.

    //need to import this for jFrame
    import javax.swing.*;

    public class test extends JFrame{
    public test() {
    //sets the title of the jFrame by calling the jFrame constructor
    super("My First JFrame");
    //sets the size of your jFrame
    setSize(400,300);
    //makes the jFrame Visible
    show();
    }
    public static void main(String args[]){
    //creates a new instance of your class
    test t = new test();
    //causes the process to terminate when you click the x
    t.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    }

  4. #4
    Join Date
    Feb 2004
    Posts
    34
    You have to initialize the GUI in the JFrame by having your constructor method declare all the visual parts of your JFrame.

    public thing()
    {
    setSize(400 , 400);
    setVisible(true);
    }

    Then , later on , you have to run the program and create an instance of your JFrame.

    public static void main (String args[])
    {
    thing application = new thing();
    }

    then when you run the program , the JFrame should come up.
    www.saterasoft.cjb.net

    Come and play the games.....

  5. #5
    Join Date
    Feb 2004
    Posts
    808

    Re: How Do I Make A JFrame

    Originally posted by BigJoe
    Hi, Im having a little trouble building a JFrame....I Just started learning java and im having a lot of trouble figuring out what it all means.....

    Can someone please show me how to make a JFrame layout with just 2 buttons on the frame.
    Thanks in Advance

    Joe
    few corrections here:

    if you "jkust started java" then the last thing you should be thinking about is making a GUI. i know that it feels very constructive and a monumentous achievement if you make a window with buttons and stuff, but you will miss the point of proper programming this way. if you want to program like that, VB would be a better language for you to learn, because it literally is a case of "draw a GUI in something similar to a sophisticated Microsoft Paint" then "add fragments of code onto the GUI to make things happen when you click buttons"

    java programs arent built that way.. :/

    -

    care with how you use the term "layout" .. a layout to us means a Layout Manager, some clever thing that resizes and controls component appearance for you.. JFrame is not a Layout Manager, so using the term "JFrame layout" will confuse some java programmers.. "JFrame appearance" would be a better choice of words...

    -

    now.. how do you want your buttons.. horizontal, vertical or diagonally aligned?
    The 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, www.dictionary.com et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

  6. #6
    Join Date
    May 2004
    Posts
    2
    i want to make a JFrame that has 2 buttons that allows u to input hex(button1) and decimal(button2) so that it can be converted

    so what im really trying to make is a little program that allows me to convert from hex to decimal numbers and from decimal to hex

    thanks for all the help so far guys

  7. #7
    Join Date
    Feb 2004
    Posts
    808

    Re: here is some simple code with comments

    Originally posted by general4172
    //need to import this for jFrame
    import javax.swing.*;
    importing javax.swing.JFrame would be sufficient

    do try and use the UBB [code] and [/code] tags to wrap any code you write.. it makes it readable

    public class test extends JFrame{
    if youre going to provide example code, do please make an effort to follow the Java Language Specification's recommendations that:

    Class Names Start With A Capital Letter

    public static void main(String args[]){
    it's "String[] args"

    i know that the difference is trivial and that the compiler doesnt mind.. but [] is a type identifier and hence belongs with the TYPE String, not the name "args"

    After all, you have an array of Exceptions, perhaps.. and you know they are of type SQLExcetion.. how would you cast an array from Object type to SQLException type? like this:

    SQLException[] se_ary = (SQLException[])object_ary;

    having your [] flitting about all over the place makes code harder to read because of it's inconsistency.. aside from a declaration, you cannot use [] next to a variable name.. even in something like this, you must use a full array index:

    Code:
    int[][] the2D = new int[10][10];
    
    for(int x = 0; x<the2D.length; x++){
      for(nt y = 0; y<the2D[].length; y++){
        //do stuff
      }
    }
    the red part is wrong.. an array index is required.. hence, try to avoid putting [] next to variable names.. always put it next to the type instead
    The 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, www.dictionary.com et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center