L21 - CompSci101 Lecture 21 1 Computer Science 101 SS C Lecture 21 Contents Using standard input and output Defne a JFrame class Create an instance

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CompSci101 Lecture 21 1 Computer Science 101 SS C Lecture 21 Contents Using standard input and output. Defne a JFrame class. Create an instance o¡ the JFrame class. Defne the title and position ¡or the JFrame instance. Defne a JPanel class. Create a JPanel object and add it to the visible area o¡ the JFrame. Defne a paintComponent() method inside the JPanel class. The Graphics object - instance methods Course Book: §15 1 2 3 4 5 6 2 Standard input and output So ¡ar we have written programs which are able to process input ¡rom the keyboard ( Keyboard.readInput() ) and which are able produce output to the terminal window ( System.out.println() ). public class L21 { public void start() { } } String message = Keyboard.readInput() ; System.out.println( message ) ; 3 JFrame - is a Window, a Container We now want to write applications which create their own windows and display graphics inside the window area. A JFrame object is a window with a border and a title bar. The visible area o¡ a JFrame object is a Container which to we will be able to add components, such as a JPanel object. (Later we will use a graphical inter¡ace to obtain user input and to display output.) Firstly we want our applications to create a graphical window. 1 2 3 4 5 6 4 JFrame - preliminary defnition setVisible(true) : this ensures that the JFrame object is visible on the screen and is in ¡ront o¡ any other open windows. import javax.swing.*; public class MyJFrame extends JFrame { public MyJFrame() { } } javax.swing package: contains the defnition o¡ a JFrame class. This package needs to be imported. extends JFrame : the class has all the ¡unctionality o¡ a JFrame object e.g. a title bar and a border and many instance methods which we can use such as setTitle(), setVisible(). setVisible(true); 1 2 3 4 5 6 5 JFrame - create a JFrame object When using JFrames we will not be using a program class. Instead we create an instance o¡ a JFrame class, in this case MyJFrame, in our applications, ¡or example, import javax.swing.*; public class L21 { public static void main(String args) { } } JFrame window = new MyJFrame() ; 6 A window ¡or drawing, 1 So ¡ar the JFrame (or window) object created is blank inside. We would like now to display some drawing inside this window. Be¡ore we can do this we will need to: add the instance o¡ the JPanel to the JFrame. and defne a JPanel class, create an instance o¡ this JPanel class. CompSci101 Lecture 21 2 7 A window for drawing, 2 The JFrame instance which we create is our window with title, menu bar and border. The JPanel instance which we will add to the JFrame contains all the visible things we want to see inside the JFrame window. Once we have added a JPanel instance to the JFrame we can draw inside the JPanel area and, later still, we will add components to the JPanel....
View Full Document

This note was uploaded on 03/01/2012 for the course COMPSCI 101 taught by Professor Adrianaferraro during the Summer '12 term at Auckland.

Page1 / 6

L21 - CompSci101 Lecture 21 1 Computer Science 101 SS C Lecture 21 Contents Using standard input and output Defne a JFrame class Create an instance

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online