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.
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
There is a bunch of code additions to our normal applet- I would
recommend starting from the example below or the starter code below.
We are going to put them in an applet (because that is easier for you to put online and then for me to see it online). An application would be just as easy.
An example is below
//notice that I say implements actionlistener, this is to say that I will be listening for actions
public class SwingExample extends JApplet implements ActionListener
//We have 3 swing components that we must declare up here, so every method has access to them
JTextField textField = new JTextField(20);
JButton lowerCase= new JButton ("To Lower");
JButton upperCase = new JButton ("To Upper");
//init is a method that gets called automatically when the applet starts or initializes
//in init we are adding our swing components to the applet
public void init()
Container screen = getContentPane();
screen.setLayout (new FlowLayout() );
//for all components that i want to listen for an action [like a click of button]
//I add action listener saying Im going to be listening
//in paint I tell it to paint the components and then draw a line
public void paint (Graphics g)
//now we have to have a method actionPerformed that will be called if an action happens
//we then see which component had the action on it
public void actionPerformed(ActionEvent thisEvent)
Object source = thisEvent.getSource();
if (source == lowerCase)
if (source == upperCase)
when you do textField.getText() it returns a String, sometimes we want an int, to convert it you will say:
int theNumber=Integer.parseInt( textField.getText());
Integer.parseInt(str) takes in a string and returns it as an int (if possible).
Where the components are placed is a little tricky in Java. See
Absolute positioning (pixel
by pixel is here. It is beyond the scope of the class for us
Create a new project called GUI and create classes for each of the following assignments. (do 4 of 6 in pairs)
- Use the SwingExample 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 CleverPassword with 2 textboxes and a button They are to put their
name and when they hit the button it will then put a clever password
version of their name in the 2nd textbox. The clever formula can
be anything you want, one idea is to replace E or e with 3, I
or i with 1, o or O with 0. So Tim would be T1M
- 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 Avearge 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 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
- to access graphics to draw you might say: Graphics g=getGraphics();
- now you can say g.drawLine(.... or anything to g
Some other gui objects if you want to try:
screen.add(new JSlider(JSlider.HORIZONTAL,1,10,1)); //for a slider
screen.add(new JTextArea(3,20)); //for a text area
screen.add(new JProgressBar(1,10)); //for a progress bar
//FOR A MENU:
JMenuBar menuBar = new JMenuBar();
menuBar.add(new JMenu("A Menu"));