Lab 4: Introduction to GUI Design Using Swing & AWT

snottybugbearΛογισμικό & κατασκευή λογ/κού

3 Νοε 2013 (πριν από 3 χρόνια και 7 μήνες)

72 εμφανίσεις

CMM005: OOP Techniques 1 Lab 4: Intro to GUI Design

David G. Davidson SCMS 28/05/2003

Copyright 2001 Robert Gordon University. All rights reserved. \\galahad\homes\Modules\CMM005\Labs\lab4.doc

Lab 4: Introduction to GUI Design Using Swing &
AWT
Aims
· To introduce the JFrame class and explore how it can be used to create a "window frame" for a
Graphical User Interface (GUI).
Exercise 1: Using the JFrame Class
Type the outline application given below. We will use this application to explore features of the
JFrame class.

import java.awt.* ; // do not forget these two
import javax.swing.* ; // import statements

public class MyGUIApplication extends JFrame
{
public static void main(String[ ] args)
{
MyGUIApplication f = new MyGUIApplication( ) ;
// create a top-level window
f.setSize(400,400) ; // the size of any JFrame
// is specified in units of pixels
f.setTitle("MyGUIApplication") ;
// give the JFrame a title
f.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE) ;
// ensure that nothing will happen to frame
// if user hits "X" window control
// (normally close operation hides
// the frame when user hits "X")
f.show( ) ; // JFrame has a visible aspect as well as
// an underlying data structure used to
// contain other graphics elements
}
}

Build and run the application. Note:

The application does not respond to all the usual window controls (i.e. the "buttons" located at the
top right corner of the title bar); in particular the application can not be terminated by clicking on
the "X" window control;

this is due to the fact that the application is not designed to respond to window events
associated with the window controls;

you will need to use the Kawa "Stop Java" button to force the application to terminate.

Try deleting the call to f.setSize(400,400) ;

what is the default size for a JFrame?

Try deleting the call to f.show( ) ;

does the application still display a window?

Try replacing the call to f.show( ) ;

with the call f.setVisible(true) ;

Any program that displays a JFrame is executing at least two "threads" in parallel. (A thread is a
sequence of Java instructions):
· one thread is responsible for managing the JFrame (and all of its associated events),
· the other thread continues the execution of the main() method.
You can convince yourself that your program is running several threads in parallel:

Modify the outline application so that it generates and displays several JFrame windows.

N.B. you may need to move one or more of the windows to see the others.
CMM005: OOP Techniques 2 Lab 4: Intro to GUI Design

David G. Davidson SCMS 28/05/2003

Copyright 2001 Robert Gordon University. All rights reserved. \\galahad\homes\Modules\CMM005\Labs\lab4.doc


Modify the outline application so that the main() method continues to do some computation after it
has displayed the JFrame

e.g. after the call f.show() ; make the main() method repeatedly call
System.out.println("main is still running") ;

Find out how to change the background colour of a JFrame. Hint: refer to the Java Application
Programming Interface (API) help files, look up details of the method:
public void setBackground(Color c) ;
and the class:
java.awt.Color
In order to change the background colour of a JFrame your must set send the message setBackground()
to the content pane of the affected JFrame e.g.
Container c = f.getContentPane() ;
c.setBackground(Color.blue) ;
(For future reference, note that the same method setBackground() can be used to change the
background colour of individual graphic elements [e.g. buttons, labels, text areas etc]).

Reorganise the source code in the outline application. Define a constructor for the class
MyGUIApplication. The constructor should contain code to:

set the title of the frame

set the size of the frame

make the frame visible
Exercise 2: Displaying Text in a JFrame
Type the outline application given below. We will use this application to explore drawing capabilities
of the JFrame class.

import java.awt.* ;
import javax.swing.* ;

public class WelcomeGUI extends JFrame
{
private Font theFont = new Font("SansSerif", /*name of font*/
Font.BOLD + Font.ITALIC, /*style of font*/
24) ; /*size of font*/

// method to initialise fonts and
// colours to be used by this application
private void setupFont(Graphics g)
{
g.setFont(theFont) ;
g.setColor(Color.blue) ;
}

public void paint(Graphics g)
{
super.paint(g) ;
setupFont(g) ;
g.drawString("Welcome", 200,200) ;
}

public static void main(String[ ] args)
{
WelcomeGUI f = new WelcomeGUI( ) ;
f.setSize(400,400) ;
f.setTitle("WelcomeGUI") ;
f.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE) ;
f.show( ) ;
}
}
CMM005: OOP Techniques 3 Lab 4: Intro to GUI Design

David G. Davidson SCMS 28/05/2003

Copyright 2001 Robert Gordon University. All rights reserved. \\galahad\homes\Modules\CMM005\Labs\lab4.doc


Build and run the application.

Refer to the Java Application Programming Interface (API) help files to find out more about the:
java.awt.Font
class. Modify the application so that the welcome message is displayed in:

"Serif" font

plain style (i.e. nether bold nor italic)

font size 36

text coloured red.

Try altering the location at which the welcome message is displayed in the frame.
Exercise 3: Drawing Lines, Rectangles and Ovals in a JFrame
Type the following outline application:

import java.awt.* ;
import javax.swing.* ;

public class DrawingGUI extends JFrame
{
public DrawingGUI()
{
super() ;
setTitle("Lines, Rectangles, Ovals") ;
setSize(400,165) ;
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE) ;
show() ;
}

public void paint(Graphics g)
{
super.paint(g) ;

g.setColor(Color.red) ;
g.drawLine(5,30,350,30) ;

g.setColor(Color.blue) ;
g.drawRect(5,40,90,55) ;
g.fillRect(100,40,90,55) ;

g.setColor(Color.cyan) ;
g.fillRoundRect(195,40,90,55,50,50) ;
g.drawRoundRect(290,40,90,55,20,20) ;

g.setColor(Color.yellow) ;
g.draw3DRect(5,100,90,55,true) ;
g.fill3DRect(100,100,90,55,false) ;

g.setColor(Color.magenta) ;
g.drawOval(195,100,90,55) ;
g.fillOval(290,100,90,55) ;
}

public static void main(String[ ] args)
{
DrawingGUI f = new DrawingGUI( ) ;
}
}

CMM005: OOP Techniques 4 Lab 4: Intro to GUI Design

David G. Davidson SCMS 28/05/2003

Copyright 2001 Robert Gordon University. All rights reserved. \\galahad\homes\Modules\CMM005\Labs\lab4.doc

Refer to the Java Application Programming Interface (API) help files to find out more about the:
java.awt.Graphics
class. In particular use the help files to determine the meaning of the individual parameters passed to
the methods:

drawLine()

drawRect()

fillRect()

drawRoundRect()

fillRoundRect()

draw3DRect()

fill3dRect()

drawOval()

fillOval()

As an exercise, draw a grid of horizontal and vertical lines on a frame so that the frame is "divided up"
into 8 rows and 8 columns of equal width and height.

As another exercise, draw a chessboard pattern of alternating black and white squares, filling 8 rows
and 8 columns.