扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
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); } } |
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者