-1

I trying to add a rectangle and 30 JButton in JPanel totalGUI but only rectangle is shown.

public class SelectSeat {

    static JFrame frame;
    int a;

    public JPanel createContentPane() throws IOException
    {

        JPanel totalGUI = new JPanel();
        RectDraw rect= new RectDraw();
        rect.setPreferredSize(new Dimension(330,35)); 
        totalGUI.setLayout(null);
        totalGUI.setBackground(Color.WHITE);
        totalGUI.add(rect);
        Dimension d = rect.getPreferredSize();
        rect.setBounds(100, 20, d.width, d.height);

        JToggleButton[] ButtonList = new JToggleButton[30];

        for(int i = 0; i < 30; i++) {
            a=i+1;
            ButtonList[i]= new JToggleButton(""+a);
            totalGUI.add(ButtonList[i]);
        }
        totalGUI.add(rect);

        return totalGUI;
    }

    void setVisible(boolean b) {
        // TODO Auto-generated method stub
    }

    static void createAndShowGUI() throws IOException
    {

        JFrame.setDefaultLookAndFeelDecorated(true);
        frame = new JFrame("Seat Selection");

        //Create and set up the content pane.
        SelectSeat demo = new SelectSeat();
        frame.setContentPane(demo.createContentPane());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(535, 520);
        frame.setLocation(500,220);
        frame.setVisible(true);
    }

    private static class RectDraw extends JPanel
    {
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);  
             g.setColor(Color.GRAY); 
             g.fillRect(0,0,getWidth(),getHeight());
             g.setColor(Color.BLUE);
             g.drawString("Movie Sceen", 130, 20);   
            }


    }

}

enter image description here

Latest Output

enter image description here


BuyTicket

next.addActionListener(new ActionListener()  // go to SelectSeat class
        {
             public void actionPerformed(ActionEvent e)
                {
                 SelectSeat back = new SelectSeat();
                 back.setVisible(true);
                    setVisible(false);
                    frame.dispose();
                }
        });

SelectSeat

public class SelectSeat {

    public static void main(String[] atrg) throws IOException {
    JFrame meinJFrame = new JFrame();
    meinJFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    meinJFrame.setSize(800, 600);
    meinJFrame.setVisible(true);
    meinJFrame.add(new RectDraw());
    JPanel over = new JPanel(new GridLayout(2, 1));
    over.add(new RectDraw());
    meinJFrame.setTitle("Cinemax-Beispiel");
    JPanel panel = new JPanel();
    panel.setLayout(new GridLayout(6, 5));
    for (int i = 1; i <= 30; i++) {
        panel.add(new JToggleButton("Seat :" + i));
    }
    over.add(panel);
    meinJFrame.add(over);
    }

    public void setVisible(boolean b) {  // auto-generated
        // TODO Auto-generated method stub

    }



}

class RectDraw extends JPanel {
    @Override
    protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.setColor(Color.GRAY);
    g.fillRect(0, 0, getWidth(), getHeight());
    g.setColor(Color.WHITE);
    g.drawString("Movie Sceen", 130, 20);
    }
}       
AI.
  • 934
  • 2
  • 14
  • 30
  • 1
    Instances should not start with capital letter - it should be `buttonList`. In this scenario I would probably create an inner JPanel with a GridLayout, add all these radio buttons to it, and then add the panel to the `totalGUI`. Finally, I would set `totalGUI` layout to SpringLayout, so the width and height of your rectangle and buttons would adjust depending on the size of JFrame. – Coderino Javarino Jul 03 '16 at 13:25
  • 1) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/).That code needs at least the impot statements and a `main(String[])` method to put it on-screen, in order to be The C, V or E of MCVE. 2) Provide ASCII art or a simple drawing of the *intended* layout of the GUI at minimum size, and if resizable, with more width and height. – Andrew Thompson Jul 03 '16 at 13:47
  • @AndrewThompson Is it a must to have `main(String[])` ? I have many classes without `main` but they still can run. – AI. Jul 03 '16 at 16:35
  • 1
    *"I have many classes written without main but they still can run."* If that's the case, your IDE must be creating a main method behind the scenes. *"Is it a must to have `main(String[])`?"* To be an MCVE, yes. And although I started that comment with *"For better help sooner.."* what I did not mention is that there are actually 2 close reasons that mention 'no MCVE' as part of the reason. I just selected one of those reasons, and if 4 more people agree, the question will be closed. So I'll leave the question of whether it is 'must' - to your discretion. – Andrew Thompson Jul 03 '16 at 16:40
  • What you've posted now is a number of code snippets. Some not even a complete class. Part of the idea of posting an MCVE is to make it easy for people to see the problem, but that requires multiple copy/pastes, adding imports and fleshing out a complete class for whatever that action listener was in. I mentioned imports in my first comment, and it seems you have either not read, or not understood, the MCVE/SSCCE articles. I have better things to do than spoon feed instructions that are clearly outlined in links, mostly helping people that **can** manage to post code we can work with. – Andrew Thompson Jul 03 '16 at 17:39

1 Answers1

2

The point is that you are not well organizing the jpanel You should organize your panels in that form:

----JPanel----------------------------|
| ---Screen el----------------------  |
| |             txt                 | |
| |                                 | |
| ----------------------------------- |
| ---seats -------------------------- |
| |seat1  seat2 .. ...  ...  ...    | |
| |seat11 seat12 .. ...  ...  ..    | |
| |... ... ...                      | |
| ----------------------------------- |
---------------------------------------


public class Mango {

    public static void main(String[] atrg) throws IOException {
    JFrame meinJFrame = new JFrame();
    meinJFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    meinJFrame.setSize(800, 600);
    meinJFrame.setVisible(true);
    meinJFrame.add(new RectDraw());
    JPanel over = new JPanel(new GridLayout(2, 1));
    over.add(new RectDraw());
    meinJFrame.setTitle("Cinemax-Beispiel");
    JPanel panel = new JPanel();
    panel.setLayout(new GridLayout(6, 5));
    for (int i = 1; i <= 30; i++) {
        panel.add(new JToggleButton("Seat :" + i));
    }
    over.add(panel);
    meinJFrame.add(over);
    }
}

class RectDraw extends JPanel {
    private static final long serialVersionUID = -6507364980760764676L;

    @Override
    protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.setColor(Color.GRAY);
    g.fillRect(0, 0, getWidth(), getHeight());
    g.setColor(Color.WHITE);
    g.drawString("Movie Sceen", 130, 20);
    }
}

enter image description here

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97