Grid Layout Java

I have created a java program that shows a grid of buttons and labels and i want to know how to pit a constraint on the labels and buttons so they dont re-size when window is dragged out.. can anyone help please?

this is my gridlayout class

package JFrameTester;

import java.awt.Dimension;
import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class layout extends JFrame {

    JButton button1,button2,button3;
    JLabel label1,Label2,Label3;

    public layout (){

    setLayout(new GridLayout(2, 2));

    button1 =  new JButton ("button1");


    label1 =  new JLabel ("label1");

    button2 =  new JButton ("button1");

    Label2 =  new JLabel ("Label2");

    button3 =  new JButton ("button1");

    Label3 =  new JLabel ("Label3");



and this is my main class

public class JFrameTester {

 * @param args
public static void main(String[] args) {

    layout Lay = new layout();

    // JFrame frame = new JFrame("BUY COMPUTER");

    Lay.setSize(800, 600);
   //  frame.setVisible(true);
     Lay.addWindowListener(new MyWindowListener());


Place the labels and buttons inside a JPanel, and then add that JPanel to your GridLayout. GridLayout automatically resizes components to fit the size of the cell they are placed in and when a JFrame is resized it will adjust the components size as needed. If you place them in JPanels only the JPanel in that cell will be resized, not the components.


In addition to that it's Java practice to name all classes with an uppercase letter, your layout should be Layout. However, the name Layout does not accurately convey the purpose of class Layout as your class is not a layout, it's a JFrame. Perhaps LayoutFrame would be a better class name.

And (this is default Java practice) variables should be camelcase, beginning with a lowercase letter and subsequent words having a capital letter e.g. thisIsAVariable. Alternatively you could use underscores which is not normal Java practice, this_is_a_varaible. Given that in Java classes generally start with a capital letter and variables start with a lowercase letter the statement layout Lay = new layout(); looks very strange and a bit confusing at first glance.

Need Your Help

Double.GetHashCode algorithm or override

c# algorithm double gethashcode

i have an application project that both managed and unmanaged code runs and i need to use the same algorithm for hashing double values in both systems. so either i will override

PHP Calculate starttime and end time from a list of mintues with ID's

php sql datetime loops

Sorry to bother again. I have searched tried my best, looked everywhere and still cannot solve the problem. Could you please help? - I would try my best to explain the problem.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.