Results 1 to 5 of 5
  1. #1
    dane1193 is offline Member
    Join Date
    Feb 2011
    Posts
    2
    Rep Power
    0

    Default Buttons in JFrames

    I am making a game for my computer science class and cannot figure out how to place buttons in the JFrame where I want using dimensions. For instance, I do now want to use the gridbag or spring layouts. I want to be able to individually place all my buttons and labels excacly where I want them. Here is a sample code that does not place them where I want.
    I imported a lot of stuff because I didn't know what I needed.


    import java.awt.*;
    import java.awt.event.*;
    import sun.audio.*;
    import javax.swing.*;
    import java.io.*;
    import java.util.Random;
    import java.awt.Dimension;
    import java.awt.Rectangle;

    public class d
    {

    public static void main(String[] args)
    {
    JButton button1= new JButton("Start!");
    JButton button2= new JButton("Score!");
    JFrame frame= new JFrame();
    frame.setSize(1200,800);


    button1.setSize(200,200);
    button1.setLocation(300,300);
    button2.setSize(600,600);
    button2.setLocation(100,100);

    frame.add(button1);
    frame.add(button2);
    frame.show(true);

    }

    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by dane1193 View Post
    I am making a game for my computer science class and cannot figure out how to place buttons in the JFrame where I want using dimensions. For instance, I do now want to use the gridbag or spring layouts. I want to be able to individually place all my buttons and labels excacly where I want them. Here is a sample code that does not place them where I want.
    I imported a lot of stuff because I didn't know what I needed.
    I agree, avoid Spring and GridBagLayout if at all possible, but don't avoid using layouts in general. I suggest that you read the Swing tutorial section called Laying out Components in a Container, where you'll learn about all the easier to use layouts such as BoxLayout, BorderLayout, GridLayout, and FlowLayout. You'll find that it is far easier to nest JPanels each using a simple layout to get your JButtons and components just where you want to place them. You'll also learn about using a "null" layout if you want to do absolute positioning -- but even though this can be done, I don't recommend doing it in general.

    I suggest you start here: Laying Out Components in a Container

  3. #3
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    I have a question for you fubarable, I am fairly new and was wondering if you are discouraging the use of grid bag because it is more difficult, or is it just not considered good to use? Sorry to ask this in someone else's post.

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,779
    Rep Power
    7

    Default

    I believe he is discouraging the use of GridBag due to it being difficult to learn/use, especially for a newcomer. For more experienced users then Gridbag becomes very powerful and they could probably whip up a great looking GUI in fewer lines than a n00b using other layout managers.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Junky View Post
    I believe he is discouraging the use of GridBag due to it being difficult to learn/use, especially for a newcomer. For more experienced users then Gridbag becomes very powerful and they could probably whip up a great looking GUI in fewer lines than a n00b using other layout managers.
    Yep. Also, there are some good substitutes for GridBagLayout such as MIgLayout, though you'll have to download this.

Similar Threads

  1. need help with jframes
    By glomic in forum New To Java
    Replies: 13
    Last Post: 01-08-2011, 11:16 AM
  2. help with jframes
    By pao09 in forum AWT / Swing
    Replies: 0
    Last Post: 04-15-2009, 02:10 PM
  3. jframes
    By ddj in forum AWT / Swing
    Replies: 0
    Last Post: 03-24-2009, 03:15 PM
  4. little help for Jframes,buttons and quicktime
    By ratb0y in forum NetBeans
    Replies: 0
    Last Post: 02-11-2009, 02:42 PM
  5. two JFrames
    By kirtesh4u in forum New To Java
    Replies: 0
    Last Post: 11-17-2008, 08:26 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •