Java Tutorial 12: Arranging Component on Frame / Panel in Java

///Java Tutorial 12: Arranging Component on Frame / Panel in Java

Java Tutorial 12: Arranging Component on Frame / Panel in Java

FavoriteLoadingAdd to favorites

In this tutorial, I will teach you how arrange buttons on Java frame using Grid bag Layout in Java.

Java468x60

Please Rate , Comment and Subscribe.

source

By |2020-04-21T03:03:12+00:00April 21st, 2020|Java Video Tutorials|26 Comments

26 Comments

  1. Chibble Dibble April 21, 2020 at 3:03 am - Reply

    Seriously? Am I the only one getting an โ€œUnresolved compilation problem?โ€

  2. MJ Castillo Vlogs April 21, 2020 at 3:03 am - Reply

    Thank you, what I just did when I want to place a components to frame I just used "setBounds", but thank you I gain more idea on how to arrange a components in JFrame. May God bless you more!

  3. arun ramanathan April 21, 2020 at 3:03 am - Reply

    Thanks a ton…your explanation skills are amazing!!!

  4. pritam das April 21, 2020 at 3:03 am - Reply

    how to arrange compoments from upper left corner

  5. habtamu amsalu April 21, 2020 at 3:03 am - Reply

    AWESOME VIDEO continue

  6. Siddharth Jain April 21, 2020 at 3:03 am - Reply

    Thanks Naveed. Was trying to align all components in jpanel in center from many days and your this tutorial helped me in aligning and i finished the same.. looking for your more vids for help.. Thanks Gr8 help ๐Ÿ™‚

  7. Muhammad Mubashar Hussain April 21, 2020 at 3:03 am - Reply

    Best โค

  8. Ian Owens April 21, 2020 at 3:03 am - Reply

    I know it's off topic, but I'm trying to make my JButton have an image. I don't know why it wont work.
    import java.awt.FlowLayout;

    import java.awt.GridBagConstraints;

    import java.awt.GridBagLayout;

    import java.awt.event.ActionEvent;

    import java.awt.event.ActionListener;

    import java.util.Random;

    import javax.swing.ImageIcon;

    import javax.swing.JButton;

    import javax.swing.JFrame;

    import javax.swing.JLabel;

    import javax.swing.JPanel;

    public class Main {

    public static void main(String[] args) {

    JFrame frame = new JFrame("Piggy Tools");

    frame.setVisible(true);

    frame.setSize(1000,600);

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel panel = new JPanel(new GridBagLayout());

    JLabel output = new JLabel("");

    JButton randomNumber = new JButton("Click For Random Number!!");

    randomNumber.addActionListener(new ActionListener() {

    public void actionPerformed(ActionEvent e) {

    Random theNumber = new Random();

    for (int a = 0; a < 1; a++){

    int b = theNumber.nextInt(50);

    output.setText("" + b);

    }

    }

    });

    JButton exit = new JButton("Exit");

    exit.addActionListener(new ActionListener() {

    public void actionPerformed(ActionEvent e) {

    frame.setVisible(false);

    }

    });

    GridBagConstraints layout = new GridBagConstraints();

    //organize stuff here

    JLabel background=new JLabel(new ImageIcon("C:\Users\(IHaveMyNameHereButThatIsNotYourBuisnes)\Documents\Superblock Studio(Not a real studio)\Piggy Tools\meta\Textures"));

    add(background);

    background.setLayout(new FlowLayout());

    background.add(exit);

    background.setLayout(new FlowLayout());

    layout.gridx = 1;

    layout.gridy = 1;

    panel.add(exit,layout);

    layout.gridx = 2;

    layout.gridy = 1;

    panel.add(randomNumber,layout);

    layout.gridx = 2;

    layout.gridy = 0;

    panel.add(output,layout);

    frame.add(panel);

    }

    private static void add(JLabel background) {

    }

    }

  9. Danielius Z April 21, 2020 at 3:03 am - Reply

    old…

  10. Shucayb Bashir Dirie April 21, 2020 at 3:03 am - Reply

    thanks teacher

  11. VIBHU JAKHRA April 21, 2020 at 3:03 am - Reply

    this is not working why??

  12. marsmoy negessa April 21, 2020 at 3:03 am - Reply

    thnks sir best tiotorial

  13. R MERLANG April 21, 2020 at 3:03 am - Reply

    The best Tutorial i've ever seen , thank you ๐Ÿ˜€ …

  14. xRiccardo April 21, 2020 at 3:03 am - Reply

    I'm a completely beginner at programming, but i have a thing to say. If i make the gridBagConstraints instance a class variable, and i create a method like this : public void setgrid(int gx, int gy){c.gridx = gx; c.gridy = gy;} the code is going to be less repetitive.
    Please answer if i did an error.

  15. Vandana Nayak Nayak April 21, 2020 at 3:03 am - Reply

    thank you sir.. it was really helpful

  16. Conlan Cason April 21, 2020 at 3:03 am - Reply

    I like your videos a lot. They are extremely helpful to my programming journey. However, I must inform you that asterisk is pronounced AH-sterisk, not uh-STER-risk. I don't mean to be rude, but just for the future that is how you pronounce it (at least in America; if it is different elsewhere, please disregard this comment).

  17. ArtsyDionne April 21, 2020 at 3:03 am - Reply

    thank you for having this tutorial … it really helps a lot ๐Ÿ™‚

  18. Francis Friday April 21, 2020 at 3:03 am - Reply

    cool

  19. Prince Bautista April 21, 2020 at 3:03 am - Reply

    I followed everything to the letter but my buttons don't move. I've tried NORTH, EAST, WEST, SOUTH but it's still in the middle. Can someone help?

  20. Sarfaraz Ali April 21, 2020 at 3:03 am - Reply

    osm sir

  21. ashiinsane90 April 21, 2020 at 3:03 am - Reply

    what about the size of the buttons? also this doesnt make it so you can adjust where ever you want it to be. and you have button1 in the bottom, so how do we fix this?

  22. Advaith H L April 21, 2020 at 3:03 am - Reply

    Legend says he broke the keyboard at 2:01
    WTF!?

  23. pbd0210 April 21, 2020 at 3:03 am - Reply

    How come when I removed the second button and set x coordinates to 1 and y coordinates to 0 it just stayed at 0 0 and never moved

  24. Sumaiya Tasnim April 21, 2020 at 3:03 am - Reply

    You are so good. You literally saved my finals. Explained very well.

  25. hulksuperstar April 21, 2020 at 3:03 am - Reply

    SUBBED! simply amazing!

  26. turntabletyger April 21, 2020 at 3:03 am - Reply

    The gridx = 0; and the gridy = 0; gives me the error cannot find symbol. Any idea what's wrong?

    import javax.swing.*;
    import java.awt.*;

    public class GUILesson3 {

    public static void main(String[] args) {

    JFrame f = new JFrame ();

    f.setVisible(true);
    f.setSize (600, 400);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel p = new JPanel(new GridBagLayout());
    JButton b1 = new JButton("Button 1");
    JButton b2 = new JButton("Button 2");

    GridBagLayout c = new GridBagLayout(); // aranges buttons//

    c.gridx = 0;
    c.gridy = 1;

    p.add(b1,c);

    c.gridx = 0;
    c.gridy = 1;

    p.add(b2,c);

    f.add(p);

    }
    }

Leave A Comment

*