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

  • SEARCH
  • Home
  • 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 7442827
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T11:14:07+00:00 2026-05-29T11:14:07+00:00

How to achieve the following output in Java like windows Calculator, Example, First Row

  • 0

How to achieve the following output in Java like windows Calculator,

Example,

First Row = Text Field
Second Row = 5 buttons
Third  Row = 5 Buttons

and the remaining Data in Center of border layout,

How to put first , second, third row in North of Border Layout together ? here is my code ,

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;

import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextField;

    public class GridLayoutFrame extends JFrame 
    {

        private JButton[] buttons; // Array of Buttons 1 to 10
        private JButton[] topButton; // 10 buttons that has to be placed under Text field 5 button in each row
        private JPanel Bottom; // A panel to show everything under first three Row
        private JPanel Top; // Top Panel that contains Text Field and 2nd and third Row
        private JTextField Input = new JTextField();

        private static final String[] names = 
          {"7", "8", "9", "4", "5", "6","1","2","3"};

        private static final String[] topButtons = 
              {"MC","MR","MS","M+","M-","<-","CE","C","+-","Root"};

       // no-argument constructor
       public GridLayoutFrame()
       {
          super ("Calculator");
          Bottom = new JPanel();  
          Top = new JPanel();
          Top.setLayout(new GridLayout(3,1,5,5));   // Three Rows (first row must only show a text field
          Bottom.setLayout(new GridLayout(5,5,10,10));
          topButton = new JButton[topButtons.length];
          buttons = new JButton[ names.length ];


          Input.setSize(500,500);
          Input.setEditable(false);
          Input.setBackground(Color.WHITE);
          Input.setPreferredSize(new Dimension(300, 30));

          for ( int count = 0; count < topButtons.length; count++ ) 
          {
             topButton[ count ] = new JButton( topButtons[count]);
             Top.add( topButton[ count ] ); // add button to panel
          }


          for ( int count = 0; count < names.length; count++ )

          {
             buttons[ count ] = new JButton( names[count]);
             Bottom.add( buttons[ count ] ); // add button to panel
          }

         add(Top,BorderLayout.NORTH);
         //add(Bottom,BorderLayout.CENTER);

       }
    } // end class GridLayoutFrame

The problem with the code is , the Text field is appearing in the same line where the top buttons are appearing.
please help

  • 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-29T11:14:08+00:00Added an answer on May 29, 2026 at 11:14 am

    It is best to use gridbaglayout for the requirements you have as it is very flexible.
    You can take a look at http://docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html

    I have modified your program to use GridBagLayout for the top panel. I think it does what you expect for the top panel.

    public GridLayoutFrame() {
            super("Calculator");
            Bottom = new JPanel();
            Top = new JPanel();
            Top.setLayout(new GridBagLayout()); // Three Rows (first row must
                                                        // only show a text field
            GridBagConstraints c = new GridBagConstraints();
            c.fill = GridBagConstraints.HORIZONTAL;
            c.weightx = 1;
    
    
            topButton = new JButton[topButtons.length];
    
            Input.setSize(500, 500);
            Input.setEditable(false);
            Input.setBackground(Color.WHITE);
            Input.setPreferredSize(new Dimension(300, 30));
    
            c.gridwidth = 5;
            Top.add(Input, c);
            c.gridwidth = 1;
    
    
            for (int count = 0; count < topButtons.length; count++) {
                topButton[count] = new JButton(topButtons[count]);
                c.gridx = count % 5;
                Top.add(topButton[count], c ); // add button to panel
            }
    
            Bottom.setLayout(new GridLayout(5, 5, 10, 10));
            buttons = new JButton[names.length];
            for (int count = 0; count < names.length; count++)
    
            {
                buttons[count] = new JButton(names[count]);
                Bottom.add(buttons[count]); // add button to panel
            }
    
            add(Top, BorderLayout.NORTH);
            add(Bottom,BorderLayout.CENTER);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to achieve the following: I want a free Application Lifecycle Management
Im trying to achieve the following effect in rails: If the text is bigger
The following code fails to compile ( Output is not of type java.reflect.annotation.Annotation ):
Admin: Please create image urls as images I'm trying to achieve the following output:
I was wondering if there is a way to achieve the following output in
Let's say, that I have the following source output: <p>This is first paragraph</p> <p>This
I'm trying to achieve following: class A { def foo() { foo } }
How can I achieve the following? I have two models (blogs and readers) and
I want to achieve the following: ID | Counter ------------ 0 | 343 1
How can I achieve the following with a format string: Do 01.01.2009 ? It

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.