calling jframe methods from constructor

别说谁变了你拦得住时间么 提交于 2019-12-12 06:30:41

问题


Are there any problems in calling methods from the constructor in this particular case?

    class GUI2
{
    JFrame jfrm;
    static Container cntr;
    GUI2(){

        jfrm=new JFrame("Raaga");
        jfrm.setSize(555,493);
        jfrm.setResizable(false);
        jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jfrm.addWindowListener(new WindowAdapter(){
            public void windowClosing(WindowEvent we){
                System.exit(0);
            }
        });
        jfrm.setVisible(true);
    }

回答1:


There would be no problems if you write so. Ofcourse,writing too much business logic is not a good practice,IMO.

If you still can't avoid, in such cases create a method and do there.That should be more readable.

GUI2(){
    intialize();    
}

And write logic there.

  private void intialize(){
        jfrm=new JFrame("Raaga");
        jfrm.setSize(555,493);
        jfrm.setResizable(false);
        jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jfrm.addWindowListener(new WindowAdapter(){
            public void windowClosing(WindowEvent we){
                System.exit(0);
            }
        });
        jfrm.setVisible(true);       
  }


来源:https://stackoverflow.com/questions/18844405/calling-jframe-methods-from-constructor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!