0

I am trying to create a simple calculator. I'm about to finish its UI but I have a problem in the use of my layouts. I'm trying to find a way in appearing the panel3 just below the panel2, instead of left, but I can't. (This is for academic use)

So, this is the class MyFrame.

package lab5;
import java.awt.*;
    import javax.swing.*;

public class MyFrame extends JFrame {


    public MyFrame(){

        JPanel panel1 = new JPanel();
        JPanel panel2 = new JPanel();
        JPanel panel3 = new JPanel();
        JPanel panel4 = new JPanel();

        JTextArea txt = new JTextArea(3,28);


        //Frame
        this.setSize(320, 375);
        this.setTitle("Calculator by Liath");
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setVisible(true);
        this.setResizable(false);
        this.validate();



        //TextArea
        txt.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
        panel1.add(txt);

        //Panels
        this.add(panel1, BorderLayout.PAGE_START);
        this.add(panel2, BorderLayout.EAST);
        this.add(panel3, BorderLayout.CENTER);
        this.add(panel4,BorderLayout.SOUTH);
        panel3.setLayout(new GridLayout(3, 4));

        //Buttons
        JButton C = new JButton("C");
        JButton Slash = new JButton("/");
        JButton seven = new JButton("7");
        JButton eight = new JButton("8");
        JButton four = new JButton("4");
        JButton one = new JButton("1");
        JButton five = new JButton("5");
        JButton two = new JButton("2");
        JButton nine = new JButton("9");
        JButton six = new JButton("6");
        JButton three = new JButton("3");
        JButton x = new JButton("*");
        JButton minus = new JButton("-");
        JButton plus = new JButton("+");
        JButton zero = new JButton("0");
        JButton point = new JButton(".");
        JButton equal = new JButton("=");




        panel2.add(C);
        panel2.add(Slash);
        panel3.add(seven);
        panel3.add(eight);
        panel3.add(nine);
        panel3.add(x);
        panel3.add(four);
        panel3.add(five);
        panel3.add(six);   
        panel3.add(minus);
        panel3.add(one);
        panel3.add(two);
        panel3.add(three);

        panel3.add(plus);
        panel4.add(zero);
        panel4.add(point);
        panel4.add(equal); 

    }
}

and this is my main class

package main;


public class Main {


        public static void main(String[] args) {
            MyFrame frame= new MyFrame();

        }

    }
Nor Tee
  • 7
  • 1
  • 3
  • Your code clearly states: panel 3 to center, panel 2 to east. Why do you expect them to be on top of one another? – Ordous Jun 12 '14 at 16:15
  • To be honest, I don't expect it to appear this way. I just can't find a way out. – Nor Tee Jun 12 '14 at 16:18
  • Why don't you remove all non-relevant code (creating the frame, adding buttons to panels, etc), make meaningful names for your panels (panel1 doesn't give a very good idea of what it is and where it goes), and create a simple picture of how you want your panels to be laid out? Without that, the question is guesswork and not very readable. – Ordous Jun 12 '14 at 16:26
  • The example cited [here](http://stackoverflow.com/q/10099686/230513) uses a `GridLayout`. – trashgod Jun 12 '14 at 16:52

0 Answers0