Create a basic GUI
From BUG Wiki
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.
- If necessary, switch Perspective to the Dragonfly perspective.
- Select the New BUG Project icon in the toolbar.
- In the New BUG Project window, in the Name field, enter: OneButtonAWT
- Click Next.
- Click Start Virtual BUG
- Once the Virtual Bug has started, right click on one of the slots and select LCD
- Under Target BUG select Virtual BUG
- 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
- Click Finish
- Right click on the OneButtonAWT project in the package explorer, and select New->Other->Class
- Click Next.
- Under package enter onebuttonawt.app
- Under name enter OneButtonAWTApp
- Click Finish
- In left-side Project Explorer view, expand the node for the OneButtonAWT application
- Expand the node for the onebuttonawt.servicetracker package
- Open the OneButtonAWTServiceTracker.java file
- 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.*;
- Near the top of the OneButtonAWTServiceTracker class add the variable
private IModuleDisplay display;
- Under the doStart() method of OneButtonAWTServiceTracker add the following code
display = (IModuleDisplay) getService(IModuleDisplay.class); OneButtonAWTApp app = new OneButtonAWTApp(display);
- 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;
- 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();
}
}
}
- Go to File > Save all
- Click Run As and choose Virtual BUG
- Right click on a slot on the virtual bug and choose the LCD module
- To see a working copy of this code go here
- Once comfortable with this example, look at the AWTTutorialForBUG application which shows all of the AWT objects
