user interface - Java SWT: Widgets(buttons, labels) used properly -
This is my first question but I'm really stuck. Maybe it's my exorbitance for the past few days, but now I looked at Google for a few hours and could not get any closer to the good answer.
I know that SWT is running like all GUIs which I think and when creating widgets, I should keep in mind that they should be able to reach those people with whom they modify / talk Want to But I need to know that if my thinking is correct and if I am not good then what should I do?
Let's say I add Eclipse + Window Builder + swt / jface java project then I add buttons and clawl + click listener (button button), so the generated code looks more or less (main only Method, above only main square and import)
public static zero main (string [] args) {display display = Display.getDefault (); Shell shell = new shell (); Shell.set size (450, 300); Shell.setText ("SWT Application"); Button BTN Newton = new button (shell, SWT.NONE); BtnNewButton.addSelectionListener (new selection adapter) {@ Override Public Wide WidgetSwitch (SelectionEvent e) {}}); BTN New Button Setbound (51, 31, 75, 25); BtnNewButton.setText ("new button"); CLabel lblOneTwo = new collabel (shell, SWT.NONE); LblOneTwo.setBounds (180, 119, 61, 21); LblOneTwo.setText ("One Two"); Shell.open (); Shell.layout (); While (! Shell.isDisposed ()) {if (! Display.readAndDispatch ()) {display.sleep (); }}} As I know and it's probably obvious to most of you I just can not go and
btnNewButton.addSelectionListener ( New Select Adapter () {@ Override Public Zero Widget Selected (SelectionEvent e) {}}); lblOneTwo.setText ("three") And as I know and sometimes use, I declare all the things before the code, as is a static widget widget < / Code> and after that I can access them basically everywhere, therefore code like static button btnNew button; Static CLabel lblOneTwo; Public static zero main (string [] args) {display display = Display.getDefault (); Shell shell = new shell (); Shell.set size (450, 300); Shell.setText ("SWT Application"); BTnNEbutton = new button (shell, SWT.NONE); BtnNewButton.addSelectionListener (New selectionEdapter () {@ Override Public Zero widget selected (SelectionEvent e) {lblOneTwo.setText ("three");}}); (...) works fine but I think and think that this is not the best practice and the way to do it, right? So please, help me, tell me in the right direction so that I can stop coding in one sin. Thanks in advance! Indirect answers will be very good with links to articles / tutorials, but I liked the example people who are being put here because of their obvious way of displaying things.
There are several ways to do this
-
Your class Create widget field. Do not static until they are needed Private label L; Private button b; Public static zero main (string [] args) {... b.addListener (...); -
Firstly define all your widgets (if the should be the last , if you use them in listener / server )>), Then add your listener s. last label l = ...; Last button b = ....; B.addListener (...); -
If you want to change the widget to the listener then you can use the Event # Widget Find the source of the event b.addListener (SWT.Selection, new listener) {public void handleEvent (event e) {button button = (button) e.widget;}} );
Comments
Post a Comment