SampleAction类:
package com.example.myhello.actions;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.window.IShellProvider;
import org.eclipse.jface.window.Window;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.IWorkbenchWindowActionDelegate;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.MessageDialog;
/**
* 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;
private IShellProvider is = null;
/**
* 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) {
DialogSample dialog = new DialogSample(null);
// System.out.println("asdfasdfadfadf");
dialog.setTextValue("我也爱eclipse");
if (dialog.open() == Window.OK) {
System.out.println("新的值是:" + dialog.getTextValue());
}
}
/**
* Selection in the workbench has been changed. We can change the state of
* the 'real' action here if we want, but this can .ly 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;
}
}
DialogSample类:
package com.example.myhello.actions;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.window.IShellProvider;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class DialogSample extends Dialog {
private String textValue = "我爱JAVA";
// 将Text设为类变量,否则buttonPressed方法无法访问 private Text text;
private Text text;
protected DialogSample(Shell parentShell) {
super(parentShell);
// TODO Auto-generated constructor stub
}
protected Control createDialogArea(Composite parent) {
getShell().setText("标题");
// 设置Dialog的标头
text = new Text(parent, SWT.BORDER);
// 设置一个Text控件
text.setText(textValue);
// 设置text中的内容
return parent;
}
protected void buttonPressed(int buttonId) {
// 如果是点了OK按钮,则将值设置回类变量
if (buttonId == IDialogConstants.OK_ID)
textValue = text.getText();
super.buttonPressed(buttonId);
}
public String getTextValue() {
// 增加两个类变量的设值取值方法
return textValue;
}
public void setTextValue(String string) {
textValue = string;
}
protected int getShellStyle() {
return super.getShellStyle() | SWT.RESIZE | SWT.MAX;
}
}
昨天调用菜单的时候总是有问题. SampleAction类里的 dialog.open()==Window.OK 总是不相等.一直没弄明白是怎么回事.还以为是哪里出了问题.原来我把DialogSample类中的createDialogArea方法中的变量text,又重新new了一下.new text = new Text(parent, SWT.BORDER);写成了这样.这样的话,就会有问题了.造成了dialog.open()==Window.OK 的值不相等.下次记住一定不要这么马虎了
package com.example.myhello.actions;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.window.IShellProvider;
import org.eclipse.jface.window.Window;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.IWorkbenchWindowActionDelegate;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.MessageDialog;
/**
* 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;
private IShellProvider is = null;
/**
* 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) {
DialogSample dialog = new DialogSample(null);
// System.out.println("asdfasdfadfadf");
dialog.setTextValue("我也爱eclipse");
if (dialog.open() == Window.OK) {
System.out.println("新的值是:" + dialog.getTextValue());
}
}
/**
* Selection in the workbench has been changed. We can change the state of
* the 'real' action here if we want, but this can .ly 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;
}
}
DialogSample类:
package com.example.myhello.actions;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.window.IShellProvider;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class DialogSample extends Dialog {
private String textValue = "我爱JAVA";
// 将Text设为类变量,否则buttonPressed方法无法访问 private Text text;
private Text text;
protected DialogSample(Shell parentShell) {
super(parentShell);
// TODO Auto-generated constructor stub
}
protected Control createDialogArea(Composite parent) {
getShell().setText("标题");
// 设置Dialog的标头
text = new Text(parent, SWT.BORDER);
// 设置一个Text控件
text.setText(textValue);
// 设置text中的内容
return parent;
}
protected void buttonPressed(int buttonId) {
// 如果是点了OK按钮,则将值设置回类变量
if (buttonId == IDialogConstants.OK_ID)
textValue = text.getText();
super.buttonPressed(buttonId);
}
public String getTextValue() {
// 增加两个类变量的设值取值方法
return textValue;
}
public void setTextValue(String string) {
textValue = string;
}
protected int getShellStyle() {
return super.getShellStyle() | SWT.RESIZE | SWT.MAX;
}
}
昨天调用菜单的时候总是有问题. SampleAction类里的 dialog.open()==Window.OK 总是不相等.一直没弄明白是怎么回事.还以为是哪里出了问题.原来我把DialogSample类中的createDialogArea方法中的变量text,又重新new了一下.new text = new Text(parent, SWT.BORDER);写成了这样.这样的话,就会有问题了.造成了dialog.open()==Window.OK 的值不相等.下次记住一定不要这么马虎了
转载于:https://blog.51cto.com/chenying/123414