刚开始接触Eclipse插件开发。
调布局也好,直接size也好。我都试了不行。我想把TreeView和文本框宽度和Dialog一样。
下面那个TreeView的高度大一点,弄得适合。ClassConstructionDialog.javapackage test.plugintreeview.actions;import java.awt.Dimension;
import java.awt.Toolkit;import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;import org.eclipse.swt.widgets.Shell;
public class ClassConstructionDialog extends Dialog { protected ClassConstructionDialog(Shell parentShell) {
super(parentShell);
// TODO Auto-generated constructor stub
} protected Point getInitialSize() {
Point p = super.getInitialSize();
p.x = 220;
p.y = 600;
return p;
} protected Point getInitialLocation(Point initialSize) {
Point size = getInitialSize();
Point p = super.getInitialLocation(size);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
p.x = (d.width - size.x) / 2;
p.y = (d.height - size.y) / 2;
return p; } // protected Layout getLayout() {
// return new FillLayout();
// } protected int getShellStyle() {
return super.getShellStyle() | SWT.RESIZE;
// | SWT.MAX;
} protected Control createDialogArea(Composite parent) {
Shell shell = this.getShell();
shell.setText("JUnit Ext"); Composite c = new Composite(parent, SWT.NONE);
// Composite c = parent;
// c.setLayout(new GridLayout(1, false));
RowLayout rowLayout = new RowLayout(SWT.VERTICAL); // 默认是横向排,加一个参数SWT.VERTICAL则是竖向排
rowLayout.marginTop = 10; // 离上边的间距
// c.setLayout(new FillLayout());
// c.getShell().setSize(200, 450);
c.setLayout(rowLayout);
//
// c.setSize(200, 450);
// Point p = c.getShell().getSize();
// p.x = 200;
// p.y = 450;
// c.getShell().setSize(p);
// c.setBounds(0, 0, 220, 450);
StyledText text = new StyledText(c, SWT.BORDER);
text.setText("java.lang.String");
TreeViewer tv = new TreeViewer(c, SWT.BORDER);
tv.setContentProvider(new MyContentProvider());
tv.setLabelProvider(new MyLableProvider());
tv.setInput(new Object());// 设置输入对象的方法与前面相同
return parent;
} /**
* 内容提供器。由它决定那些对象应该输出在TreeViewer里显示
*/
private static final class MyContentProvider implements
ITreeContentProvider {
/**
* 由这个方法决定树的顶级显示那些对象。在此方法里生成了三个Country对象
*
* @param inputElement
* 用tv.setInput()方法输入的那个对象,在这里没有使用这个对象
*/
public Object[] getElements(Object inputElement) {
String[] obj = new String[3];
obj[0] = "中国";
obj[1] = "美国";
obj[2] = "英国";
return obj;
} /**
* 由这个方法决定结点应该显示那些子结点。在这里也不管父结点是什么,每个结点都统一有三个字结点
*
* @param parentElement
* 被点击的结点(父结点)
*/
public Object[] getChildren(Object parentElement) {
String[] obj = new String[4];
obj[0] = "桂林";
obj[1] = "北京";
obj[2] = "纽约";
obj[3] = "伦敦";
return obj;
} /**
* 判断某结点是否有子结点。在这里不管3721,全返回真,即都有子结点。这时结点前都有一个“+”号图标
*
* @param element
* 需要判断是否有子的结点
*/
public boolean hasChildren(Object element) {
return true;
} /**
* 取得某结点的父结点。极少需要实现此方法
*/
public Object getParent(Object element) {
return null;
} /**
* 当TreeViewer被销毁时将执行这个方法。极少需要实现此方法
*/
public void dispose() {
} /**
* 当tv.setInput()发生变化时调用此方法。极少需要实现此方法
*/
public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
}
} /**
* 标签提供器。主要负责每一单个对象在TreeViewer中的显示(文字、图象)
*/
private static final class MyLableProvider implements ILabelProvider { /**
* 显示什么图片
*
* @param 结点
* @return 可以为null值
*/
public Image getImage(Object element) {
// ISharedImages sharedImages =
// PlatformUI.getWorkbench().getSharedImages();
// return sharedImages.getImage(ISharedImages.IMG_OBJS_WARN_TSK);
return null;
} /**
* 显示什么文字
*
* @param 结点
* @return 不能为null值
*/
public String getText(Object element) {
return (String) element; } public void addListener(ILabelProviderListener listener) {
} public void dispose() {
} public boolean isLabelProperty(Object element, String property) {
return false;
} public void removeListener(ILabelProviderListener listener) {
}
} /**
* 标签提供器。如果你赚上面的MyLableProvider那些没用的方法碍眼,
* Eclipse中有一个ILabelProvider的缺省实现LabelProvider,继承它也行
*/
// private static final class MyLableProvider extends LabelProvider {
// public String getText(Object element) {
// return (String)element;
// }
// }
}
SampleAction.javapackage test.plugintreeview.actions;import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.IWorkbenchWindowActionDelegate;
import org.eclipse.jface.dialogs.MessageDialog;import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;/**
* Our sample action implements workbench action delegate.
* The action proxy will be created by the workbench and
* shown in the UI. When the user tries to use the action,
* this delegate will be created and execution will be
* delegated to it.
* @see IWorkbenchWindowActionDelegate
*/
public class SampleAction implements IWorkbenchWindowActionDelegate {
private IWorkbenchWindow window;
/**
* The constructor.
*/
public SampleAction() {
} /**
* The action has been activated. The argument of the
* method represents the 'real' action sitting
* in the workbench UI.
* @see IWorkbenchWindowActionDelegate#run
*/
public void run(IAction action) {
// MessageDialog.openInformation(
// window.getShell(),
// "TestPluginTreeView",
// "TestPluginTreeView");
ClassConstructionDialog ccDialog = new ClassConstructionDialog(window.getShell());
ccDialog.open();
} /**
* Selection in the workbench has been changed. We
* can change the state of the 'real' action here
* if we want, but this can only happen after
* the delegate has been created.
* @see IWorkbenchWindowActionDelegate#selectionChanged
*/
public void selectionChanged(IAction action, ISelection selection) {
} /**
* We can use this method to dispose of any system
* resources we previously allocated.
* @see IWorkbenchWindowActionDelegate#dispose
*/
public void dispose() {
} /**
* We will cache window object in order to
* be able to provide parent shell for the message dialog.
* @see IWorkbenchWindowActionDelegate#init
*/
public void init(IWorkbenchWindow window) {
this.window = window;
}
}
试试setPreferredSize(preferredSize)方法:
例子:
JPanel p = new JPanel();
p.setPreferredSize(new Dimension(500,500));
第二种:
例子:
JPanel p = new JPanel();
p.setSize(new Dimension(500,500));
你也可以不写getInitialSize()方法
重写configureShell方法,像这样:
protected void configureShell(Shell newShell) {
newShell.setSize(450, 450);
}
改成
TreeViewer tv = new TreeViewer(c, SWT.BORDER);
GridData gd = new GridData(GridData.FILL_BOTH);
tv.getTree().setLayoutData(gd); // 上下自适应
Eclipse自带的帮助里有关于SWT和布局的相关说明,
也可以到官网去搜索一下,有一篇相关的文章
GridLayout layout= new GridLayout();
composite.setLayout(layout);
composite.setLayoutData(new GridData(GridData.FILL_BOTH));然后
TreeViewer tv = new TreeViewer(composite, SWT.BORDER);
GridData gd = new GridData(GridData.FILL_BOTH);
tv.getTree().setLayoutData(gd); // 上下自适应