Monday, February 26, 2018

Java swing program : Create frame by Inheritance

 //We can also inherit the JFrame class, so there is no need to create the instance of JFrame class explicitly.
   
    import javax.swing.*; 
    public class SwingByInheritance extends JFrame{//inheriting JFrame 
   
    SwingByInheritance(String s){  // we must know which method should be used to display this string as frame title
    //JFrame f1=new JFrame(s);
    super(s);
    JButton b=new JButton("Submit");//create button 
    b.setBounds(130,100,100, 40); 
       
    add(b);//adding button on frame 
    setSize(400,500); 
    setLayout(null); 
    setVisible(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   
    //f1.add(b);//adding button on frame 
    //f1.setSize(400,500); 
    //f1.setLayout(null); 
    //f1.setVisible(true);
    //f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

   
    public static void main(String[] args) { 
    new SwingByInheritance("If all is done");
   
    JFrame f=new JFrame("But I want to label my frame hence I need JFrame constructor");
    JButton b1=new JButton("Click");
     b1.setBounds(130,100,100, 40);
    f.add(b1);   
    f.setSize(400,400);   
    f.setLayout(null);   
    f.setVisible(true);   
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   
    }
   
    } 

Thanks
Happy programming!

No comments:

Post a Comment

How can I run a C++ program directly from Windows?

How-can-I-run-a-C-program-directly-from-Windows