Java Swing for Building Graphical User Interfaces

Introduction to Java Swing
Graphical User Interface
Two Types of Applications
Console Applications:
Input from keyboard using java.util.Scanner
Output is usually displayed to a console window
using System.out.print().
Not practical for real-world
GUI Applications
Graphical User Interface
Very practical for real-world
What is Java Swing?
Java Swing is a framework for developing Java
GUI Apps.
Graphical functionality is provided by the library.
There is no need to write your own.
Note:
Java 7: Uses Swing (current applications still use it.)
Java 8+: JavaFX
Topics
Swing Components
 JFrame
JComponent
JPanel
User Interface (UI) components: buttons, text fields,
labels, etc.
Action Events
Building a Fractal drawing application.
Swing Components
Swing is a toolkit that Sun Microsystems
created to enable large-scale java applications.
 Swing provides a wide array of visual and
interactive components.
All Swing component names begin with "J"
JFrame
The graphical window for an application.
JFrame provides a top-level window with a
title, border.
JFrame is a container component.
User Interface components can be easily
added to a JFrame.
JFrame Example
The application window shown below is a JFrame.
 Buttons, text input fields, and a graphic
component have been added to the JFrame.
JComponent
This is a container class.
jComponent also provides functionality for
painting and drawing.
Jcomponent Example:
.
A JComponent element was added to the Jframe.
Rectangles were drawn on the JComponent.
Common UI Components
JButton : This is a typical button.  Buttons can
react to an action event.
JTextField: This is a standard input text box for the
input of a single line of text. A JTextField can react
to input events.
JLabel: Used to label a nearby action component.
Labels do not react to action events or input
events.
ActionEvent Handling
UIComponents, such as a Jbutton, fire off ActionEvents
to indicate some kind of action has occurred.
For example, a JButton fires off an ActionEvent whenever
the user presses it.
To respond to an ActionEvent, an action listener must
be attached to the UI component.
An event listener is triggered when an action event
occurs.
 action event can be registered to many UI
components.
Handling Events
Clicking a button on the interface is an example
of an event.  When an event occurs, there are
three objects that must be involved:
1.
The object causing the event, the event source,
such as a button.
2.
An object representing the event, the
ActionEvent.
3.
The object that listens for and then handles a
triggered event, the ActionListener.
The 
ActionListener
 object provides a method
called 
actionPerformed
 that accepts an
ActionEvent
 argument.
Example:
a.
A button is created.
b.
An 
ActionListener 
is instantiated and registered to the button.
c.
When a button click event occurs, the 
actionPerformed 
() method
will respond.
private
 
void
 createButton() {
  
button = 
new
 JButton("Create Rectangles");
  
ActionListener listener = 
new
 AddMyListener();
  
button.addActionListener(listener);
}
class
 AddMyListener 
implements
 ActionListener {
  
public
 
void
 actionPerformed(ActionEvent event)
{
      
   
//DRAW ON ADRAWING COMPONENT
   
DrawComponent.createRectangle();
  
}
}
Build the
Fractal
Generator
App
App
Structure Design
 uses a
MVC
Architecture
Build the App from the Bottom Up!
Fractal.java
C_curveComponent
C-curveFrame
MyApp
import
 java.awt.Graphics;
public
 
class
 Fractal {
 
public
 
void
 drawCCurve(Graphics g, 
int
 x1, 
int
 y1, 
int
 x2, 
int
 y2, 
int
 level) {
  
//PRIMITIVE STATE: DRAWS A STRAIGHT LINE FROM X1,Y1 TO X2,Y2
  
if
 (level == 1){
   
g.drawLine(x1, y1, x2, y2);
  
}
  
//RECURSIVE ELEMENT: TWO LINES WILL BE PRODUCED FOR EVERY SINGLE LINE
  
else
 {
   
int
 xn = (x1 + x2) / 2 + (y1 - y2) / 2;
   
int
 yn = (x2 - x1) / 2 + (y1 + y2) / 2;
   
drawCCurve(g, x1, y1, xn, yn, level - 1);
   
drawCCurve(g, xn, yn, x2, y2, level - 1);
  
}
 
}
}
Fractal.java
public
 
class
 
C_curveComponent
 
extends
 JComponent {
 
private
 Fractal fractal;
 
private
 
int
 level;
 
private
 
int
 x1, y1, x2, y2;
 
public
 C_curveComponent() {
  
level = 2;
  
fractal = 
new
 Fractal();
 
}
 
public
 
void
 setXY(
int
 _x1, 
int
 _y1, 
int
 _x2, 
int
 _y2) {
  
x1 = _x1;    y1 = _y1;   x2 = _x2;    y2 = _y2;
 
}
 
public
 
void
 setLevel(
int
 _level){
  
level = _level;
 
}
 
public
 
void
 createCcurve() {
  
repaint();
 
}
 
public
 
void
 paintComponent(Graphics g) {
                 fractal.drawCCurve(g, x1, y1, x2, y2, level);
 
}
}
C-curveComponent.java
public
 
class
 
C_curveFrame
 
extends
 JFrame {
 
private
 JPanel panel;
 
 
private
 JLabel levelLabel;
 
private
 JTextField levelField;
 
private
 JButton button;
 
private
 C_curveComponent cCurveDrawComponent;
 
public
 C_curveFrame() {
  
// TASK : BUILD THE PANEL ELEMENTS
  
this
.setSize(700, 700);  //WINDOW SIZE
  
panel = 
new
 JPanel();
 
  
levelLabel = 
new
 JLabel("Level: ");
  
levelField = 
new
 JTextField(10); //FIELD WIDTH OF 10
  
levelField.setText("" + 2);
  
 //INITIALIZE THE TEXT FIELD TO 2
  
button = 
new
 JButton("Create C-curve");
  
panel.add(levelLabel);
  
panel.add(levelField);
  
panel.add(button);
  
cCurveDrawComponent = 
new
 C_curveComponent();
  
cCurveDrawComponent.setPreferredSize(
new
 Dimension(700,700));
  
cCurveDrawComponent.setLevel(2); //2  is the default level
  
panel.add(cCurveDrawComponent);
  
  
C-curveFrame.java
  
  
// TASK : ADD THE COMPLETED PANEL TO THE FRAME WINDOW
  
this
.add(panel);
  
  
// TASK : REGISTER AN ACTION LISTENER FOR THE BUTTON
  
ActionListener listener = 
new
 AddMyListener();
  
button.addActionListener(listener);
 
}
 
C-curveFrame.java
 
class
 AddMyListener 
implements
 ActionListener {
  
public
 
void
 actionPerformed(ActionEvent event) {
   
//TASK 1: GET THE FRACTAL LEVEL INPUT BY THE USER
   
int
 level = Integer.
parseInt
(levelField.getText());
   
cCurveDrawComponent.setLevel(level);
   
//TASK 2: COMPUTE THE STARTING AND ENDING XY COORDINATES
   
int
 frame_width = 700;
   
int
 frame_height = 700;
   
int
 x1 = frame_width / 4;
   
int
 y1 = frame_height / 3;
   
int
 x2 = frame_width - x1;
   
int
 y2 = y1;
   
//TASK 3: SET THE XY START AND END FOR THE DRAWING COMPONENT
   
cCurveDrawComponent.setXY(x1, y1, x2, y2);
   
//TASK 4: CREATE THE CURVE ON THE DRAWING COMPONENT
   
cCurveDrawComponent.createCcurve();
  
}
 
}
}
C-curveFrame.java
import
 javax.swing.JFrame;
public
 
class
 MyApp {
 
public
 
static
 
void
 main(String[] args) {
 
      JFrame frame = 
new
 C_curveFrame();
 
      frame.setDefaultCloseOperation(JFrame.
EXIT_ON_CLOSE
);
 
      frame.setVisible(
true
);
 
}
}
MyApp.java
Slide Note
Embed
Share

Java Swing is a powerful framework for creating graphical user interface applications in Java. It provides a wide array of visual components such as JFrame, JComponent, and JPanel, enabling developers to build interactive and visually appealing applications easily. With Swing, developers can design user interfaces that incorporate buttons, text fields, labels, and more, making it a practical choice for real-world applications. Swing components begin with the prefix "J," signifying their association with the Swing toolkit. Explore Java Swing to enhance your skills in GUI development.

  • Java Swing
  • Graphical User Interface
  • GUI Applications
  • Swing Components
  • User Interface

Uploaded on Sep 12, 2024 | 1 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

E N D

Presentation Transcript


  1. Introduction to Java Swing Graphical User Interface

  2. Two Types of Applications Console Applications: Input from keyboard using java.util.Scanner Output is usually displayed to a console window using System.out.print(). Not practical for real-world GUI Applications Graphical User Interface Very practical for real-world

  3. What is Java Swing? Java Swing is a framework for developing Java GUI Apps. Graphical functionality is provided by the library. There is no need to write your own. Note: Java 7: Uses Swing (current applications still use it.) Java 8+: JavaFX

  4. Topics Swing Components JFrame JComponent JPanel User Interface (UI) components: buttons, text fields, labels, etc. Action Events Building a Fractal drawing application.

  5. Swing Components Swing is a toolkit that Sun Microsystems created to enable large-scale java applications. Swing provides a wide array of visual and interactive components. All Swing component names begin with "J"

  6. JFrame The graphical window for an application. JFrame provides a top-level window with a title, border. JFrame is a container component. User Interface components can be easily added to a JFrame.

  7. JFrame Example The application window shown below is a JFrame. Buttons, text input fields, and a graphic component have been added to the JFrame.

  8. JComponent This is a container class. jComponent also provides functionality for painting and drawing.

  9. Jcomponent Example: . A JComponent element was added to the Jframe. Rectangles were drawn on the JComponent.

  10. Common UI Components JButton : This is a typical button. Buttons can react to an action event. JTextField: This is a standard input text box for the input of a single line of text. A JTextField can react to input events. JLabel: Used to label a nearby action component. Labels do not react to action events or input events.

  11. ActionEvent Handling UIComponents, such as a Jbutton, fire off ActionEvents to indicate some kind of action has occurred. For example, a JButton fires off an ActionEvent whenever the user presses it. To respond to an ActionEvent, an action listener must be attached to the UI component. An event listener is triggered when an action event occurs. action event can be registered to many UI components.

  12. Handling Events Clicking a button on the interface is an example of an event. When an event occurs, there are three objects that must be involved: 1. The object causing the event, the event source, such as a button. 2. An object representing the event, the ActionEvent. 3. The object that listens for and then handles a triggered event, the ActionListener.

  13. The ActionListener object provides a method called actionPerformed that accepts an ActionEvent argument.

  14. Example: a. b. c. A button is created. An ActionListener is instantiated and registered to the button. When a button click event occurs, the actionPerformed () method will respond. privatevoid createButton() { button = new JButton("Create Rectangles"); ActionListener listener = new AddMyListener(); button.addActionListener(listener); } class AddMyListener implements ActionListener { publicvoid actionPerformed(ActionEvent event) { //DRAW ON ADRAWING COMPONENT DrawComponent.createRectangle(); } }

  15. Build the Fractal Generator App

  16. App Structure Design uses a MVC Architecture

  17. Build the App from the Bottom Up! Fractal.java C_curveComponent C-curveFrame MyApp

  18. import java.awt.Graphics; Fractal.java publicclass Fractal { publicvoid drawCCurve(Graphics g, int x1, int y1, int x2, int y2, int level) { //PRIMITIVE STATE: DRAWS A STRAIGHT LINE FROM X1,Y1 TO X2,Y2 if (level == 1){ g.drawLine(x1, y1, x2, y2); } //RECURSIVE ELEMENT: TWO LINES WILL BE PRODUCED FOR EVERY SINGLE LINE else { int xn = (x1 + x2) / 2 + (y1 - y2) / 2; int yn = (x2 - x1) / 2 + (y1 + y2) / 2; } } drawCCurve(g, x1, y1, xn, yn, level - 1); drawCCurve(g, xn, yn, x2, y2, level - 1); }

  19. publicclass C_curveComponent extends JComponent { private Fractal fractal; privateint level; privateint x1, y1, x2, y2; } public C_curveComponent() { level = 2; fractal = new Fractal(); } publicvoid setXY(int _x1, int _y1, int _x2, int _y2) { x1 = _x1; y1 = _y1; x2 = _x2; y2 = _y2; } publicvoid setLevel(int _level){ level = _level; } publicvoid createCcurve() { repaint(); } publicvoid paintComponent(Graphics g) { fractal.drawCCurve(g, x1, y1, x2, y2, level); } C-curveComponent.java

  20. publicclass C_curveFrame extends JFrame { private JPanel panel; private JLabel levelLabel; private JTextField levelField; private JButton button; private C_curveComponent cCurveDrawComponent; C-curveFrame.java public C_curveFrame() { // TASK : BUILD THE PANEL ELEMENTS this.setSize(700, 700); //WINDOW SIZE panel = new JPanel(); levelLabel = new JLabel("Level: "); levelField = new JTextField(10); //FIELD WIDTH OF 10 levelField.setText("" + 2); button = new JButton("Create C-curve"); panel.add(levelLabel); panel.add(levelField); panel.add(button); cCurveDrawComponent = new C_curveComponent(); cCurveDrawComponent.setPreferredSize(new Dimension(700,700)); cCurveDrawComponent.setLevel(2); //2 is the default level panel.add(cCurveDrawComponent); //INITIALIZE THE TEXT FIELD TO 2

  21. C-curveFrame.java } // TASK : ADD THE COMPLETED PANEL TO THE FRAME WINDOW this.add(panel); // TASK : REGISTER AN ACTION LISTENER FOR THE BUTTON ActionListener listener = new AddMyListener(); button.addActionListener(listener);

  22. class AddMyListener implements ActionListener { publicvoid actionPerformed(ActionEvent event) { //TASK 1: GET THE FRACTAL LEVEL INPUT BY THE USER int level = Integer.parseInt(levelField.getText()); cCurveDrawComponent.setLevel(level); //TASK 2: COMPUTE THE STARTING AND ENDING XY COORDINATES int frame_width = 700; int frame_height = 700; int x1 = frame_width / 4; int y1 = frame_height / 3; int x2 = frame_width - x1; int y2 = y1; C-curveFrame.java //TASK 3: SET THE XY START AND END FOR THE DRAWING COMPONENT cCurveDrawComponent.setXY(x1, y1, x2, y2); } } } //TASK 4: CREATE THE CURVE ON THE DRAWING COMPONENT cCurveDrawComponent.createCcurve();

  23. MyApp.java import javax.swing.JFrame; publicclass MyApp { } publicstaticvoid main(String[] args) { JFrame frame = new C_curveFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); }

More Related Content

giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#