【发布时间】:2014-02-27 00:56:49
【问题描述】:
我一直在 main 方法中完全初始化我的 SWT shell,如下所示:
public class MySWTTemplate;
public static void main(String[] args){
Display display = new display();
Shell shell = new Shell(display);
shell.setSize(250, 250);
Rectangle bds = getMonitor().getBounds();
Point p = shell.getSize();
int xPos = (bds.width - p.x) / 2;
int yPos = (bds.height - p.y) / 2;
shell.setBounds(xPos, yPos, p.x, p.y);
shell.open();
while(!shell.isDisposed()){
if(!display.readAndDispatch()){
display.sleep();
}
}
display.dispose();
}
但是我看到有些人设置了shell启动和定义在他们自己的方法中,而启动时屏幕上的单元格定位在另一个中,两者都通过main方法简单地调用。我对此进行了尝试,并提出了以下建议:
public class MySWTTemplate;
public static void main(String[] args){
Display display = new Display();
new MySWTTemplate(display);
display.didpose();
}
public MySWTTemplate(Display display){
Shell shell = new Shell(display);
shell.setSize(250, 250);
shellPos(shell);
shell.open();
while(!shell.isDisposed()){
if(!display.readAndDispatch()){
display.sleep()
}
}
}
public void shellPos(Shell shell){
Rectangle bds = shell.getMonitor().getBounds();
Point p = shell.getSize();
int xPos = (bds.width - p.x) / 2;
int yPos = (bds.height - p.y) / 2;
shell.setBounds(xPos, yPos, p.x, p.y);
}
两者都有效。我的两个问题是
1) 这样做只是为了视觉组织,还是为每个作业调用单独的方法提供资源收益?
2) 有人可以在第二个代码中解释创建 MySWTTemplate() 方法然后在 main 方法中简单地用“new MySWTTemplate(display)”调用时发生了什么吗?为什么方法的名称与类的名称完全相同?这从根本上说的是什么(您不能将此方法设置为 public 与任何其他方法名称。
【问题讨论】: