Click here to Skip to main content
15,041,525 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have created the following JPanel and I want to display that JPanel within a JFrame form.

JPanel

Java
  import javax.swing.*;

public class JPanelFormInJFrameForm extends JPanel{

    /**
     * @param args the command line arguments
     */
    
        public JPanelFormInJFrameForm(){
        
        setLayout(null);
        JLabel lStudNo = new JLabel("Student No:");
        JLabel lStudName = new JLabel("Student Name:");
        JLabel lMaths = new JLabel("Maths:");
        JLabel lScience = new JLabel("Science:");
        JLabel lEnglish = new JLabel("English:");
        
        JTextField tStudNo = new JTextField();
        JTextField tStudName = new JTextField();
        JTextField tMaths = new JTextField();
        JTextField tScience = new JTextField();
        JTextField tEnglish = new JTextField();
        
        lStudNo.setBounds(50, 40, 80, 25);
        lStudName.setBounds(50, 80, 110, 25);
        lMaths.setBounds(50, 120, 80, 25);
        lScience.setBounds(50, 160, 80, 25);
        lEnglish.setBounds(50, 200, 80, 25);
        
        tStudNo.setBounds(145, 40, 110, 30);
        tStudName.setBounds(145, 80, 110, 30);
        tMaths.setBounds(145, 120, 110, 30);
        tScience.setBounds(145, 160, 110,30);
        tEnglish.setBounds(145, 200, 110, 30);
        
        add(lStudNo);
        add(lStudName);
        add(lMaths);
        add(lScience);
        add(lEnglish);
        
        add(tStudNo);
        add(tStudName);
        add(tMaths);
        add(tScience);
        add(tEnglish);
     /*   
        setTitle("Java Swing Null Layout");
        setSize(300, 250);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);   
        
     */   
        }
        
    public static void main(String[] args) {
        // TODO code application logic here
        
        JFrame Frame = new JFrame();
        Frame.getContentPane().add(new JPanelFormInJFrameForm());
        Frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Frame.setSize(300, 300);
    }
}


At the main() I have tried that but it didn't worked.

I cannot figure out the problem so that if you could someone please help me.

Thank You!

Chiranthaka
Posted
Updated 27-Jun-13 0:45am
v2

- Do NOT work in main
- Do NOT use keywords like "Frame" for variables.
- Do NOT start variable names with upper case - uppercase is an objects name, never a variable.

Please bookmark this link: http://docs.oracle.com/javase/tutorial/uiswing/components/frame.html[^]

Java
public class MyForm extends JFrame{
     
        public MyForm(){
        
        this.init();
       
        }

        private void init(){
        // put code here and in other methods - not constructor
        }
        
    public static void main(String[] args) {
        // TODO code application logic here
        
        JFrame myFrame = new MyForm();
        myFrame.getContentPane().add(new JPanelFormInJFrameForm());
        myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        myFrame.setSize(300, 300);
    }
}
   
You did everything but forget to display the frame.
Java
frame.setVisible(true);


And the line should come at the end.
   

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