[go: up one dir, main page]

0% found this document useful (0 votes)
15 views24 pages

Lecture 8-GUI QH

This document provides an overview of Java JFrame and JPanel, essential components of Java Swing for creating graphical user interfaces. It details various constructors and methods for JFrame, as well as JPanel functionalities, including adding components and setting layouts. Additionally, it includes examples of using graphics in Java and introduces WindowBuilder, a GUI designer for Java applications.

Uploaded by

aoe7289
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views24 pages

Lecture 8-GUI QH

This document provides an overview of Java JFrame and JPanel, essential components of Java Swing for creating graphical user interfaces. It details various constructors and methods for JFrame, as well as JPanel functionalities, including adding components and setting layouts. Additionally, it includes examples of using graphics in Java and introduces WindowBuilder, a GUI designer for Java applications.

Uploaded by

aoe7289
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Object-Oriented

Programming
IT069IU
Lecture 8
GUI

Dr. Huynh Tan Quoc


Java JFrame & JPanel
• Java JFrame
▪ Thе Java JFramе is an еssеntial componеnt of Java Swing.
▪ JFrame in Java is a class that allows you to crеatе and manage a top-lеvеl
window in a Java application.

Constructor Description
This is the default constructor for JFrame. It
JFrame()
creates a new frame with no title

This constructor creates a new frame with the


JFrame(String title)
specified title.

This constructor creates a JFrame that uses the


JFrame(GraphicsConfiguration gc)
specified graphics configuration.

This constructor creates a JFrame with the


JFrame(String title, GraphicsConfiguration gc) specified title and using the specified graphics
configuration.

This constructor creates a JFrame with the


JFrame(Rectangle bounds)
specified bounds.

This constructor creates a JFrame with the


JFrame(String title, Rectangle bounds)
specified title and bounds. 2
Java JFrame & JPanel
➢ Methods of Java JFrame
Methods Description
setTitle(String title) Sets the title of the JFrame.
setSize(int width, int height) Sets the size of the JFrame.
Sets the default close operation for the JFrame. Common
setDefaultCloseOperation(int options include JFrame.EXIT_ON_CLOSE,
operation) JFrame.HIDE_ON_CLOSE, and
JFrame.DO_NOTHING_ON_CLOSE.
Sets the visibility of the JFrame. Pass true to make it visible
setVisible(boolean b)
and false to hide it.
setLayout(LayoutManager Sets the layout manager for the JFrame, which controls how
manager) components are arranged within the frame.
add(Component comp) Adds a Swing component to the JFrame.
remove(Component comp) Removes a component from the JFrame.
Forces the layout manager to recalculate the layout of
validate()
components within the JFrame.
setResizable(boolean resizable) Controls whether the user can resize the JFrame.
3
setIconImage(Image image) Sets the icon (image) for the JFrame window.
Java JFrame & JPanel
import javax.swing.JFrame;

public class Ex1_JFrame {


public static void main(String[] args)
{
// Create a new JFrame
JFrame frame = new JFrame("My First JFrame");

// Set the size of the frame


frame.setSize(600,400);

// Close operation
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// Make the frame visible


frame.setVisible(true);
}
}
4
Java JFrame & JPanel

➢ 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;

public class EX2_plotRec {


public static void main(String[] args) {
Plot_Graph plot = new Plot_Graph();
JFrame app = new JFrame("EX App1");
app.add(plot);
app.setSize(600, 600);
app.setLocationRelativeTo(null);
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
app.setVisible(true);
}
}
10
Class Graphics Example

11
Class Graphics Example
package Graphics;

import java.awt.Color;
import java.awt.Graphics;

import javax.swing.JPanel;

public class Fill_Graph extends JPanel {


@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.orange);
g.fillRect(200, 60, 100, 60);
g.setColor(Color.blue);
g.fillOval(400, 60, 100, 60);
g.setColor(Color.orange);
g.fillRoundRect(200, 160, 100, 60,30,30);
// 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.fillPolygon(x, y, numberofpoints);
} 12
}
Class Graphics Example

13
Class Graphics Example

14
WindowBuilder
❖ A powerful, easy-to-use, bi-directional Java GUI designer

Available now from : https://eclipse.dev/windowbuilder/downloads/

• 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

You might also like