Graphical User Interface (GUI)
A graphical user interface makes using an application much easier
and more visually appealing. As opposed to a text based console
program, a gui requires a little more coding. We need to figure
out that interface.
I have written a library (this year 2015) that will allow you to get past some limitations of swing. Here are the instructions and download for that library.
In Java, the main library of gui components (like menus, textfields,
buttons, etc [See here
for a list of them.]) is called Swing. We need to deal with creating
and adding the component and also managing events (if someone uses
them). Usually we will use BTextField (for Borland) and JButton
SAMPLE STARTER
CODE
An example is below
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.image.*;
import java.util.Scanner;
/**
* This class practices using a GUI
*
* @author (your name)
* @version (a version number)
*/
public class GuiStarter extends BorlandBase
{
//We have 3 swing components that we must declare up here, so every method has access to them
BTextField textField = new BTextField(20);
JButton lowerCase= new JButton ("To Lower");
JButton upperCase = new JButton ("To Upper");
/**
* This method will be called one time at the beginning of your applet/program
*/
public void initialize()
{
addItem(textField);
addItem(lowerCase);
addItem(upperCase);
}
/**
* This method gets called if the button is pressed
* c is the component (button usually) that was pressed
*/
public void buttonPressed (Component c)
{
echo ("action being performed"); //this is just like system.out.println - but shorter
if (c == lowerCase)
{
String text=textField.getText();
String lower=text.toLowerCase();
textField.setText(lower);
}
if (c == upperCase)
{
String text=textField.getText();
String caps=text.toUpperCase();
textField.setText(caps);
}
}
/**
* This method if you would like to do drawing on your screen
*/
public void paintScreen (Graphics g)
{
}
public static void runAsApplet()
{
try{ BorlandBase.runAsApplet((BorlandBase)(new Object() { }.getClass().getEnclosingClass().newInstance())); }
catch (Exception e){e.printStackTrace();}
}
public static void main(String[] args) {
try{ BorlandBase.main((BorlandBase)(new Object() { }.getClass().getEnclosingClass().newInstance())); }
catch (Exception e){e.printStackTrace();}
}
}
Layout
Where the components are placed is a little tricky in Java. For now , we will just let components go where Java puts them.
Create a new project called GUI and create classes for each of the following assignments. (do 3 of 5 in pairs)
- Use the GuiStarter above but rename the class HideGame and instead of toUpper and to lower,
the buttons will say hide or show which will hide the word or
show the word.
- Create a gui called PSAccount with 3 textboxes and a button.
The first two text boxes will be for first name and then last
name. When they hit the submit button, the three textbox will
then create a Portland Schools account (which is the first 5 of
last name and then the first letter of first name).
- Create a gui called Counting that will have a textfield and a button. The textfield will start at 0. Everytime they hit the button the number will go up by one.
- Create a gui called Average with 2 textboxes and a button One textbox will be their grade and one will be their average (which you calculate). Each time they hit the button, it will update their average with that grade. so if they put in 50, and hit the button, average would be 50. If they then put in 100, hit button, average would be 75. If they then put in 60, and hit button, it would average to 70.
- Create a gui called NextNum that will have a textarea and a button and each time the button is pressed another line is added to the textarea (myTextArea.append("sdfsdf\n"); //where \n is for new line). The line will say "This is line 1" then "This is line 2")
- Create a gui called ConvertUnits where there will be 2 textfields and a button that will convert between inches and centimeters. If they put in inches, it will convert to centimeters. If they put in cm it will convert to inches.
- Create a gui called DrawCircle that will have a textfield and
a button. The textfield will take in how large a circle you will
draw and the button will be called draw and it will draw that
circle. Advanced, instead of a textfield for size, use a slider. Also use a dropdown for color.
- to access graphics to draw you can say: Graphics g=getGraphics();
- now you can say g.drawLine(.... or anything to g
Some other gui objects if you want to try:
JLabel labelA=new JLabel("some text");
JSlider slider= new JSlider(JSlider.HORIZONTAL,1,10,1);
JTextArea textArea= new JTextArea(3,20); //for a text area
JProgressBar = new JProgressBar(1,10); //for a progress bar
//FOR A MENU:
JMenuBar menuBar = new JMenuBar();
menuBar.add(new JMenu("A Menu"));
menuBar.add(new JMenu("Two"));
menuBar.add(new JMenu("Three"));
setJMenuBar(menuBar);
|