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 [Link];
import [Link];
import [Link];
→ 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 [Link];
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
import [Link];
→ publicwe
Here static void main(String[]
import Swing classesargs) that
{ will be used in the code example.
public class Example extendsRunnable()
[Link](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");
[Link](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.
[Link](new Runnable() {
public void run() {
SimpleExample ex = new SimpleExample();
[Link](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 [Link];
import [Link].*;
Adding “Quit” Button
import [Link].*;
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() {
[Link](null);
JButton quitButton = new JButton("Quit");
initUI();
[Link](50, 60, 80, 30);
[Link](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
[Link](0);
}
method.
});
[Link](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) {
[Link](new Runnable() {
[Link](null);
public void run() {
QuitButtonExample ex = new QuitButtonExample();
→ If [Link](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");
[Link](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.
[Link](new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
[Link](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
[Link](quitButton);
→ In order to show the quit button, we must add it to the panel.
package [Link];
import [Link].*;
How things works
import [Link].*;
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);
[Link](null);
JButton quitButton = new JButton("Quit");
[Link](50, 60, 80, 30);
[Link](new ActionListener() {
Quit
@Override
public void actionPerformed(ActionEvent event) {
[Link](0);
}
});
[Link](quitButton);
setTitle("Quit button");
setSize(300, 200); ❹ this frame is created
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args) {
❶ program
[Link](new Runnable() {starts with main
public void run() {
QuitButtonExample ex = new QuitButtonExample(); ❷ with new statement, constructor is called
[Link](true);
} ❺ this statement then makes the frame visible
});
}
}
Thank you
Next class: Math and String classes in java
→The output is as like below