gui chapter 10 graphics context and objects creating a window based application jframe, jtextfield,...

25
GUI Chapter 10 Graphics context and objects Creating a window based application JFrame, JTextField, JButton Containers and Controls Graphics commands Layout Managers Event Handlers

Upload: emory-mathews

Post on 03-Jan-2016

223 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: GUI Chapter 10 Graphics context and objects Creating a window based application JFrame, JTextField, JButton Containers and Controls Graphics commands Layout

GUI Chapter 10

Graphics context and objectsCreating a window based applicationJFrame, JTextField, JButtonContainers and ControlsGraphics commandsLayout ManagersEvent Handlers

Page 2: GUI Chapter 10 Graphics context and objects Creating a window based application JFrame, JTextField, JButton Containers and Controls Graphics commands Layout

Terms

GIUcomponentcontainercoordinatesgraphics context

Page 3: GUI Chapter 10 Graphics context and objects Creating a window based application JFrame, JTextField, JButton Containers and Controls Graphics commands Layout

Types of interfaces

Console applications Using print and Scanner

A simple GUI Using JoptionPane

A complete GUI using a Frame Can be created using AWT Can be create using Swing

Page 4: GUI Chapter 10 Graphics context and objects Creating a window based application JFrame, JTextField, JButton Containers and Controls Graphics commands Layout

The swing versus the AWT

AWT part of JDK v1 Mapped to platform specific

components Heavyweight

Swing introduced in V2 Platform independent components Lightweight

Page 5: GUI Chapter 10 Graphics context and objects Creating a window based application JFrame, JTextField, JButton Containers and Controls Graphics commands Layout

Components

JButton tnOK = new JButton(“OK”);JLabel tnOK = new JLabel(“OK”);JTextField tfNAME = new JTextField(“Enter Name”);JCheckBox cbOK = new JTextBox(“OK”);JComboBox cmb = new JComboBox(new String[] {“Red”,”Green”,Blue”});

Page 6: GUI Chapter 10 Graphics context and objects Creating a window based application JFrame, JTextField, JButton Containers and Controls Graphics commands Layout

The Class chart

JComponent AbstractButton

Jbutton JToggleButton

JCheckBox

JTextComponent JTextField

JLabelJPanel… many others …

Page 7: GUI Chapter 10 Graphics context and objects Creating a window based application JFrame, JTextField, JButton Containers and Controls Graphics commands Layout

Classes

Container ClassesJFrameJDialogJAppletJPanelHelper Classes Graphics, Color, Font, FontMetrics LayoutManager

Page 8: GUI Chapter 10 Graphics context and objects Creating a window based application JFrame, JTextField, JButton Containers and Controls Graphics commands Layout

Frames

Public class myframe { public static void main(String[] args) { JFrame yframe = new JFrame(“MyFrame”); myframe.setsize(300,300); myframe.setvisible=true; myframe.setdefaultcloseoperation(Jframe.EXIT_ON_CLOSE); }}

Page 9: GUI Chapter 10 Graphics context and objects Creating a window based application JFrame, JTextField, JButton Containers and Controls Graphics commands Layout

Frames with components(limited example)

Import javax.swing.*;

Public class myframe { public static void main(String[] args) { JFrame yframe = new JFrame(“MyFrame”);

java.awt.Container mycnt = frame.getContentPane(); Jbutton btOK = new JButton(“OK”); mycnt.add(btOK);

myframe.setsize(300,300); myframe.setvisible=true; myframe.setdefaultcloseoperation(Jframe.EXIT_ON_CLOSE); }}

Page 10: GUI Chapter 10 Graphics context and objects Creating a window based application JFrame, JTextField, JButton Containers and Controls Graphics commands Layout

Getting the screen size

Dimension ScrS = Toolkit.getDefaultToolkit().getScreenSize();int wid = screensize.width;int hgt = screensize.height;

This can be used to center or dynamically size frame.

Frame.setlocation(x,y);Frame.setwidth(wid);Frame.sethight(hgt);

Page 11: GUI Chapter 10 Graphics context and objects Creating a window based application JFrame, JTextField, JButton Containers and Controls Graphics commands Layout

Layout Managers

Use to determine how components will be positioned on the frame or panel.To create and apply a layout manager to a component.LayoutManager Lm = new FlowLayout();Frame.setLayout(LM);

Page 12: GUI Chapter 10 Graphics context and objects Creating a window based application JFrame, JTextField, JButton Containers and Controls Graphics commands Layout

Type of layout managers

FlowGridBorderOthers ….

Properties.setHGap(10);.setVGap(10);.setAlignment(FlowLayout.RIGHT);

Page 13: GUI Chapter 10 Graphics context and objects Creating a window based application JFrame, JTextField, JButton Containers and Controls Graphics commands Layout

Creating a subclass of Frame

Import javax.swing.*;Import java.awt.*;

Public class theframe extends Jframe {

Public theframe() {

Container mycnt = getContentPane(); mtcnt.setLayout(new FlowLayout()); Jbutton btOK = new JButton(“OK”); mycnt.add(btOK);}

public static void main(String[] args) {

theframe myframe = new theframe(); myframe.settitle(“Frame subclass”); myframe.setsize(300,300); myframe.setvisible=true; myframe.setdefaultcloseoperation(Jframe.EXIT_ON_CLOSE); }}

Page 14: GUI Chapter 10 Graphics context and objects Creating a window based application JFrame, JTextField, JButton Containers and Controls Graphics commands Layout

The color Class

Color c = new Color(100,100,100)

The color constructor expects:Color(R,G,B)Red, Green, Blue 0-255

btnOK.setForeground( c );

Page 15: GUI Chapter 10 Graphics context and objects Creating a window based application JFrame, JTextField, JButton Containers and Controls Graphics commands Layout

The Font class

Font ( name, Style, size)

Font F = new Font(“Serif”, Font.BOLD + Font.ITALIC, 12);

btnOK.setFont(F);

Page 16: GUI Chapter 10 Graphics context and objects Creating a window based application JFrame, JTextField, JButton Containers and Controls Graphics commands Layout

Drawing shapes

drawlinedrawrectfillrectdrawarcfillarcdrawstringdraw3drectdrawroundedrectdrawpolygon

Page 17: GUI Chapter 10 Graphics context and objects Creating a window based application JFrame, JTextField, JButton Containers and Controls Graphics commands Layout

Making a window based application

import javax.awt.*;import javax.swing.*;public class MyWindow extends JFrame { public MyWindow() { super(“My Window”); setSize(400,200); setVisible(true); } public void paint(Graphics g) { super.paint(g); g.setColor(Color.red); g.fillRect(20,20,100,100); } public static void main( String args[]) { MyWindow app1=new MyWindow(); app1.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE); }}

Page 18: GUI Chapter 10 Graphics context and objects Creating a window based application JFrame, JTextField, JButton Containers and Controls Graphics commands Layout

Event Handlers

Event-driven programmingA change in the state of a GUI component triggers a call to an event handler methodEvent source is the object that the user interacts with to trigger the event.Event handler is an object with an event handler method designed to handle a specific event type.

Page 19: GUI Chapter 10 Graphics context and objects Creating a window based application JFrame, JTextField, JButton Containers and Controls Graphics commands Layout

Implementing Event Handlers

1 – Code a class for the event handler. The class must implement the correct listener. This can be the same class file as out

application runs in.

2 – Create an event handler object from the class.3 – Register the handler object with the GUI component.

Page 20: GUI Chapter 10 Graphics context and objects Creating a window based application JFrame, JTextField, JButton Containers and Controls Graphics commands Layout

Event Handlers – Step 1

Create a class to implement the Handler

public class MyProgram extends JFrame implements ActionListener {

… /** This method will be invoked when a button is clicked */ public void actionPerformed(ActionEvent e) { System.out.println("The " + e.getActionCommand() + "

button is " + "clicked at\n " + new java.util.Date(e.getWhen()));

} …}

Page 21: GUI Chapter 10 Graphics context and objects Creating a window based application JFrame, JTextField, JButton Containers and Controls Graphics commands Layout

Event Handlers – Step 2

Create event handler object

//ButtonListener btListener = new ButtonListener();

This step may be skipped if we are using the Program class as the event object.

Page 22: GUI Chapter 10 Graphics context and objects Creating a window based application JFrame, JTextField, JButton Containers and Controls Graphics commands Layout

Event Handlers – Step 3

Register the event handler object with the source object. In this example a button.

… private JButton jbtOk = new

JButton("OK"); jbtOk.addActionListener(this);…

Page 23: GUI Chapter 10 Graphics context and objects Creating a window based application JFrame, JTextField, JButton Containers and Controls Graphics commands Layout

Exampleimport javax.swing.*; import java.awt.*; import java.awt.event.*; public class TestActionEvent extends

JFrame implements ActionListener { // Create two buttons private JButton jbtOk = new

JButton("OK"); private JButton jbtCancel = new

JButton("Cancel"); public TestActionEvent() { // Set the window title setTitle("TestActionEvent"); // Set FlowLayout manager to arrange

the components inside the frame getContentPane().setLayout(new

FlowLayout());

// Add buttons to the frame getContentPane().add(jbtOk); getContentPane().add(jbtCancel); // Create a listener object //ButtonListener btListener = new

ButtonListener(); // Register listeners jbtOk.addActionListener(this); jbtCancel.addActionListener(this); } /** Main method */ public static void main(String[] args) { TestActionEvent frame = new

TestActionEvent();

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setSize(100, 80); frame.setVisible(true); } /** This method will be invoked when a button

is clicked */ public void actionPerformed(ActionEvent e) { System.out.println("The " +

e.getActionCommand() + " button is " + "clicked at\n " + new

java.util.Date(e.getWhen())); } }

Page 24: GUI Chapter 10 Graphics context and objects Creating a window based application JFrame, JTextField, JButton Containers and Controls Graphics commands Layout

Getting and Setting the value of a TextField

Getting a value from a Textfield String s; s= TheTextField.getText();

Setting the value in a TextField TheTextField.setText(“A value”);

Page 25: GUI Chapter 10 Graphics context and objects Creating a window based application JFrame, JTextField, JButton Containers and Controls Graphics commands Layout

Summary

AWT and Swing APIContainers – JFrameLabels, TextFields and ButtonsLayout ManagersGraphics commandsHandling EventsLayout Managers