Lecture 8-GUI QH
Lecture 8-GUI QH
Programming
IT069IU
Lecture 8
GUI
Constructor Description
This is the default constructor for JFrame. It
JFrame()
creates a new frame with no title
// Close operation
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
➢ Java JPanel
▪ Part of the Java Swing package
▪ Is a container that can store a group of components.
• add(Component c): Adds a component to a specified container
• setLayout(LayoutManager l): sets the layout of the container to the specified
layout manager
• updateUI(): resets the UI property with a value from the current look and feel.
• setUI(PanelUI ui): sets the look and feel of an object that renders this component.
• getUI(): returns the look and feel object that renders this component.
• paramString(): returns a string representation of this JPanel.
• getUIClassID(): returns the name of the Look and feel class that renders this
component.
• getAccessibleContext(): gets the AccessibleContext associated with this JPanel.
5
Java JFrame & JPanel
➢ Java JPanel
import java.awt.*;
import javax.swing.*;
public class EX2_JPanel {
static JFrame f; // JFrame
static JButton b; // JButton
static JLabel l; // Label to display text
public static void main(String[] args)
{
// Creating a new frame to store text field and button
f = new JFrame("panel");
l = new JLabel("panel label"); // Creating a label text
b = new JButton("button1"); // Creating a new buttons
JPanel p = new JPanel(); // Creating a panel to add buttons
// Adding buttons and textfield to panel
p.add(b);
p.add(l);
p.setBackground(Color.red); // setbackground of panel
f.add(p); // Adding panel to frame
f.setSize(300, 300); // Setting the size of frame
f.show();
} } 6
Java JFrame & JPanel
➢ Class Graphics
abstract boolean drawImage(Image img, int x, int y, Color bgcolor, ImageObserver observer)
Draws as much of the specified image as is currently available.
abstract boolean drawImage(Image img, int x, int y, int width, int height, ImageObserver observer)
Draws as much of the specified image as has already been scaled to fit inside the
specified rectangle.
abstract void drawLine(int x1, int y1, int x2, int y2)
Draws a line, using the current color, between the points (x1, y1) and (x2, y2) in this
graphics context's coordinate system.
abstract void drawOval(int x, int y, int width, int height)
Draws the outline of an oval.
abstract void drawPolygon(int[] xPoints, int[] yPoints, int nPoints)
Draws a closed polygon defined by arrays of x and y coordinates.
abstract void drawPolyline(int[] xPoints, int[] yPoints, int nPoints)
Draws a sequence of connected lines defined by arrays of x and y coordinates.
void drawRect(int x, int y, int width, int height)
Draws the outline of the specified rectangle.
abstract void drawRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight)
Draws an outlined round-cornered rectangle using this graphics context's current color.
abstract void drawString(String str, int x, int y)
Draws the text given by the specified string, using this graphics context's current font and
color. 7
https://docs.oracle.com/javase/8/docs/api/java/awt/Graphics.html
Java JFrame & JPanel
➢ Class Graphics
void fill3DRect(int x, int y, int width, int height, boolean raised)
Paints a 3-D highlighted rectangle filled with the current color.
abstract void fillArc(int x, int y, int width, int height, int startAngle, int arcAngle)
Fills a circular or elliptical arc covering the specified rectangle.
abstract void fillOval(int x, int y, int width, int height)
Fills an oval bounded by the specified rectangle with the current color.
abstract void fillPolygon(int[] xPoints, int[] yPoints, int nPoints)
Fills a closed polygon defined by arrays of x and y coordinates.
abstract void fillRect(int x, int y, int width, int height)
Fills the specified rectangle.
abstract void fillRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight)
Fills the specified rounded corner rectangle with the current color.
abstract void setColor(Color c)
Sets this graphics context's current color to the specified color.
abstract void setFont(Font font)
Sets this graphics context's font to the specified font.
8
https://docs.oracle.com/javase/8/docs/api/java/awt/Graphics.html
Class Graphics Example
package Graphics;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;
public class Plot_Graph extends JPanel {
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.red);
g.drawLine(10, 60, 50, 50);
g.setColor(Color.orange);
g.drawRect(200, 60, 100, 60);
g.setColor(Color.blue);
g.drawOval(400, 60, 100, 60);
g.setColor(Color.orange);
g.drawRoundRect(200, 160, 100, 60,30,30);
g.drawString("Hello Class", 200, 10);
// x coordinates of vertices
int x[] = { 10, 30, 40, 50, 110, 140 };
// y coordinates of vertices
int y[] = { 340, 310, 350, 340, 330, 310 };
// number of vertices
int numberofpoints = 6;
// set the color of line drawn to blue
g.setColor(Color.blue);
// draw the polygon using drawPolygon function
g.drawPolygon(x, y, numberofpoints);
9
}}
Class Graphics Example
package Graphics;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
11
Class Graphics Example
package Graphics;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;
13
Class Graphics Example
14
WindowBuilder
❖ A powerful, easy-to-use, bi-directional Java GUI designer
• Composed of WindowBuilder
Engine, SWT, eRCP, XWT & Swing
Designer
• WindowBuilder Engine provides a
rich API for creating UI designers
15
WindowBuilder
❖ WindowBuilder is composed of the following major components
• Source View
• Design View
• Component Tree
• Property Pane
• Palette
• Wizards
• Toolbars &
Context Menus
16
WindowBuilder
Adding a Window
17
WindowBuilder
Design View
18
WindowBuilder
19
WindowBuilder
20
JButton
21
JLabel
22
JTextField
23
JTextArea
24