Trouble in Layout of Java Swing Gui

Viewed 65

I'm a newbie of java and still learning. And here is the trouble I'm in while making the GUI. Its format supposes to be like this.

image 1

But instead of the above GUI, it's like this

image 2

My tutor said using the GridLayout for the main panel to show 3 other panels, then using the FlowLayout inside 3 panels. I did it, but it's still not working. By the way, how I can make the component being down the line? Please help and thank you. Here is my code of the class showing in image 2.

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import model.*;

public class AddPanel extends CustomPanel
{
    private Cinema cinema;
    private Movies movies = new Movies();
    private Theatres theatres = new Theatres();
    private Sessions sessions = new Sessions();
    private AddMoviePanel panel1 = new AddMoviePanel();
    private AddTheatrePanel panel2 = new AddTheatrePanel();
    private AddSessionPanel panel3 = new AddSessionPanel();
    public AddPanel(Cinema cinema)
    {  
        this.cinema = cinema;
        setup();
        build();
    }

    public void setup()
    {  
        setLayout(new GridLayout(3, 1)); 
    }   

    public void build()
    {   
        add(panel1);
        add(panel2);
        add(panel3);
    }
    private class AddMoviePanel extends JPanel
    {
        private JTextField name = new JTextField(10);
        private JTextField cost = new JTextField(5);
        private setListener listener = new setListener();
        public AddMoviePanel()
        {
            setup();
            build();
        }

        public void setup()
        {
            setLayout(new FlowLayout(FlowLayout.CENTER));
        }

        public void build()
        {
            addTitle("Add a Movie");
            addPair("Movie name: ", name);
            addPair("Movie cost: ", cost);
            addButton("Set", listener);
        }
        private class setListener implements ActionListener
        {
            public void actionPerformed(ActionEvent e)
            {
                double thecost = Double.parseDouble(cost.getText());
                String thename = name.getText();
                movies.add(thename,thecost);
            }
        }
    }
    private class AddTheatrePanel extends JPanel
    {
        private JTextField name = new JTextField(10);
        private JTextField gold = new JTextField(5);
        private JTextField regular = new JTextField(5);
        private setListener listener = new setListener();
        public AddTheatrePanel()
        {
            setup();
            build();
        }

        public void setup()
        {
            setLayout(new FlowLayout(FlowLayout.CENTER));
        }

        public void build()
        {
            addTitle("Add a Theatre");
            addPair("Theatre name: ", name);
            addPair("Number of gold seats: ", gold);
            addPair("Number of regualr seats: ", regular);
            addButton("Set", listener);
        }
        private class setListener implements ActionListener
        {
            public void actionPerformed(ActionEvent e)
            {
                int thegold = Integer.parseInt(gold.getText());
                int theregular = Integer.parseInt(regular.getText());
                String thename = name.getText();
                theatres.add(thename,thegold,theregular);
            }
        }
    }
    private class AddSessionPanel extends JPanel
    {
        private JTextField name = new JTextField(10);
        private JTextField movieid = new JTextField(5);
        private JTextField theatreid = new JTextField(5);
        private JTextField time = new JTextField(5);
        private JLabel sessiontime = new JLabel("Enter Session Time - 0 for 9am, 1 for 12noon, 2 for 3pm or 3 for 6pm");
        private JTextField goldprice = new JTextField(5);
        private JTextField regularprice = new JTextField(5);
        private setListener listener = new setListener();
        public AddSessionPanel()
        {
            setup();
            build();
        }

        public void setup()
        {
            setLayout(new FlowLayout(FlowLayout.CENTER));
        }

        public void build()
        {
            addTitle("Add a Session");
            addPair("Session name: ", name);
            addPair("Movie ID: ", movieid);
            addPair("Theatre ID: ", theatreid);
            add(sessiontime);
            addPair("Session time: ", time);
            addPair("Price for a gold seat: ", goldprice);
            addPair("Price for a regular seat: ", regularprice);
            addButton("Set", listener);
        }
        private class setListener implements ActionListener
        {
            public void actionPerformed(ActionEvent e)
            {
                int thegold = Integer.parseInt(goldprice.getText());
                int theregular = Integer.parseInt(regularprice.getText());
                int themovieid = Integer.parseInt(movieid.getText());
                int thetheatreid = Integer.parseInt(theatreid.getText());
                int thetime = Integer.parseInt(time.getText());
                String thename = name.getText();
                cinema.addSession(thename,themovieid,thetheatreid,thetime,thegold,theregular);
            }

        }
    }
}

0 Answers
Related