GUI-Frames - nsf-copire

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

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

63 εμφανίσεις

GUI
-
Frames

A GUI for a Java program can be created by
extending the class
JFrame.



The
JFrame

class holds the basic GUI
functionality.


You extend this class by adding the functions
your
application

needs..

JFrames


The JFrame class.



Extending the JFrame class



The
paint()

method.



The
drawString()

method
.

JFrames


Graphical Components

that make
up the Graphical User Interface.



Listener methods


that receive the
events and respond to them.


Application methods


that do useful
work for the user.


1st Frame Prog1


import java.awt.*;


import javax.swing.*;


public class TestFrame1


{



public static void main ( String[] args )



{



JFrame frame = new JFrame ("Test Frame 1");



frame.setSize(200,100);



frame.setVisible
(
true

);



}


}

1
Is this program an applet
or Application?


2.
What will be the output
for this program?


JFrames


The program is an application. It is
not called by a Web browser.




It has a main() method where it starts
running


import java.awt.*; //
1. import the application

windowing toolkit


import javax.swing.*; //
2. import the Swing classes


public class TestFrame1


{



public static void main ( String[] args )
//// usual
main() method



{



JFrame frame
// 4. a reference to the
object



= new JFrame("Test Frame 1");
// 3. construct a
JFrame object



frame.setSize(200,100); //
5. set it to 200 pixels
wide by 100 high



frame.setVisible( true ); }//
6. ask it to become
visible on the screen


}




When the program is running:


How many objects are there?


Where is the main() method?

Answer:





How many objects are there?
One



Where is the main() method?
It is a
static method of the class TestFrame1


Ist half of the 2nd program
-
Frame


import java.awt.*;


import javax.swing.*;


class MyFrame extends JFrame{


// paint() is called automatically by the system



// to display your customizations to the
frame.




public void paint ( Graphics g ){



// draw a String at location x=10 y=50



g. drawString ("A MyFrame object", 10,
50 ); }



}

2
nd

half of the 2
nd

program


public class TestFrame2 {



public static void main ( String[] args ) {



MyFrame frame = new MyFrame();
//
construct a MyFrame object



frame. setSize( 150, 100 );
// set it to 150
wide by 100 high



frame.setVisible( true );
// ask it to become
visible



} //
paint() is indirectly
called



}

Fill in the blanks: events,*;, drawString, setSize


WindowAdapter, Systwm.exit, setVisible


import java.awt.*;


import java.awt.________;


import javax.swing.*;


class myFrame extends JFrame {



public void paint ( Graphics g ) {



g._____________ ("Click the close button", 10, 50 );



}


}


class WindowQuitter extends ______________


{



public void windowClosing( WindowEvent e ) {



____________( 0 );



}


}

These are 3 sample programs: