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

     class calculator{
     public static void main(String args[])
     {
         JFrame frame=new JFrame("Simple Calculator");
         JPanel mainpanel=new JPanel();

         JPanel tfpanel=new JPanel();
         JTextField tf=new JTextField();
         tfpanel.add(tf);
         tfpanel.setSize(200,40);
         tfpanel.setLayout(new GridLayout(0,1));

This is the second panel which consits of 'Delete' Button and 'Clear' Button

         JPanel panel2=new JPanel();  
         JButton buttondel=new JButton("Delete");
         JButton buttonclear=new JButton("Clear");
         panel2.add(buttondel);
         panel2.add(buttonclear);
         panel2.setSize(200,60);
         panel2.setLayout(new GridLayout(0,2,10,10));
         panel2.setBorder(BorderFactory.createEmptyBorder(10,0,10,0));

         JPanel buttonpanel=new JPanel();

         JButton b1=new JButton("9");
         JButton b2=new JButton("8");
         JButton b3=new JButton("7");
         JButton b4=new JButton("/");
         JButton b5=new JButton("6");
         JButton b6=new JButton("5");
         JButton b7=new JButton("4");
         JButton b8=new JButton("*");
         JButton b9=new JButton("3");
         JButton b10=new JButton("2");
         JButton b11=new JButton("1");
         JButton b12=new JButton("-");
         JButton b13=new JButton("0");
         JButton b14=new JButton(".");
         JButton b15=new JButton("=");
         JButton b16=new JButton("+");

         buttonpanel.add(b1);
         buttonpanel.add(b2);
         buttonpanel.add(b3);
         buttonpanel.add(b4);
         buttonpanel.add(b5);
         buttonpanel.add(b6);
         buttonpanel.add(b7);
         buttonpanel.add(b8);
         buttonpanel.add(b9);
         buttonpanel.add(b10);
         buttonpanel.add(b11);
         buttonpanel.add(b12);
         buttonpanel.add(b13);
         buttonpanel.add(b14);
         buttonpanel.add(b15);
         buttonpanel.add(b16);
         buttonpanel.setSize(200,220);
         buttonpanel.setLayout(new GridLayout(0,4,10,10));


         mainpanel.add(tfpanel);
         mainpanel.add(panel2);
         mainpanel.add(buttonpanel);
         mainpanel.setLayout(new GridLayout(0,1));
         mainpanel.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
         frame.add(mainpanel);
         frame.setSize(300,300);
         frame.setVisible(true);
         frame.setResizable(false);
         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

     }
  }

In the above example, I have created the three panels, and I have assigned all of them different heights. But all the three panels are taking more than assigned heights.

How to assign the different heights to different panels ?

Zaid Khan
  • 357
  • 4
  • 14
  • 1
    Its probably a better approach to use your layout manager only, some managers wont respect the height or width of components. If you're looking for more control over a layout manager try [GridBagLayout](https://docs.oracle.com/javase/8/docs/api/java/awt/GridBagLayout.html), if one manager doesnt offer what you require, its likely that several nested ones will. – Chains Nov 17 '16 at 08:54
  • Provide ASCII art or a simple drawing of the *intended* layout of the GUI at minimum size, and if resizable, with more width and height. – Andrew Thompson Nov 17 '16 at 09:33
  • @Zaid Khan Perhaps you can post an image of or a link to an image showing us what is your desired output. – user3437460 Nov 17 '16 at 15:45

1 Answers1

0

Firstly: Instead of setSize() better use setPreferredSize() for your Panels

tfpanel.setPreferredSize(new Dimension(200,40));
panel2.setPreferredSize(new Dimension(200,60));
buttonpanel.setPreferredSize(new Dimension(200,120));

Secondly don't use GridLayout for your mainpanel because it makes all its components of same size. You could simply leave this line out

//mainpanel.setLayout(new GridLayout(0,1));
ArcticLord
  • 3,999
  • 3
  • 27
  • 47
  • 2
    1) *"Instead of `setSize()` better use `setPreferredSize()` for your Panels"* See [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?](http://stackoverflow.com/q/7229226/418556) (Yes.) 2) *"Secondly don't use `GridLayout` for your `mainpanel` because it makes all its components of same size."* No! It is controlled by the [`GridBagConstraints.fill`](http://docs.oracle.com/javase/8/docs/api/java/awt/GridBagConstraints.html#fill) attribute. – Andrew Thompson Nov 17 '16 at 09:32
  • @AndrewThompson Totaly agree. I just wanted to give the simplest solution to OP's problem. Explaining how to write good swing production code seems to me way too overkill for this kind of question. – ArcticLord Nov 17 '16 at 09:47
  • *"..the simplest solution.."* ..is wrong. There was factual inaccuracy in one part of the answer and sub-optimal advice in another part that will lead to more problems than it solves. After the OP provides the requirement of the GUI as outlined in my comment on the question, I will enter a robust solution based on layouts (probably `GridBagLayout`, but it will depend on the exact requirement) and allowing **all** components to be their natural size. If you feel like it, you can down vote my answer then. In the meantime, I am down voting this answer. – Andrew Thompson Nov 17 '16 at 10:30
  • Oh crap! My deepest apologies. I just realized that the question, **and** your answer, were referring to `GridLayout` as opposed to **`GridBagLayout`**. I still don't like the use of setting a preferred size, but most of the stuff I've claimed in comments on this answer is rubbish. My bad. :P – Andrew Thompson Nov 17 '16 at 10:34