I am trying to make a calculator
in java
. I am new to Layout Managers
. I created a JFrame
object with GridBagLayout
. I created 2 JPanel
's, one for the numbers and the other for the operations. Both the JPanel
use GridLayout
.
This is how they are arranged in JFrame
.
window=new JFrame("Calculator");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setLayout(new GridBagLayout());
//Creating window
GridBagConstraints gbc=new GridBagConstraints();
btns=new JPanel(); //JPanel for numbers
operations=new JPanel(); //JPanel for operations
numdisp=new JTextField(15); //JTextFIeld which displays the number
gbc.gridx=1;
gbc.gridy=1;
window.add(numdisp,gbc);
addbtn(); //method which defines all the variables
gbc.gridx=1;
gbc.gridy=2;
window.add(btns,gbc);
gbc.gridx=2;
gbc.gridy=2;
window.add(operations,gbc);
btns.setLayout(new GridLayout(4,3,0,0)); //Layout for both JPanel's
operations.setLayout(new GridLayout(7,1,1,1));
window.pack();
window.setVisible(true);
But this is how the result looks like...
I don't understand why there is a large gap between my number
panel and number display
. Why is the problem occurring? How could it be resolved?