javaswing图形用户界面的布局问题

wf82jlnq  于 2021-07-04  发布在  Java
关注(0)|答案(0)|浏览(248)

我是java的新手,还在学习。这是我在制作gui时遇到的麻烦。它的格式应该是这样的。

但不是上面的gui,而是这样的

我的导师说使用主面板的gridlayout来显示其他3个面板,然后在3个面板中使用flowlayout。我做了,但还是不行。顺便问一下,我怎样才能使组件处于下线状态?请帮忙,谢谢。这是我的类代码,如图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);
            }

        }
    }
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题