Adding JPanel to JFrame

后端 未结 5 1613
长情又很酷
长情又很酷 2021-02-06 04:24

I have a program in which a JPanel is added to a JFrame:

public class Test{

    Test2 test = new Test2();
    JFrame frame = new JFrame();

    Test(){

    ...         


        
5条回答
  •  夕颜
    夕颜 (楼主)
    2021-02-06 04:52

    do it simply

    public class Test{
        public Test(){
            design();
        }//end Test()
    
    public void design(){
        JFame f = new JFrame();
        f.setSize(int w, int h);
        f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        f.setVisible(true);
        JPanel p = new JPanel(); 
        f.getContentPane().add(p);
    }
    
    public static void main(String[] args){
         EventQueue.invokeLater(new Runnable(){
         public void run(){
             try{
                 new Test();
             }catch(Exception e){
                 e.printStackTrace();
             }
    
     }
             );
    }
    
    }
    

提交回复
热议问题