0

Suppose to pull up 3 different windows with different Labels and Strings in the frames. Below is a picture of the projected output

https://i.stack.imgur.com/KlcKh.png

I have three classes.

  • P1Panel that extends JPanel
  • P1Frame that extends JFrame
  • P1Driver used for main

    ----------------------------------------------------------P1Panel class below:

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class P1Panel extends JPanel
    {
    
        private String s0;
        public P1Panel(String s0){
            {
                add(new P1Panel(s0));
            }
        }
    } 
    

---------------------------------------------This is P1Frame class below:

        import java.awt.*;
        import java.awt.event.*;
        import javax.swing.*;
        public class P1Frame extends JFrame
    {
            private String s1;
            public P1Frame (String s1){
                this.s1 = s1;
                {
                    add(new P1Panel(s1));
                }
                P1Frame p1 = new P1Frame(s1);



      this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            p1.setSize(300,200);
            p1.setVisible(true);
        }
    }

----------------------------------------This is P1Driver class:

        import java.awt.*;
        import java.awt.event.*;
        import javax.swing.*;
        public class P1Driver
        {
            public static void main(String [] args)
            {
                P1Frame p1 = new P1Frame("This is window 1");
                //JFrame f2 = new JFrame("This is window 2");
                //JFrame f3 = new JFrame("This is window 3");
                p1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                p1.setSize(300,200);
                p1.setVisible(true);        
            }
        }
  • I believe my P1Panel class is correct in that I called a constructor and added it to itself. The text in my label is passing the P1Panel constructor as a parameter

  • My P1Frame class I am having difficult with. In the constructor I am wanting to make a P1Panel object and add it to the P1Frame. I think I need to pass a string into the P1Frame constructor as a parameter and then pass string to P1Panel?

  • I believe my Driver class is correct too as I am just putting main here and setting items size and visibility.

I believe my fix is a small one, but I am stuck and unsure in how to do so. When I run the program as is, it runs infinite with nothing popping up.

camickr
  • 321,443
  • 19
  • 166
  • 288
Tanner10
  • 29
  • 5
  • 2
    `Need to open 3 separate windows` See: http://stackoverflow.com/questions/9554636/the-use-of-multiple-jframes-good-bad-practice – camickr Sep 01 '15 at 01:38
  • 1
    1) See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) 2) *"I believe my fix is a small one,.."* Is your *question* along the lines of *"How to fix this?"* - so far there is no question that I can see. – Andrew Thompson Sep 01 '15 at 01:38
  • Yes, I am stuck in the P1Frame class and need some aid in solving the issue – Tanner10 Sep 01 '15 at 01:41

1 Answers1

0

You have an infinitely recursive constructor in P1Panel class and that is why your program gets stuck. Just remove the line

add(new P1Panel(s0));

when you execute this line in the constructor you are essentially calling it again by doing

new P1Panel(s0)

so the constructor never returns and it keeps calling itself recursively. Plus why are you not adding any Swing components to your P1Panel? It will be empty without Swing components. If you want to display a String inside the panel, I suggest you do the below in the constructor.

setLayout(new BorderLayout());
JLabel label = new JLabel(s0);
add(label, BorderLayout.CENTER);