Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Need help with my code

  1. #1
    Junior Member anonymous's Avatar
    Join Date
    Jan 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thumbs up Need help with my code

    this is my first post here ever <I cant believe i got anonymous as a user-name>
    IDE = Netbeans

    im trying to create a simple GUI.

    import javax.swing.JFrame;
    import javax.swing.JButton;
     
     
    public class Main {
     
            public static void main(String[] args) {
     
            JFrame frame = new JFrame("Commands."); /*Create a frame with the title "Title" */
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); /*This is so the window closes when user clicks on the the X*/
            frame.setSize(650, 650); /*Create a frame of 500x500*/
            frame.setVisible(true); /*Set the frame visible*/
            frame.add(new JButton("NAMEHERE")();
     
        }
     
    }
    i want the lettering on the jbutton to be smaller i tryed like
     frame.add(new JButton("NAMEHEREI"))(frame.setSize(120,125);
    But it dident work.
    Help me please?
    EDIT: Im fairly new to java.
    Picture of what hapend when i tryed to run it

    Instead of doing
     frame.setSize(650, 650); /*Create a frame of 500x500*/
    it did this
     frame.add(new JButton("NAMEHEREI"))(frame.setSize(120,125);
    Last edited by anonymous; January 1st, 2010 at 05:08 AM. Reason: Fixed


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Need help with my code

    A few things about your code. First, if you wish to change the size of the font in the JButton, you need to change the actual font of the JButton. Second, its better practice to add components to a JFrames content pane as opposed to the JFrame container. Both of these aspects are encorporated in the code snippet below:

    JButton button = new JButton("BUTTON");
    Font f = button.getFont();/*Be sure to import the java.awt package*/
    button.setFont(f.deriveFont(8.0f));
    frame.getContentPane().add(button);