问题
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