Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6874427
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:11:36+00:00 2026-05-27T04:11:36+00:00

I have created a java program that shows a grid of buttons and labels

  • 0

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");

    add(button1);

    label1 =  new JLabel ("label1");
    add(label1);

    button2 =  new JButton ("button1");
    add(button2);

    Label2 =  new JLabel ("Label2");
    add(Label2);

    button3 =  new JButton ("button1");
    add(button3);

    Label3 =  new JLabel ("Label3");
    add(Label3);

        }


}

and this is my main class

public class JFrameTester {


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


    layout Lay = new layout();
     Lay.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     Lay.setVisible(true);


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

    Lay.setSize(800, 600);
        //frame.pack();
   //  frame.setVisible(true);
     Lay.addWindowListener(new MyWindowListener());
 }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T04:11:36+00:00Added an answer on May 27, 2026 at 4:11 am

    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.

    EDIT

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a java application for Debian Linux. Now I want that that
I have created Excel Sheet using Java program. It works fine. My problem is,
I have created a simple web-service using Java. i want to load jars related
On Mac OSX 5.8 I have a Java program that runs at 100% CPU
I have created an executable Java .jar file. That is to say, I properly
I have a Java program which needs to be a startup program that runs
I created a really simple Java program that creates a java.net.ServerSocket on an open
I have created a java application and packed it into a jar file on
I have created a java server, which takes screenshots, resizes them, and sends them
Hi I have created a java application. I found there is an application menu

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.