UCSCBaskin School of Engineering  
General Information Events, News & Organizations Degrees & Departments Research Classes Admissions & Advising People & Jobs Administration
CMPS 012A/L - Week 10 Lab

Simple GUI in Java

For the week of Mar 13 - 16, 2006

GUI is a fairly common acronym in the computer world that stands for Graphical User Interface. Every program we've made so far has all been textual. Originally, text-based programs were the only easy programs to write, but with the advancement of the computer came the advancement of the user interface. The user interface is anything that you, the user, has to interact with--this includes the mouse and keyboard as input as well as the monitor and sound as output. We're used to seeing graphical programs with buttons and menus that we can click on with our mouse, but it's the graphical part of the program that takes just as much time as the logic part of the program to write and debug.

Today we're going to convert a simple game called In Between from a working text version to a GUI version. When you begin to modify the code, you'll see that there is actually more code that makes the GUI work than code that makes the program logic work.

Additional Information

Read chapter 3 of Java by Dissection starting on page 125 about Java applets for some more information.

Procedure

You may do this lab on your Windows computer or any other computer that's set up to compile and run Java.
  1. Download InBetween.jar and InBetween.java--right-click on the link and select Save Target.... DO NOT COPY AND PASTE THE JAVA CODE!!!
  2. Make a new directory to store the three files needed to make the GUI, then download using right-click and Save Target the following files:
  3. Find out what InBetween does!
    1. Open up the InBetween specifications to find out what the game is supposed to do. (It will open in a new window automatically.)
    2. To see what the GUI program is supposed to look like, use java -jar inBetween.jar to run the jar of the GUI program that's already been built. You may also be able to run it by double-clicking it. Note: You cannot run the GUI while logged in via SSH.
    3. You can also compile and run InBetween.java, which is the text-version of the program.
  4. Once you have an idea of what the program does, you can start working on making your version work. You need to add in some of the program logic into InBetweenLogicStarter.java.
    • In the actionPerformed() function, there are four actions that need to be coded by you.
    • Each line of comments relates to a few lines of code (often condensible to one line) or an if/else statement.
    • Use the text-version of InBetween (which uses slightly different variable names) to try and figure out what to do in the GUI-version.
    • Hint: The private variables at the bottom of InBetweenLogicStarter.java have no direct relation to the text inside the text boxes. If I had a text box called myTextBox and an integer value called myNumber, then to set the text in myTextBox to the value in myNumber use...

      myTextBox.setText(String.valueOf(myNumber)); // myTextBox <-- myNumber

      ...and to set myNumber equal to the value in myTextBox you would use...

      myNumber = Integer.parseInt(myTextBox.getText()); // myNumber <-- myTextBox

  5. To compile and run your GUI program...
    1. First compile InBetweenLogicStarter.java with javac InBetweenLogicStarter.java
    2. Once you get the logic file compiling, you have to also compile InBetweenGUI.java with javac InBetweenGUI.java. It WILL give you a message about it using deprecated API, but it's safe to ignore that.
    3. You can now try to run your code with java InBetweenGUI. Note: You cannot run your GUI program via SSH.
    4. If you make more changes to the logic file, you only need to recompile that one file, then run the GUI with the same command above--basically skip step #2 after you do it the first time and repeat steps #1 and #3 each time you change InBetweenLogicStarter.java.

General info · News · Events · Degree Programs · Research · Classes · Admissions · Advising · People · Jobs · Administration
SOE Webmail · SOE SSH · SOE Wiki · Search · Sitemap · Contact us · Driving directions · Privacy · UCSC
© Baskin School of Engineering, University of California, Santa Cruz
1156 High St., Santa Cruz, CA 95064 · (831) 459-2158 · webmaster@soe.ucsc.edu