0% found this document useful (0 votes)
46 views10 pages

Genesis & Structure of Java: Topic 02

→ In our first example, we will show a basic window on the screen. The code is shown here and creates a simple JFrame window with a title and size. → Here we import Swing classes that will be used in the code example. We create an instance of our code example class, which inherits from JFrame, and make it visible on the screen. → In our next example, we add a "Quit" button to the window. We create a JButton, position it using setBounds(), and add an action listener so that clicking the button terminates the application.

Uploaded by

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

Genesis & Structure of Java: Topic 02

→ In our first example, we will show a basic window on the screen. The code is shown here and creates a simple JFrame window with a title and size. → Here we import Swing classes that will be used in the code example. We create an instance of our code example class, which inherits from JFrame, and make it visible on the screen. → In our next example, we add a "Quit" button to the window. We create a JButton, position it using setBounds(), and add an action listener so that clicking the button terminates the application.

Uploaded by

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

Genesis & Structure of

java
TOPIC 02
Topics covered
 GUI and Swing
 Basic Swing Based program in java
 Adding Controls
 Adding Events
Introduction
 Swing
→ Swing is a principal GUI toolkit for the Java programming language.
→ It is a part of the JFC (Java Foundation Classes), which is an API for providing a
graphical user interface for Java programs.
→ It is completely written in Java.
 The main characteristics of the Swing toolkit
→ platform independent
→ customizable
→ extensible
→ configurable
→ lightweight
Our first example
package com.bit;

import javax.swing.JFrame;
import javax.swing.SwingUtilities;
→ In our first example, we will show a basic window on the screen.
public class SimpleExample extends JFrame {
→ The code is shown here c

→ public this
While SimpleExample() { short, the application window can do quite a lot. It can be
code is very
setTitle("Simple
resized, example");
maximized, minimized. All the complexity that comes with it has been hidden
setSize(300, 200);
from the application programmer.
setLocationRelativeTo(null);
import javax.swing.JFrame;
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
import javax.swing.SwingUtilities;
→ publicwe
Here static void main(String[]
import Swing classesargs) that
{ will be used in the code example.
public class Example extendsRunnable()
SwingUtilities.invokeLater(new JFrame { {
→ @Override class inherits from the JFrame widget. JFrame is a toplevel container. In
The Example
public void run() {
the container, we put other widgets.
SimpleExample ex = new SimpleExample();
setTitle("Simple example");
ex.setVisible(true);
}
→ Here
}); we set the title of the window using the setTitle() method.
}
}
Our first example
setSize(300, 200);
→ This code will resize the window to be 300px wide and 200px tall.
setLocationRelativeTo(null);
→ This line will center the window on the screen.
setDefaultCloseOperation(EXIT_ON_CLOSE);
→ This method will close the window, if we click on the close button of the titlebar. By
default nothing happens.
SwingUtilities.invokeLater(new Runnable() {
public void run() {
SimpleExample ex = new SimpleExample();
ex.setVisible(true);
}
});
→ We create an instance of our code example and make it visible on the screen.
(advanced topic and we should not worry right now about it.)
We position a JButton on the window. We will add an action listener to this button.

package com.bit;
import java.awt.event.*;

Adding “Quit” Button


import javax.swing.*;

public class QuitButtonExample extends JFrame {


public QuitButtonExample() {
initUI();
→ }In our first example, we will show a basic window on the screen.
private void initUI() {
→ The JPanel codepanel =isnew
shown
JPanel();here c
getContentPane().add(panel);
ipublic QuitButtonExample() {
panel.setLayout(null);
JButton quitButton = new JButton("Quit");
initUI();
quitButton.setBounds(50, 60, 80, 30);
quitButton.addActionListener(new ActionListener() {
} @Override
public void actionPerformed(ActionEvent event) {
→ It is a good programming practice to put the code that creates the GUI inside a specific
System.exit(0);
}
method.
});
panel.add(quitButton);
JPanel panel = new JPanel();
setTitle("Quit button");
setSize(300, 200);
getContentPane().add(panel);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
→ }We create a JPanel component. It is a generic lightweight container. We add the JPanel
to the
public JFrame.
static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
panel.setLayout(null);
public void run() {
QuitButtonExample ex = new QuitButtonExample();
→ If weex.setVisible(true);
call setLayout(null) we can position our components absolutely. For this, we use the
}
setBounds()
});
method.
}
}
Adding “Quit” button
JButton quitButton = new JButton("Quit");
quitButton.setBounds(50, 60, 80, 30);
→ Here we create a button. We position it by calling the setBounds() method.
setLocationRelativeTo(null);
→ This line will center the window on the screen.
quitButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
System.exit(0);
}
});
→ We add an action listener. The action listener will be called, when we perform an action
on the button. In our case, if we click on the button. The click will terminate the
application.
Adding “Quit” button
panel.add(quitButton);
→ In order to show the quit button, we must add it to the panel.
package com.bit;
import java.awt.event.*;

How things works


import javax.swing.*;

public class QuitButtonExample extends JFrame {


public QuitButtonExample() {
initUI(); ❸ this method calls executes
→ }Lets see how do the thing woks Quit button 1
private void initUI() {
JPanel panel = new JPanel();
getContentPane().add(panel);
panel.setLayout(null);
JButton quitButton = new JButton("Quit");
quitButton.setBounds(50, 60, 80, 30);
quitButton.addActionListener(new ActionListener() {
Quit
@Override
public void actionPerformed(ActionEvent event) {
System.exit(0);
}
});
panel.add(quitButton);
setTitle("Quit button");
setSize(300, 200); ❹ this frame is created
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args) {
❶ program
SwingUtilities.invokeLater(new Runnable() {starts with main
public void run() {
QuitButtonExample ex = new QuitButtonExample(); ❷ with new statement, constructor is called
ex.setVisible(true);
} ❺ this statement then makes the frame visible
});
}
}
Thank you

Next class: Math and String classes in java


→The output is as like below

You might also like