-2

I am a complete beginner at Java coding and I have never worked with GUI before. Here's what I am trying to do. I have main class "Frame" and two classes: "Circle" and "Square". How do I add Circle and Square to Frame that they'll appear on the frame?

Sorry for this easy question but I need your help on this. Thanks in advance!

public class Frame extends JFrame{

    public static void main(String[] args) {
        Frame f = new Frame();
        Circle circle  = new Circle();
        Square square  = new Square();
        f.add(circle);
        f.add(square);
    }

    public Frame(){
        setTitle("Frame");
        setSize(500, 500);
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }
}



public class Circle extends JFrame{

    public Circle(){
    }

    public void paint(Graphics g){
        g.setColor(Color.GREEN);
        g.drawOval(300, 300, 200, 200);
    }
}


public class Square extends JFrame{

    public Square(){
    }

    public void paint(Graphics g){
        g.setColor(Color.GREEN);
        g.drawRect(300, 300, 500, 500);
   }
}
David
  • 14,569
  • 34
  • 78
  • 107
lioli
  • 191
  • 1
  • 13
  • 2
    You cannot add a `Top Level Container` to another `Top Level Container`, in my humble opinion. Either make Circle and Square `JPanel`s and then try to add them, on the `JFrame`. – nIcE cOw Jun 10 '14 at 18:03

1 Answers1

1

First off all you cannot add a JFrame to a JFrame because you would be adding a window to a window. What you could do though is make circle and square extend JPanel. This way you would be able to add them to your JFrame without any problem and they will do what they have to do. Also my JDK tells me that Circle and Square should have their own file. And also depending on the order you add square and circle in to the JFrame, you'll only see one of them because the second one is painted over the first one. Ok so after this your code would transform into:

File 1 Frame:

public class Frame extends JFrame{

public static void main(String[] args) {
    Frame f = new Frame();
    Circle circle  = new Circle();
    Square square  = new Square();
    f.add(circle);
    f.add(square);
}

public Frame(){
    setTitle("Frame");
    setSize(500, 500);
    setVisible(true);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
}

}

File 2 Circle:

public class Circle extends JPanel{

public Circle(){
}

public void paint(Graphics g){
    g.setColor(Color.GREEN);
    g.drawOval(300, 300, 200, 200);
}

}

File 3 Square:

public class Square extends JPanel{

public Square(){
}

public void paint(Graphics g){
    g.setColor(Color.GREEN);
    g.drawRect(300, 300, 500, 500);

} }



Hope this help you and that you understand why this works. If you do not feel free to ask for more clarification.

P.S. Welcome to Java GUI programming ;)

Roan
  • 1,200
  • 2
  • 19
  • 32
  • Heya! Thanks very much! I just tried your code and it works so far. But is there also a way on how I can make both JPanels visible? Without the latter one overlapping the first JPanel that was set? – lioli Jun 10 '14 at 18:32
  • @user3561975: use `LayoutManager` for your specific needs. `GridLayout` might work for this case. – nIcE cOw Jun 10 '14 at 19:10
  • Yeah sure there's. Specify a Layout manager to layout the components. for example add this: f.setLayout(new GridLayout(1, 2, 0, 0));right under the decalration of frame (the second line in the main method) and maybe make you circle and square a little smaller. And now you should see both net to each other. This is because without layout manager only the last component added is visible. Also if this answer answers your question don't forget to accept this as the answer by checking the green check thing at the answer. And if you have any more question just ask them and I'll try to answer them. – Roan Jun 10 '14 at 19:12
  • Thanks again! Just tried it out and it almost works if circle and square are displayed next to each other. Both elements are visible but once the square enter the circle's "territory" a part of the circle vanishes... – lioli Jun 10 '14 at 19:23
  • That's because the layout manager is dividing the available space equally between the circle and the cube. They'll both get half no more or less. You'll have to think of it as two sheets of paper one with a square one with a circle. Now when you move them togheter one of the paper sheets is going beneth the other one. – Roan Jun 10 '14 at 19:40
  • Thank you very much. But is there also a way with which I can overlap those two sheets. Let's say by making the background of the upper sheet transparent? – lioli Jun 10 '14 at 20:29
  • 1
    What you're saying is indeed possible. But it's far more easier to just let one class paint both a circle and a square. Else you'd have to put the panels(sheets) on top of each other by using OverlayLayout or JLayeredPane and then give the upper one a transparent background (I'm willing to expain this if you really want to). – Roan Jun 10 '14 at 20:41