0% found this document useful (0 votes)
119 views22 pages

Java GUI Interface Design Basics

The document discusses basic Java interface design and provides examples of defining classes and objects. It includes: 1. Defining a Rectangle class with properties like height and width, a constructor, and methods like findArea(). 2. Creating a TestRectangle class with a main method that instantiates a Rectangle object and calls its findArea() method. 3. Illustrates how to define GUI components like buttons and text fields, add event listeners, and write an event handling method to process button clicks and perform calculations with input values.

Uploaded by

api-26793394
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
119 views22 pages

Java GUI Interface Design Basics

The document discusses basic Java interface design and provides examples of defining classes and objects. It includes: 1. Defining a Rectangle class with properties like height and width, a constructor, and methods like findArea(). 2. Creating a TestRectangle class with a main method that instantiates a Rectangle object and calls its findArea() method. 3. Illustrates how to define GUI components like buttons and text fields, add event listeners, and write an event handling method to process button clicks and perform calculations with input values.

Uploaded by

api-26793394
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd

Basic Java – Interface design

Understand:

How to define classes and objects


How to create a GUI interface
How event-driven programming works
How classes inherit methods
Overall program structure

How to use TextPad for Java


Classes or Objects in Java

Illustrate with Rectangle object

Class definition of rectangle


including constructor, properties, methods

Driver or Test program that creates instance of rectangle


class= blueprint for object

class Rectangle properties or


instance variables
{
int height , width ;
constructor
public Rectangle (int h, int w )
{
height = h;
width = w;
}
methods

int findArea ( )
{
return height * width ;
}

int getHght ( ) { return height }

}
Driver or Test program
saved in file
[Link]

public class TestRectangle


{ constructor
public static void main ( String [ ] args )
{
instance r Rectangle r ;
of rectangle
r = new Rectangle (2, 4) ;

int area = [Link] ( );

[Link] ( "Area is: " + area )


}

} say:
r’s findArea
public class Rectangle
{
public static void main ( String [ ] args )
{
Rectangle r ;

r = new Rectangle (2, 4) ;

int area = [Link] ( );

[Link] ( "Area is: " + area ) ;


another
instance Rectangle s = new Rectangle (5, 10) ;

[Link] ( "Area is: " + [Link] ( ) );

}
}
Defining & Executing

a window-like object
Import Java
import [Link].* ; class libraries
import [Link].* ; to be used
import [Link].* ;

public class X extends JFrame


{

X is a JFrame
} ...and maybe more

currently...
just a shell
import [Link].* ;
import [Link].* ; class X
import [Link].* ;
file [Link]

public class X extends JFrame


{
needs a main
program to run

Compile: ctrl-1 [ in TextPad ]


Execute: ctrl-2 " "
not this class !
Empty
JFrame
main
import [Link].* ;
import [Link].* ; main program

public class TestX


{
public static void main (String [ ] args )
{ creates instance m
X m = new X() ; like: int k = 3;
[Link](true) ;
[Link](200, 200) ; JFrame methods
}
// [Link]();
}
For dos IO or stalling program
- requires MyInput class
Summary

class X - blueprint for object


import - provides methods for object

extends JFrame - X can use [inherits] JFrame methods

naming - class X in file [Link]


TextPad - can compile separately; but run main

Main - public static void main (String [ ] args)


create instance - X m = new X( ) ;
frame methods - setVisible(true) & setSize(200, 200)
DOS IO - [Link] ( ) ;
Calculator example

inputs multiply
Plan of Attack for calculator object

Structure Java

Containers: content pane getContentPane ( )

Containers: a canvas JPanels

Make input fields & place in panel JTextFields

Make buttons trigger actions JButtons

Define handler for button events actionPerformed

Acquire, convert and store field data


2. ContentPane 1. JFrame

3. JPanel

hello

button

4. JTextField JButton
JFrame

Title Bar Content Pane

Panel

JLabel JTextField JButton


import [Link].*;
import [Link].*;
import [Link].*;
Constructor Y() panels
public class Y extends JFrame
{
JPanel p;

Declare panel p
public Y()
{
p = new JPanel() ;
Create panel p
[Link]().add(p) ;

Add p to pane

Panels can contain GUI


objects like buttons

}
import [Link].*;
import [Link].*;
import [Link].*;
Fields & Buttons
public class Y extends JFrame implements ActionListener
{
JPanel p;
JTextField n1, n2, n3;
JButton b1,b2; declare fields & buttons
public Y()
{
p = new JPanel();
[Link]().add(p);

n1=new JTextField(10); [Link](n1); create fields & add to p


n2=new JTextField(10); [Link](n2);
n3=new JTextField(10); [Link](n3);

b1=new JButton("+"); [Link](b1); create buttons & add to p


b2=new JButton("*"); [Link](b2);
}

}
import [Link].*;
import [Link].*; says class has events
Events
import [Link].*;

public class Y extends JFrame implements ActionListener


{
JPanel p;
JTextField n1, n2, n3 ;
JButton b1, b2 ;
public Y()
{ p = new JPanel() ;
[Link]().add(p) ;

n1=new JTextField(10); [Link](n1);


n2=new JTextField(10); [Link](n2);
n3=new JTextField(10); [Link](n3);
b1=new JButton("+") ; [Link](b1);
b2=new JButton("*") ; [Link](b2);

[Link](this); make buttons listen


[Link](this); for events
}

public void actionPerformed (ActionEvent bert) template for


{
event handler

}
}
import [Link].*;
import [Link].*;
import [Link].*;

public class Y extends JFrame implements ActionListener


{ event handler template
JPanel p;
JTextField n1, n2, n3 ;
JButton b1, b2 ;
public Y()
{ p = new JPanel() ;
[Link]().add(p) ;

n1=new JTextField(10); [Link](n1);


n2=new JTextField(10); [Link](n2);
n3=new JTextField(10); [Link](n3);
b1=new JButton("+") ; [Link](b1);
b2=new JButton("*") ; [Link](b2);

[Link](this); get/convert data


[Link](this);
}
public void actionPerformed (ActionEvent bert )
{
int num1=[Link]([Link]()) ; + or - depending on
int num2=[Link]([Link]())
int num3=0;
; source of event
if ([Link]()==b1) { num3=num1 + num2 ;}
if ([Link] ()==b2) { num3=num1 * num2 ;}

[Link] ( [Link] ( num3 ) ) ;

} convert & back/store


}
import [Link].*;
import [Link].*;
import [Link].*;
class Y definition
public class Y extends JFrame implements ActionListener
{
JPanel p;
JTextField n1, n2, n3 ;
JButton b1, b2 ; Global declarations
public Y()
{ p = new JPanel() ;
[Link]().add(p) ;

n1=new JTextField(10); [Link](n1);


n2=new JTextField(10); [Link](n2); Constructor Y ( )
n3=new JTextField(10); [Link](n3);
b1=new JButton("+") ; [Link](b1);
b2=new JButton("*") ; [Link](b2);

[Link](this);
[Link](this);
}

public void actionPerformed (ActionEvent bert)


{
Event-handler
int num1=[Link]([Link]()) ;
int num2=[Link]([Link]()) ;
int num3=0;
if ([Link]()==b1){num3=num1 + num2 ;}
if ([Link]()==b2){num3=num1 * num2 ;}
[Link] ( [Link](num3) ) ;
}
}
Terms and Concepts

TextPad JPanels
Classes JPanel’s add method
Objects this notation
Methods ActionEvent
Instances of objects ActionEvent’e getSource() method
JFrame’s setSize method
Event-driven programming
JFrame’s setVisible method
Asynchronous import statements
Inheriting properties & methods Multiple instances
extend class Program structure
Constructors
Method signature
JFrames
GUI components Color objects
RGB representation
JTextFields
JPanel’s setBackground ( c) method
JButtons
JLabels dos IO – [Link] ( )

You might also like