Click here to Skip to main content
15,040,090 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hello,
i have 2 classes in java.
1)a class that extends Jframe
2)another that extends JPanel

I try to add the JPanel in the JFram class , it looks something like this:

public static void main(String args[]) { // the main is in the JFram class
        Othello Oth= new Othello();    // instance of the JFram class
        Oth.setLocation(250, 150);
        OthelloBoard OB = new OthelloBoard();     // instance of JPanel class
        Oth.add(OB);
        Oth.setVisible(true);


but only the JFrame got shown and it seems that the JPanel i added did not go onto the JFrame (i am using NetBeans).

How can I solve this problem??

Thank you...
Posted
Updated 18-Mar-11 5:18am
v4
Comments
[no name] 17-Mar-11 5:36am
   
What is your layout??
caabool_oo 17-Mar-11 11:50am
   
I didn't specify the layout for form or panel.

This could help you no then please reply,
public static void main(String args[]) { 
// the main is in the JFram class        
Othello Oth= new Othello();    // instance of the JFram class       
 Oth.setLocation(250, 150);        
OthelloBoard OB = new OthelloBoard();   // instance of JPanel class        
Oth.setLayout(new GridLayout(1,1));

///Oth.add(OB,BorderLayout.CENTER);
//Oth.setContentPane( BorderLayout.CENTER);
Oth.setVisible(true);


and please

review this classic example of adding panel to frame

panel to frame
[^]
   
v3
Comments
caabool_oo 17-Mar-11 6:04am
   
Thank you for your replay
but when i add

Oth.setContentPane( BorderLayout.CENTER);

it give me error. because this function must take container..

and when i applied the example the Jpanel is appear but i need to manage the fram easily (for that i used netbeans and used extended JFram)..

thank you
[no name] 17-Mar-11 6:14am
   
Please review my updated answer
caabool_oo 17-Mar-11 10:01am
   
sorry for late :|

the problem is not solved.

any other ideas.
[no name] 18-Mar-11 4:37am
   
i thing so its problem of layout surly
please try my updated answer
[no name] 18-Mar-11 4:39am
   
And remove you code making whole code public not good thing just provide a fragment
caabool_oo 18-Mar-11 5:25am
   
WoW its work ;D

thank you very much ..
[no name] 18-Mar-11 5:25am
   
Have you try grid layout?
caabool_oo 18-Mar-11 5:27am
   
yes and it is work...

thank you very much..
[no name] 18-Mar-11 5:28am
   
Hey its ok ,lets me explain some things regarding,
A default lay out of frame is flowlayout
you must change this layout becouase flaow of frame may take rong co-ords and elements are displays on uexpected postion.
if you satistfied then accept answer and
best of luck with you project
BEST REGARD:
SANTOSH
Can any one help me please ..

I applied the santosh dhanawade solution but it didn't work.

this is the two main clasess:

Othello.Java
C#
public class Othello extends javax.swing.JFrame {

    OthelloBoard OB;

    /** Creates new form Othello */

    public Othello() {

        initComponents();
        OB = new OthelloBoard();
        add(OB,BorderLayout.CENTER);
        setVisible(true);
}

public static void main(String args[]) {


            Othello Oth= new Othello();
            Oth.setLocation(250, 150);




    }
   
v3
Comments
[no name] 17-Mar-11 23:27pm
   
can you send me whole code
caabool_oo 18-Mar-11 3:34am
   
Ok, but how can i send it to you??
[no name] 18-Mar-11 3:44am
   
just update this question by code.
caabool_oo 18-Mar-11 3:45am
   
do you need the whole code?? because it is very very long .
[no name] 18-Mar-11 4:02am
   
Just provide fragmenemt of Jpanel class and Jframe
caabool_oo 18-Mar-11 4:22am
   
Sorry, i think i made a mistak when i update the qustion. the problem is not solved . how can i remove the accept solution announcement???

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900