In this part of the Java Video Tutorial series, I cover GridLayout, GridBagLayout, GridBagConstraints, Font, Insets and much more.
I do this by showing you how to create a calculator layout with Java Swing. You’ll see just how easy it is to layout a nice GUI interface using constraints. You don’t necessarily need to measure X and Y coordinates to get everything to look right.
The code follows the video and it is heavily commented.
If you like videos like this, share it [nttfblike]
Code from the Video
nice tutorial with nice information 🙂
Thank you. It was a fun one to make
probably this one was i always want….
just going to watch….
thank you…
nice video..
also by following your tutorial, i started to write code for a registration form, i added a label and a textbox but both are coming in center of my frame, can u help me in that, i want all should appear from top of my jframe like a registration form..
also let me know if anything i m doing wrong for a registration form..
here is my code
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import javax.swing.*;
public class Form extends JFrame{
private static final long serialVersionUID = 1L;
JLabel lbl1, lbl2, lbl3, lbl4, lbl5, lbl6,
lbl7, lbl8, lbl9, lbl0;
JTextField txtBox1;
int num1, num2;
public static void main(String[] args){
new Form();
}
public Form(){
// Create the frame, position it and handle closing it
this.setSize(400,400);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("Calculator");
JPanel thePanel = new JPanel();
thePanel.setLayout(new GridBagLayout());
GridBagConstraints gridConstraints = new GridBagConstraints();
gridConstraints.gridx = 0;
gridConstraints.gridy = 1;
gridConstraints.gridwidth = 1;
gridConstraints.gridheight = 1;
gridConstraints.weightx = 0;
gridConstraints.weighty = 0;
gridConstraints.insets = new Insets(5,5,5,5);
gridConstraints.anchor = GridBagConstraints.NORTH;
gridConstraints.fill = GridBagConstraints.BOTH;
lbl1 = new JLabel("Enter First Name");
gridConstraints.gridx = 1;
gridConstraints.gridy = 1;
txtBox1 = new JTextField("",20);
Font font = new Font("Helvetica", Font.PLAIN, 18);
txtBox1.setFont(font);
thePanel.add(lbl1,gridConstraints);
thePanel.add(txtBox1,gridConstraints);
this.add(thePanel);
this.setVisible(true);
}
}
thank you..
My next tutorial will show you how to create a more complicated Java Swing Layout.
okay..thanks..
Hello again, I watched ur vedio, it was nice but i did not get the last part with gridwidth and gridy and gridx, what is gridwidth ? and what does it do in my laytout? thanks
Thank you 🙂 gridwidth is used to define how many columns each component takes up on the grid. It changes depending on the size of the component. I hope that helps
Hi sir, nice tutorial.I have been watchin your java video tutorials from the beginnin, thanks. Please I need your help, I want add action listener to this code
JTextField writeDown = new JTextField(“0”, 12);
String[] theKeys = {“7”, “8”, “9”, “/”, “4”, “5”, “6”, “*”, “1”, “2”, “3”, “-“, “0”, “.”, “+”, “=”};
for (int i = 0; i < theKeys.length; i++) {
JButton but = new JButton(theKeys[i]);
}
Thank you 🙂 I cover action listeners here Java GUI Event Handling I hope it helps