1

I am currently working on building an ATM machine and I am having trouble setting up the borderlayout of multiple JPanels. Basically I am trying to make my picture look something along the lines of this

But when I run it it looks like this:

Here is the code for how I am trying to do this.

public Main() {

    try {
        atmPicture = ImageIO.read(new File("atmpicture.jpg"));
        welcomeScreen = ImageIO.read(new File("Welcome Screen.jpg"));
        enterAccScreen = ImageIO.read(new File("Account Number.jpg"));
        mainButtons = ImageIO.read(new File("Main Screen.jpg"));
        withdrawScreen = ImageIO.read(new File("R2 click.jpg"));
        depositScreen = ImageIO.read(new File("R3 click.jpg"));
        transferScreen = ImageIO.read(new File("R4 click.jpg"));
        accInfoScreen = ImageIO.read(new File("L1 Click.jpg"));
        withdrawScreen2 = ImageIO.read(new File("R2.2 Click.jpg"));
    } catch (IOException ex) {
        System.out.println("The file cannot read2");
        System.exit(1);
    }


    panel1 = new JPanel(new GridLayout(4, 3)); //numpad
    panel2 = new JPanel(new GridLayout(3, 1)); //left three buttons
    panel3 = new JPanel(new GridLayout(3, 1)); // right three buttons
    panelMain = new JPanel(new GridLayout(0, 3)); // textfield


    jtf.setHorizontalAlignment(SwingConstants.LEFT);
    jtf.setPreferredSize(new Dimension(10, 10));
    initButtons();
}

/**
 * Initializes all the buttons and adds them to the 
 */
private void initButtons() {

    panel1.add(b1);
    panel1.add(b2);
    panel1.add(b3);
    panel1.add(b4);
    panel1.add(b5);
    panel1.add(b6);
    panel1.add(b7);
    panel1.add(b8);
    panel1.add(b9);
    panel1.add(ba);
    panel1.add(b0);
    panel1.add(bp);

    panel2.add(l1);
    panel2.add(l2);
    panel2.add(l3);

    panel3.add(r1);
    panel3.add(r2);
    panel3.add(r3);

    Database cData = new Database();
    cData.storeData();

    ButtonListener listener = new ButtonListener(cData);

    b1.addActionListener(listener);
    b2.addActionListener(listener);
    b3.addActionListener(listener);
    b4.addActionListener(listener);
    b5.addActionListener(listener);
    b6.addActionListener(listener);
    b7.addActionListener(listener);
    b8.addActionListener(listener);
    b9.addActionListener(listener);
    b0.addActionListener(listener);
    ba.addActionListener(listener);
    bp.addActionListener(listener);
    l1.addActionListener(listener);
    l2.addActionListener(listener);
    l3.addActionListener(listener);
    r1.addActionListener(listener);
    r2.addActionListener(listener);
    r3.addActionListener(listener);

    setLayout(new BorderLayout());


    panelMain.add(panel1, BorderLayout.PAGE_END);
    panelMain.add(panel2, BorderLayout.LINE_START);
    panelMain.add(panel3, BorderLayout.LINE_END);
    panelMain.add(jtf, BorderLayout.CENTER);

    add(panelMain);

}

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {

            public void run() {

                Image upper = null;
                Image right = null;
                Image left = null;
                Image bottom = null;
                //Image middle = null;
                JFrame frame = new JFrame();
                try {
                    upper = ImageIO.read(new File("upper portion.jpg"));
                    right = ImageIO.read(new File("right portion.jpg"));
                    left = ImageIO.read(new File("left portion.jpg"));
                    bottom = ImageIO.read(new File("bottom portion.jpg"));
                    //middle = ImageIO.read(new File("middle portion.jpg"));
                } catch (IOException ex) {
                    System.out.println("The file cannot read");
                    System.exit(1);
                }

                JLabel labelTop = new JLabel(new ImageIcon(upper));
                JLabel labelRight = new JLabel(new ImageIcon(right));
                JLabel labelLeft = new JLabel(new ImageIcon(left));
                JLabel labelBottom = new JLabel(new ImageIcon(bottom));
                //JLabel labelMiddle = new JLabel(new ImageIcon(middle));

                labelTop.setLayout(new BorderLayout());
                labelRight.setLayout(new BorderLayout());
                labelLeft.setLayout(new BorderLayout());
                labelBottom.setLayout(new BorderLayout());
                //labelMiddle.setLayout(new BorderLayout());

                frame.add(labelTop, BorderLayout.PAGE_START);
                frame.add(labelLeft, BorderLayout.LINE_START);
                frame.add(labelRight, BorderLayout.LINE_END);
                frame.add(labelBottom, BorderLayout.PAGE_END);
                frame.add(new Main(), BorderLayout.CENTER);

                frame.setSize(1041, 1200);
                frame.setVisible(true);
            }
        });

}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Tyler
  • 11
  • 2
  • Use a JLayer to put the "press any key to continue" message on top of your layout – ControlAltDel Jun 02 '15 at 18:16
  • 1
    @Rocketq I need 10 reputation to post a picture. – Tyler Jun 02 '15 at 18:20
  • 1
    @ControlAltDel Thank you. But I also need to be able to move the buttons to specific locations. – Tyler Jun 02 '15 at 18:20
  • 1) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example) or [SSCCE](http://www.sscce.org/) (Short, Self Contained, Correct Example). 2) One way to get image(s) for an example is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556) however most images (e.g. used as icons) are not necessary for a layout problem. – Andrew Thompson Jun 02 '15 at 18:20

1 Answers1

3
panelMain = new JPanel(new GridLayout(0, 3)); 

You set the layout for panelMain to be a GridLayout();

panelMain.add(panel1, BorderLayout.PAGE_END);
panelMain.add(panel2, BorderLayout.LINE_START);
panelMain.add(panel3, BorderLayout.LINE_END);
panelMain.add(jtf, BorderLayout.CENTER);

But then you treat panelMain like a BorderLayout. You can't just randomly use the BorderLayout contraints.

If you want panelMain to use a BorderLayout then create it with a BorderLayout:

panelMain = new JPanel(new BorderLayoutLayout()); 
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
camickr
  • 321,443
  • 19
  • 166
  • 288