Create a basic GUI

From BUG Wiki

Jump to: navigation, search

Java includes three toolkits for creating Graphical User Interfaces, AWT, SWT, and Swing. BUG uses AWT as the standard toolkit. This example will draw a button, a textfield, and handle the event for the button being clicked.

  1. If necessary, switch Perspective to the Dragonfly perspective.
  2. Select the New BUG Project icon in the toolbar.
  3. In the New BUG Project window, in the Name field, enter: OneButtonAWT
  4. Click Next.
  5. Click Start Virtual BUG
  6. Once the Virtual Bug has started, right click on one of the slots and select LCD
  7. Under Target BUG select Virtual BUG
  8. Under Required Services begin typing IModuleDisplay until it filters out everything but com.buglabs.bug.module.lcd.pub.IModuleDisplay, and check the box next to that that service
  9. Click Finish
  10. Right click on the OneButtonAWT project in the package explorer, and select New->Other->Class
  11. Click Next.
  12. Under package enter onebuttonawt.app
  13. Under name enter OneButtonAWTApp
  14. Click Finish
  15. In left-side Project Explorer view, expand the node for the OneButtonAWT application
  16. Expand the node for the onebuttonawt.servicetracker package
  17. Open the OneButtonAWTServiceTracker.java file
  18. Add the following code to the import block at the top of the file
import com.buglabs.bug.module.lcd.pub.IModuleDisplay;
import onebuttonawt.app.*;
  1. Near the top of the OneButtonAWTServiceTracker class add the variable
private IModuleDisplay display;
  1. Under the doStart() method of OneButtonAWTServiceTracker add the following code
display = (IModuleDisplay) getService(IModuleDisplay.class);
OneButtonAWTApp app = new OneButtonAWTApp(display);
  1. Now open the OneButtonAWTApp.java file and add the following classes to the import block at the top of the file
import java.awt.Frame;
import java.awt.Button;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.BorderLayout;
import java.awt.Color;
  1. Change the OneButtonAWTApp.java file to look like this
//The action listener handles events such as when buttons get clicked
public class OneButtonAWTApp implements ActionListener {
	
        //The BUG's LCD module
	private IModuleDisplay display;
	
        //The main window other objects get placed in
	private Frame frame;
	
	private Button button;
	
	private Label label;
	
	public OneButtonAWTApp(IModuleDisplay display) {
		super();
		this.display = display;
		createUI();
	}

	private void createUI() {
		frame = display.getFrame();
		frame.setTitle("Example");
		frame.setBackground(Color.WHITE);
		
		button = new Button("Click me");
		label = new Label("Not clicked");
		
                //connects the button to the event handler
		button.addActionListener(this);
		
                //The frame needs to know how to place the objects that will be added
		frame.setLayout(new BorderLayout());
		
                //See AWTTutorialForBug for more info on layouts
		frame.add(label, BorderLayout.NORTH);
		frame.add(button, BorderLayout.CENTER);
		
                //Without this line, the GUI is not painted
		frame.show();
	}

	public void actionPerformed(ActionEvent event) {
		if ( event.getSource() == button ) {
			label.setText("Clicked");
			frame.show();
		}
	}
}
  1. Go to File > Save all
  1. Click Run As and choose Virtual BUG
  1. Right click on a slot on the virtual bug and choose the LCD module
  1. To see a working copy of this code go here
  1. Once comfortable with this example, look at the AWTTutorialForBUG application which shows all of the AWT objects