分类:
2008-09-12 12:57:52
import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.SWT; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Group; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Text; import org.eclipse.swt.widgets.*; import org.eclipse.swt.events.SelectionAdapter; import org.eclipse.swt.events.SelectionEvent; public class AddressBookUI { private Shell shell; private Text miscText; private Text addrText; private Text emailText; private Text phoneText; private Text lnameText; private Text fnameText; private Button cancelButton; private Button saveButton; private Button nextButton; private Button prevButton; private Button editButton; private Button deleteButton; private Button newButton; public static void main(String[] args) { AddressBookUI window = new AddressBookUI(); window.open(); } public void open() { final Display display = new Display(); shell = new Shell(); shell.setSize(610, 477); shell.setText("Address Book"); { newButton = new Button(shell, SWT.NONE); newButton.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { clearText(); setTextEditable(true); enableEditButtons(false); enableSaveButtons(true); System.out.println("New button selected."); } }); newButton.setBounds(10, 380, 75, 35); newButton.setText("New"); } { deleteButton = new Button(shell, SWT.NONE); deleteButton.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { clearText(); System.out.println("Delete button selected."); } }); deleteButton.setBounds(85, 380, 75, 35); deleteButton.setText("Delete"); } { editButton = new Button(shell, SWT.NONE); editButton.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { setTextEditable(true); enableEditButtons(false); enableSaveButtons(true); System.out.println("Edit button selected."); } }); editButton.setBounds(160, 380, 75, 35); editButton.setText("Edit"); } { prevButton = new Button(shell, SWT.NONE); prevButton.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { System.out.println("Previous button selected."); } }); prevButton.setBounds(265, 380, 75, 35); prevButton.setText("Previous"); } { nextButton = new Button(shell, SWT.NONE); nextButton.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { System.out.println("Next button selected."); } }); nextButton.setBounds(340, 380, 75, 35); nextButton.setText("Next"); } { saveButton = new Button(shell, SWT.NONE); saveButton.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { setTextEditable(false); enableEditButtons(true); enableSaveButtons(false); System.out.println("Save button selected."); } }); saveButton.setBounds(445, 380, 75, 35); saveButton.setText("Save"); saveButton.setEnabled(false); } { cancelButton = new Button(shell, SWT.NONE); cancelButton.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent e) { setTextEditable(false); enableEditButtons(true); enableSaveButtons(false); System.out.println("Cancel button selected."); } }); cancelButton.setBounds(520, 380, 75, 35); cancelButton.setText("Cancel"); cancelButton.setEnabled(false); } { final Group group = new Group(shell, SWT.NONE); group.setText("Details"); group.setBounds(10, 10, 585, 355); { final Label label = new Label(group, SWT.NONE); label.setBounds(10, 20, 135, 25); label.setText("First Name:"); } { final Label label = new Label(group, SWT.NONE); label.setBounds(10, 60, 135, 25); label.setText("Last Name:"); } { final Label label = new Label(group, SWT.NONE); label.setBounds(10, 100, 135, 25); label.setText("Phone:"); } { final Label label = new Label(group, SWT.NONE); label.setBounds(10, 140, 135, 25); label.setText("Email:"); } { final Label label = new Label(group, SWT.NONE); label.setBounds(10, 180, 135, 25); label.setText("Address:"); } { final Label label = new Label(group, SWT.NONE); label.setBounds(10, 255, 135, 25); label.setText("Miscellaneous Information:"); } { fnameText = new Text(group, SWT.BORDER | SWT.READ_ONLY); fnameText.setBounds(150, 15, 420, 25); } { lnameText = new Text(group, SWT.BORDER | SWT.READ_ONLY); lnameText.setBounds(150, 55, 420, 25); } { phoneText = new Text(group, SWT.BORDER | SWT.READ_ONLY); phoneText.setBounds(150, 95, 420, 25); } { emailText = new Text(group, SWT.BORDER | SWT.READ_ONLY); emailText.setBounds(150, 135, 420, 25); } { addrText = new Text(group, SWT.BORDER | SWT.READ_ONLY | SWT.V_SCROLL); addrText.setBounds(150, 175, 420, 60); } { miscText = new Text(group, SWT.BORDER | SWT.READ_ONLY | SWT.V_SCROLL); miscText.setBounds(150, 250, 420, 65); } } setupMenu(); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } } private void setupMenu() { //create the menu bar Menu menu = new Menu(shell, SWT.BAR); shell.setMenuBar(menu); //add the File option to it MenuItem file = new MenuItem(menu, SWT.CASCADE); file.setText("File"); //create a menu for the File option file.setMenu(new Menu(file)); //add MenuItems to the File menu MenuItem prevItem = new MenuItem(new Menu(file), SWT.NONE); prevItem.setText("Previous"); MenuItem nextItem = new MenuItem(new Menu(file), SWT.PUSH); nextItem.setText("Next"); MenuItem seperator = new MenuItem(new Menu(file), SWT.SEPARATOR); MenuItem quitItem = new MenuItem(new Menu(file), SWT.PUSH); quitItem.setText("Quit"); //add listeners for the actions prevItem.addListener(SWT.Selection, new Listener() { public void handleEvent(Event e) { System.out.println("Previous menu item selected."); } }); nextItem.addListener(SWT.Selection, new Listener() { public void handleEvent(Event e) { System.out.println("Next menu item selected."); } }); quitItem.addListener(SWT.Selection, new Listener() { public void handleEvent(Event e) { shell.dispose(); } }); } private void clearText() { fnameText.setText(""); lnameText.setText(""); phoneText.setText(""); emailText.setText(""); addrText.setText(""); miscText.setText(""); } private void setTextEditable(boolean editable) { fnameText.setEditable(editable); lnameText.setEditable(editable); phoneText.setEditable(editable); emailText.setEditable(editable); addrText.setEditable(editable); miscText.setEditable(editable); } private void enableEditButtons(boolean enable) { newButton.setEnabled(enable); deleteButton.setEnabled(enable); editButton.setEnabled(enable); } private void enableSaveButtons(boolean enable) { saveButton.setEnabled(enable); cancelButton.setEnabled(enable); } } |