📄 addgroupdialog.java
字号:
package mynews;
import java.awt.Toolkit;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;
import org.eclipse.jface.dialogs.MessageDialog;
import com.swtdesigner.SWTResourceManager;
public class AddGroupDialog extends Dialog {
private Text group;
protected Object result;
protected Shell shell;
/**
* Create the dialog
* @param parent
* @param style
*/
public AddGroupDialog(Shell parent, int style) {
super(parent, style);
}
/**
* Create the dialog
* @param parent
*/
public AddGroupDialog(Shell parent) {
this(parent, SWT.NONE);
}
/**
* Open the dialog
* @return the result
*/
public Object open() {
createContents();
shell.open();
shell.layout();
Display display = getParent().getDisplay();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
return result;
}
/**
* Create contents of the dialog
*/
protected void createContents() {
shell = new Shell(getParent(), SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
shell.setSize(259, 127);
Toolkit kit = Toolkit.getDefaultToolkit();
shell.setLocation((kit.getScreenSize().width - 259) / 2, (kit.getScreenSize().height - 127) / 2);
shell.setText("Add Group");
final Label groupNameLabel = new Label(shell, SWT.NONE);
groupNameLabel.setText("Group Name:");
groupNameLabel.setBounds(16, 28, 78, 14);
group = new Text(shell, SWT.BORDER);
group.setBounds(100, 25, 129, 22);
final Button okButton = new Button(shell, SWT.NONE);
okButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(final SelectionEvent e) {
if (group.getText() == null || group.getText().equals("")){
MessageDialog.openWarning(shell, "Warning", "Complete the new group name, please!");
return;
}
Tree t = (Tree)shell.getParent().getData("tree");
for(TreeItem tmp : (java.util.Vector<TreeItem>)shell.getParent().getData("groupItems")){
if (group.getText().equals(tmp.getText())){
MessageDialog.openError(shell, "Error", "Group with the same name already exists!");
return;
}
}
TreeItem ti = new TreeItem(t, SWT.NONE);
ti.setText(group.getText());
ti.setImage(SWTResourceManager.getImage(MyNewsMain.class, "/img/mbi_023.gif"));
MessageDialog.openInformation(shell, "Adding Group", "Group added successfully!");
shell.close();
}
});
okButton.setText("OK");
okButton.setBounds(100, 68, 64, 24);
final Button cancelButton = new Button(shell, SWT.NONE);
cancelButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(final SelectionEvent e) {
shell.close();
}
});
cancelButton.setBounds(170, 68, 64, 24);
cancelButton.setText("Cancel");
//
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -