Results 1 to 18 of 18
  1. #1
    netangel999 is offline Member
    Join Date
    Jul 2013
    Posts
    6
    Rep Power
    0

    Default please assist me for adding a jbutton to a jframe by looking at my code:

    This is my code. I am a beginner. I have done java but not all of it.
    I know the basics such as what is a string, variables, classes and objects types.

    I am using elcipse juno.

    I have created a new java class inside a project named: myframe1
    Java Code:
    import javax.swing.JFrame; // this is the library declaration for the jframe
    import javax.swing.Jbutton; // this is the library declaration for the jbutton
    
    // I can do something like this: import javax.swing.* 
    // by an astrick (*) i mean include everything that the SWING library implements and includes all the jframs and buttons instead of importing them seperately.
    
    
    
    class ShowAFrame {
    
    	public static void main (String args [] )
    	{
    		JFrame myFrame = new JFrame();
    Jbutton mybutton = new Jbutton();
    Jbutton.setsize(300,200);
    
    		String myTitle = "Myblackframewithjbutton";
    		myFrame.setTitle(myTitle);
    		myFrame.setSize(900,400); // sets the size of the jframe.
    		myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		myFrame.setVisible(true);
    	}
    }
    // NOW PLEASE CORREC ME IF IM WRONG. Ill compile it and if the debugger generates any errors please let me know how to fix them as i will show you the code here.
    netangel999.
    Last edited by JosAH; 07-16-2013 at 03:19 PM. Reason: fixed the [code] ... [/code] tags

  2. #2
    Trilok is offline Member
    Join Date
    Jul 2013
    Posts
    1
    Rep Power
    0

    Default Re: please assist me for adding a jbutton to a jframe by looking at my code:

    you can add JButton by callong add() method of component class like :

    myFrame.add(myButton);

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: please assist me for adding a jbutton to a jframe by looking at my code:

    Your comments are wrong. Java doesn't have anything called a 'library declaration' for a JFrame or JButton, and using an asterisk doesn't import every class in a package. Rather, the classes required are imported. This is known as import on demand, see the JLS, Chapter*7.*Packages

    You need to go through the Tutorial Trail: Creating a GUI With JFC/Swing (The Java™ Tutorials) and learn about Swing GUIs and layout managers.

    Also, go through the Guide For New Members and BB Code List - Java Programming Forum - Learn Java Programming and edit your post accordingly.

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

  4. #4
    netangel999 is offline Member
    Join Date
    Jul 2013
    Posts
    6
    Rep Power
    0

    Default Re: please assist me for adding a jbutton to a jframe by looking at my code:

    Thanks for the reply, DArrybrulk


    @ TRILOK. thanks for the reply. i have tried that but it still doesnt work, ill post another question soon if i dont understand.

  5. #5
    netangel999 is offline Member
    Join Date
    Jul 2013
    Posts
    6
    Rep Power
    0

    Default Re: please assist me for adding a jbutton to a jframe by looking at my code:

    Ok, Can somebody please put the code here for java frame and at least one button on the form.

    I have tried above answeres but cant get it right.
    Ty> :)

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: please assist me for adding a jbutton to a jframe by looking at my code:

    Quote Originally Posted by netangel999 View Post
    Ok, Can somebody please put the code here
    There are plenty of code samples in the Swing tutorial for which I gave you a link. Why do you need someone here to post still more code?

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

  7. #7
    netangel999 is offline Member
    Join Date
    Jul 2013
    Posts
    6
    Rep Power
    0

    Cool I need help with my JFrame and JButton code in JAVA using netbeans IDE.

    For some reason this code doesnt compile propely:

    :::::::::::::::::::::::::::::::::::::::::::::::::: ::::::

    import javax.swing.*;
    class ShowAFrame {
    public static void main (String args[]) {
    JFrame myFrame = new JFrame();
    String myTitle = "Blank Frame";
    myFrame.setTitle(myTitle);
    myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CL OSE);
    myFrame.setSize(400,500);
    myFrame.add(new JButton ("Please click me now!!!"));
    }
    }
    :::::::::::::::::::::::::::::::::::::::::::::::::: :::::::::::::::::

  8. #8
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,354
    Rep Power
    6

    Default Re: I need help with my JFrame and JButton code in JAVA using netbeans IDE.

    No please, don't post the compile error you get!

    The only thing that doesn't work when I compile it is because there is a space in EXIT_ON_CL OSE, when I fix that it compiles just fine. I don't see how you wouldn't figure that out yourself though, the compiler tells you exactly where the problem is.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  9. #9
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: I need help with my JFrame and JButton code in JAVA using netbeans IDE.

    Quote Originally Posted by gimbal2 View Post
    The only thing that doesn't work when I compile it is because there is a space in EXIT_ON_CL OSE
    That's more than likely an artifact of the forum software.

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

  10. #10
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: please assist me for adding a jbutton to a jframe by looking at my code:

    netangel999, in addition to the links I posted at #3 which you have rather obviously not bothered to go through, please go through the Forum Rules, with special reference to the second paragraph.

    I've merged two threads here.

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

  11. #11
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,354
    Rep Power
    6

    Default Re: I need help with my JFrame and JButton code in JAVA using netbeans IDE.

    Quote Originally Posted by DarrylBurke View Post
    That's more than likely an artifact of the forum software.

    db
    Hm good point. The only other thing I see is that the class is not public.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  12. #12
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: I need help with my JFrame and JButton code in JAVA using netbeans IDE.

    Quote Originally Posted by gimbal2 View Post
    Hm good point. The only other thing I see is that the class is not public.
    You mean you didn't see this?
    Java Code:
    :::::::::::::::::::::::::::::::::::::::::::::::::: ::::::
    Of course, it's hiding in plain sight.

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

  13. #13
    netangel999 is offline Member
    Join Date
    Jul 2013
    Posts
    6
    Rep Power
    0

    Default Re: I need help with my JFrame and JButton code in JAVA using netbeans IDE.

    @ GIMBAL2, listen, the code for: EXIT_ON_CL OSE -- There is no space in my first space. THAT SPACE IS YOUR MISTAKE NOT MY'N. i dont put any spaces there.
    I have looked at the links but even when i copy and past the code in netbeans IDE. It doesnt work.
    This is the error:

    :::::
    run:
    Error: Could not find or load main class java.windows.api.JavaWindowsAPI
    Java Result: 1
    BUILD SUCCESSFUL (total time: 0 seconds)
    :::::::::::::::::::::::::::::::::::::::::::::::::: ::


    I PRESS THE GREEN PLAY BUTTON ABOVE. Now for some reason no frame shows up.

    Do what im doing: Create a project and a class. Then just copy and past my code. Should i send you screen shots of the IDE via print screen?

  14. #14
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,354
    Rep Power
    6

    Default Re: please assist me for adding a jbutton to a jframe by looking at my code:

    Yeah you were talking about compilation not working, but it is in fact compiling just fine, it is not RUNNING. That is totally YOUR mistake, you are pretty bad at making it possible to help you.

    But when I add some code to actually show the JFrame it runs just fine, so it must be something with your Netbeans project that is fudged up. I'm not going to debug your tool problems for you.
    Last edited by gimbal2; 07-24-2013 at 01:47 PM.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  15. #15
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    268
    Rep Power
    2

    Default Re: I need help with my JFrame and JButton code in JAVA using netbeans IDE.

    Quote Originally Posted by netangel999 View Post
    For some reason this code doesnt compile propely:

    :::::::::::::::::::::::::::::::::::::::::::::::::: ::::::

    import javax.swing.*;
    class ShowAFrame {
    public static void main (String args[]) {
    JFrame myFrame = new JFrame();
    String myTitle = "Blank Frame";
    myFrame.setTitle(myTitle);
    myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CL OSE);
    myFrame.setSize(400,500);
    myFrame.add(new JButton ("Please click me now!!!"));
    }
    }
    :::::::::::::::::::::::::::::::::::::::::::::::::: :::::::::::::::::
    The frame is not visible I think, add myFrame.setVisible(true)

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

    Default Re: I need help with my JFrame and JButton code in JAVA using netbeans IDE.

    Quote Originally Posted by netangel999 View Post
    THAT SPACE IS YOUR MISTAKE NOT MY'N.
    To be fair, it was (indirectly) your mistake as you didn't put your code in code tags.
    Something Jos has done for you in your original post.

    Without them the forum has a tendency to stick spaces in long strings so they don't cause a post to go running off the edge of the page.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  17. #17
    netangel999 is offline Member
    Join Date
    Jul 2013
    Posts
    6
    Rep Power
    0

    Default Re: please assist me for adding a jbutton to a jframe by looking at my code:

    OK GOT IT. I PUT : public static void main (String args []) instead of public static void main (String [] args) { the" perenthisis has to go before the ARGS word. Somehow in my Java for dummies 2nd edition book it says that the perenthisis goes after the "String args []"" <<< like that. dONT KNOW WHY.

    :::::::::::::::::::::::::::::::::::::::::::::::::: ::::
    import javax.swing.*; // The start is i am importing everything.
    class Showaframe {
    public static void main (String [] args) {
    JFrame myFrame = new JFrame();
    String myTitle = "UltrBOX";
    myFrame.setTitle(myTitle);
    myFrame.setSize(500,500);
    myFrame.setVisible(true);
    myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CL OSE);
    myFrame.setLocation(500,400);
    myFrame.add(new JButton("Click me"));

    }
    }
    :::::::::::::::::::::::::::::::::::::::::::::::::: :::::::::::::::::::;
    tHIS WORKD for me. Can you now show me how to add more then one JButton to the form. I did this but The first jbutton comes up. Also show me how to set the location for Jbutton. Dont tell me how to handel an event for one right now.


    myFrame.add(new JButton("hello");
    myFrame.add(new JButton("lol");
    myFrame.add(new JButton("niced"); - SAY that i put this in my code, this will not work

  18. #18
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: please assist me for adding a jbutton to a jframe by looking at my code:

    This is a final notice. Go through the links I posted at #3 and learn how to post code on this forum.

    Just one more post with unformatted code and you will be banned for a period to give you time to read responses and follow links.

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

Similar Threads

  1. Replies: 3
    Last Post: 11-27-2011, 06:18 AM
  2. Assist a Beginner's Breakout Code
    By jjs in forum New To Java
    Replies: 1
    Last Post: 03-23-2011, 02:15 AM
  3. Adding JButton to JFrame with background
    By bzknight in forum AWT / Swing
    Replies: 1
    Last Post: 01-19-2011, 07:55 PM
  4. Adding JButton to a JTable
    By ting.at.net@hotmail.com in forum AWT / Swing
    Replies: 6
    Last Post: 05-26-2009, 04:37 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
  •