Javaswings Part 1
Javaswings Part 1
• Model
• View
• Controller
MVC Architecture
The Model
The model is responsible for managing the data of the
application.
It responds to the request from the view and it also
responds to instructions from the controller to update
itself.
The View
A presentation of data in a particular format, triggered
by a controller's decision to present the data.
They are script based templating systems like JSP, ASP,
PHP.
The Controller
Lightweight Containers :
Example: JPanel
Top Level Containers: JFrame
• A Frame, in general, is a container that can contain other
components such as buttons, labels, text fields, etc.
• A Frame window can contain a title, a border, and also menus,
text fields, buttons, and other components.
• The Frame in Java Swing is defined in class javax.swing.Jframe.
• Usually used as a program’s main window
We can create a JFrame window object using 2 approaches:
javax.swing.JDialog:
• More simple and limited than frames
17
• Use the static method of JoptionPane to show
standard dialog boxes:
JOptionPane.showMessageDialog(null, "4+2=6");
Lightweight Containers : JPanel
Methods Description
void setText(String text) It defines the single line of text this component will
display.
void It sets the alignment of the label's contents along the X
setHorizontalAlignment(int axis.
alignment)
Icon getIcon() It returns the graphic image that the label displays.
Methods Description
Constructor Description
Methods Description
import javax.swing.*;
class TextFieldExample {
JTextField t1,t2;
t1.setBounds(50,100, 200,30);
t2=new JTextField(“Subject");
t2.setBounds(50,150, 200,30);
f.add(t1); f.add(t2);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
Java JCheckBox
• The JCheckBox class is used to create a checkbox.
• JCheckBox(String str)
• JCheckBox(Icon i)
• JCheckBox(String str,Icon i)
Methods Description
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
//add main method