1
import java.awt.*;
import javax.swing.*;
public class guiAs {
    public static void main (String args [] )
    {
        JFrame frame = new JFrame ();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(new Dimension (300,300));
        frame.setTitle("Calculator");
                
        JPanel panel = new JPanel();
//        JLabel label = new JLabel();

        //creating num1
        JPanel panel2 = new JPanel();
        JLabel lable1 = new JLabel("Number 1 ");
        JTextField tf1 = new JTextField();
        
        //creating num2
        JLabel lable2 = new JLabel("Number 2 ");
        JTextField tf2 = new JTextField();
        
        //creating result
        JPanel panel3 = new JPanel();
        JLabel lable3 = new JLabel("Result: ");
        JTextField tf3 = new JTextField(10);
        
        //creating button
        JButton Add= new JButton("Add");
        JButton Subtract = new JButton("Subtract");
        JButton Multiply = new JButton("Multiply");
        JButton Division = new JButton("Division");
        
        //creating num1
        panel2.add(lable1);
        panel2.add(tf1);
        
        //creating num2
        panel2.add(lable2);
        panel2.add(tf2);
        
        //creating result
        panel3.add(lable3);
        panel3.add(tf3);
        
        //creating buttons
        panel.add(Add);
        panel.add(Subtract);
        panel.add(Multiply);
        panel.add(Division);
        
//        frame.getContentPane().add(BorderLayout.WEST, panel3);

        //creating Box Layout for num1 and num2
        BoxLayout layout2 = new BoxLayout(panel2, BoxLayout.Y_AXIS);
        panel2.setLayout(layout2); 
        frame.setLayout(new FlowLayout());
        frame.add(panel2);        

        //creating Box Layout for buttons
        BoxLayout layout1 = new BoxLayout(panel, BoxLayout.Y_AXIS);
        panel.setLayout(layout1); 
         frame.setLayout(new FlowLayout());
        frame.add(panel);
        
    
//        Add.setAlignmentX(Component.RIGHT_ALIGNMENT);
//        panel.add(Add);
               
      
//            frame.getContentPane().add(BorderLayout.SOUTH, panel);
            //creating Border Layout for num1 and num2
            frame.getContentPane().add(BorderLayout.WEST, panel2);
            
            //creating Border Layout for Buttons
            frame.getContentPane().add(BorderLayout.EAST, panel);
            
            //creating Box Layout for Result
            frame.getContentPane().add(BorderLayout.SOUTH, panel3);

            frame.setVisible(true);
        

    }
    
}

this is my code, and I have no idea to make the text field next to the numbers. i try several times but does not comes together. whenever i add something the panels moves its work only with the result but for the number doesn't work here i used Box Layout for buttons and number 1 and number 2:

https://i.stack.imgur.com/DRKbT.png

camickr
  • 321,443
  • 19
  • 166
  • 288
Zahra Alameri
  • 11
  • 1
  • 3
  • Read the Swing tutorial on [Layout Managers](https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html) for the basics of each layout manager. You are correct in your approach to use panels with different layout managers. You set the frame layout as follows: `frame.setLayout(new FlowLayout());`, but later use: `frame.getContentPane().add(BorderLayout.WEST, panel2);`. That won't work since you can't use BorderLayout constraints on a FlowLayout. Also, when using a BorderLayout the code should be: `frame.getContentPane().add(panel2, BorderLayout.LINE_START);`. Read the BorderLayout demo. – camickr Jun 20 '21 at 14:18

1 Answers1

0

You were very close to solving it.

Replace this line

JPanel panel2 = new JPanel();

With this line

JPanel panel2 = new JPanel(new GridLayout(2, 2));

Then, get rid of these 3 lines

BoxLayout layout2 = new BoxLayout(panel2, BoxLayout.Y_AXIS);
panel2.setLayout(layout2); 
frame.setLayout(new FlowLayout());

Now your code looks like this

NEW GUI IMAGE

davidalayachew
  • 1,279
  • 1
  • 11
  • 22