学习RCP过程中,没有注意到全局变量与局部变量的区别,导致得不到要想的全局变量的值,
花了不少时间,终于找到错误根源,惭愧!
源码如下,引以为戒!!
package org.davy.example.crm.ui;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.ui.part.ViewPart;
public class RelationViewPart extends ViewPart {
public static final String ID = "org.davy.example.crm.ui.RelationViewPart"; //$NON-NLS-1$
private Relation relation;
/*
* 注意局部变量与全局变量的区别,在本程序中由设计器生成的代码中的变量为局部变量
* 会覆盖到全局变量,例如在本程序中定义中phoneLabel会被creatPartControl方法中
* 自动生成的final Label phoneLabel所覆盖,进而变成局部变量,这样会造成setRelation方法
* 得不到phoneLabel的值,出现错误。因此应把自动生成的final Label去掉
*
*/
private Label phoneLabel;
private Label addressLabel;
private Label mailLabel;
private Label zipLabel;
private Label foxLabel;
public RelationViewPart(){
super();
}
/**
* Create contents of the view part
* @param parent
*/
@Override
public void createPartControl(Composite parent) {
parent.setLayout(new FormLayout());
final Label label = new Label(parent, SWT.NONE);
final FormData fd_label = new FormData();
fd_label.right = new FormAttachment(0, 70);
fd_label.bottom = new FormAttachment(0, 40);
fd_label.top = new FormAttachment(0, 20);
fd_label.left = new FormAttachment(0, 15);
label.setLayoutData(fd_label);
label.setText("电话:");
final Label label_1 = new Label(parent, SWT.NONE);
final FormData fd_label_1 = new FormData();
fd_label_1.bottom = new FormAttachment(0, 70);
fd_label_1.top = new FormAttachment(0, 50);
fd_label_1.right = new FormAttachment(label, 55, SWT.LEFT);
fd_label_1.left = new FormAttachment(label, 0, SWT.LEFT);
label_1.setLayoutData(fd_label_1);
label_1.setText("地址:");
final Label label_2 = new Label(parent, SWT.NONE);
final FormData fd_label_2 = new FormData();
fd_label_2.bottom = new FormAttachment(0, 100);
fd_label_2.top = new FormAttachment(0, 80);
fd_label_2.right = new FormAttachment(label_1, 55, SWT.LEFT);
fd_label_2.left = new FormAttachment(label_1, 0, SWT.LEFT);
label_2.setLayoutData(fd_label_2);
label_2.setText("电子邮箱:");
final Label label_3 = new Label(parent, SWT.NONE);
final FormData fd_label_3 = new FormData();
fd_label_3.bottom = new FormAttachment(0, 130);
fd_label_3.top = new FormAttachment(0, 110);
fd_label_3.right = new FormAttachment(label_2, 55, SWT.LEFT);
fd_label_3.left = new FormAttachment(label_2, 0, SWT.LEFT);
label_3.setLayoutData(fd_label_3);
label_3.setText("邮编:");
final Label label_4 = new Label(parent, SWT.NONE);
final FormData fd_label_4 = new FormData();
fd_label_4.bottom = new FormAttachment(0, 165);
fd_label_4.top = new FormAttachment(0, 145);
fd_label_4.right = new FormAttachment(label_3, 55, SWT.LEFT);
fd_label_4.left = new FormAttachment(label_3, 0, SWT.LEFT);
label_4.setLayoutData(fd_label_4);
label_4.setText("传真:");
/*
* 注意改正自动生成的变量,去掉final Label
*/
//改正之前代码如下:
//final Label phoneLabel = new Label(parent, SWT.NONE);
phoneLabel = new Label(parent, SWT.NONE);
phoneLabel.setText("123");
FormData fd_phoneLabel = new FormData();
fd_phoneLabel.bottom = new FormAttachment(label, 0, SWT.BOTTOM);
fd_phoneLabel.top = new FormAttachment(0, 17);
fd_phoneLabel.right = new FormAttachment(0, 390);
fd_phoneLabel.left = new FormAttachment(0, 80);
phoneLabel.setLayoutData(fd_phoneLabel);
addressLabel = new Label(parent, SWT.NONE);
FormData fd_addressLabel = new FormData();
fd_addressLabel.bottom = new FormAttachment(label_1, 23, SWT.TOP);
fd_addressLabel.top = new FormAttachment(label_1, 0, SWT.TOP);
fd_addressLabel.right = new FormAttachment(phoneLabel, 310, SWT.LEFT);
fd_addressLabel.left = new FormAttachment(phoneLabel, 0, SWT.LEFT);
addressLabel.setLayoutData(fd_addressLabel);
mailLabel = new Label(parent, SWT.NONE);
FormData fd_mailLabel = new FormData();
fd_mailLabel.bottom = new FormAttachment(addressLabel, 28, SWT.BOTTOM);
fd_mailLabel.top = new FormAttachment(addressLabel, 5, SWT.BOTTOM);
fd_mailLabel.right = new FormAttachment(addressLabel, 310, SWT.LEFT);
fd_mailLabel.left = new FormAttachment(addressLabel, 0, SWT.LEFT);
mailLabel.setLayoutData(fd_mailLabel);
zipLabel = new Label(parent, SWT.NONE);
FormData fd_zipLabel = new FormData();
fd_zipLabel.top = new FormAttachment(label_3, -23, SWT.BOTTOM);
fd_zipLabel.bottom = new FormAttachment(label_3, 0, SWT.BOTTOM);
fd_zipLabel.right = new FormAttachment(mailLabel, 310, SWT.LEFT);
fd_zipLabel.left = new FormAttachment(mailLabel, 0, SWT.LEFT);
zipLabel.setLayoutData(fd_zipLabel);
foxLabel = new Label(parent, SWT.NONE);
FormData fd_foxLabel = new FormData();
fd_foxLabel.bottom = new FormAttachment(label_4, 23, SWT.TOP);
fd_foxLabel.top = new FormAttachment(label_4, 0, SWT.TOP);
fd_foxLabel.right = new FormAttachment(zipLabel, 310, SWT.LEFT);
fd_foxLabel.left = new FormAttachment(zipLabel, 0, SWT.LEFT);
foxLabel.setLayoutData(fd_foxLabel);
//
createActions();
initializeToolBar();
initializeMenu();
}
/**
* Create the actions
*/
private void createActions() {
// Create the actions
}
/**
* Initialize the toolbar
*/
private void initializeToolBar() {
IToolBarManager tbm = getViewSite().getActionBars().getToolBarManager();
}
/**
* Initialize the menu
*/
private void initializeMenu() {
IMenuManager manager = getViewSite().getActionBars().getMenuManager();
}
@Override
public void setFocus() {
// Set the focus
}
public void setRelation(Relation relation){
System.out.println(relation.getPhone());
if(relation != this.relation){
//测试是否得到phoneLabel的值
System.out.println(phoneLabel.getText());
phoneLabel.setText(relation.getPhone());
mailLabel.setText(relation.getMail());
addressLabel.setText(relation.getAddress());
zipLabel.setText(relation.getZip());
foxLabel.setText(relation.getFox());
}
}
public Relation getRelation() {
return relation;
}
}