Upload
raja-kumar
View
121
Download
3
Tags:
Embed Size (px)
DESCRIPTION
Citation preview
Graphical User InterfaceJAVA FOUNDATION CLASS (JFC) – JAVAX.SWING
Shahjahan Samoon
JFC - Swing Swing is part of the so-called "Java Foundation Classes (JFC)"
Introduced in 1997 after the release of JDK 1.1.
JFC was subsequently included as an integral part of JDK since JDK 1.2.
JFC consists of:◦ Swing API: for advanced graphical programming.◦ Accessibility API: provides assistive technology for the disabled.◦ Java 2D API: for high quality 2D graphics and images.◦ Pluggable look and feel supports.◦ Drag-and-drop support between Java and native applications.
3
Swing Components
Containers◦ Contain and manage other components.◦ Top Level/Internal ◦ Examples: JFrame (Top Level), JScrollPane, JPanel.
Basic controls◦ Atomic components ◦ Used for showing output and/or getting some input ◦ Inherits JComponent◦ Examples: JButton, JLabel, JTextArea, JTable, Jlist
Usually every Swing class extends the corresponding AWT class◦ For backward-compatibility reasons
4
JFrame
Made of several layers
Widgets are added to the Content Pane layer.◦ Use getContentPane() to obtain it
Other layers are used for customizing the window's appearence
The JFrame Class import javax.swing.JFrame;
class MyFrame extends JFrame{
MyFrame(){
setSize(300,300);
setTitle("My Frame - HIST");
setVisible(true);
}
public static void main(String args[]){
new MyFrame();
}
}
The JFrame Class import javax.swing.JFrame;
class MyFrame {
public static void main(String args[]){
JFrame frame = new JFrame("My Frame - HIST");
frame.setSize(300,300);
frame.setVisible(true);
}
}
The JDialog Class import javax.swing.JDialog;
import javax.swing.JFrame;
class MyDialog {
public static void main(String args[]){
JDialog dialog = new JDialog(new JFrame(),"My Dialog - HIST");
dialog.setSize(300,300);
dialog.setVisible(true);
}
}
The JPanel Class import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Color;
class MyPannelFrame extends JFrame{
MyPannelFrame(){
setSize(300,300);
setTitle("My Frame - HIST");
setVisible(true);
JPanel panel = new JPanel();
panel.setBackground(Color.BLACK);
add(panel);
}
public static void main(String args[]){
new MyPannelFrame();
}
}
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Color;
class MyPannelFrame extends JFrame{
MyPannelFrame(){
setSize(300,300);
setLayout(null);
setTitle("My Frame - HIST");
setVisible(true);
JPanel panel1 = new JPanel();
panel1.setBackground(Color.BLACK);
panel1.setBounds(0,0,150,300);
add(panel1);
JPanel panel2 = new JPanel();
panel2.setBackground(Color.YELLOW);
panel2.setBounds(150,0,150,300);
add(panel2);
}
public static void main(String args[]){
new MyPannelFrame();
}
}
The Absolute Layout
The Absolute Layout
The GridLayout import javax.swing.*;
import java.awt.*;
class MyPannelFrame extends JFrame{
MyPannelFrame(){
setSize(300,300);
setLayout(new GridLayout(0,2));
setTitle("My Frame - HIST");
setVisible(true);
JPanel panel1 = new JPanel();
panel1.setBackground(Color.RED);
add(panel1);
JPanel panel2 = new JPanel();
panel2.setBackground(Color.YELLOW);
panel2.setBounds(150,0,150,300);
add(panel2);
JPanel panel3 = new JPanel();
panel3.setBackground(Color.GREEN);
add(panel3);
}
public static void main(String args[]){
new MyPannelFrame();
}
}
The GridLayout
The Jbutton Class import javax.swing.*;
import java.awt.*;
class MyFrame {
public static void main(String args[]){
JFrame frame = new JFrame("My Frame - HIST");
frame.setSize(300,300);
frame.setLayout(null);
JButton button = new JButton("Click!");
button.setBounds(10,10,100,30);
frame.add(button);
frame.setVisible(true);
}
}
The JTextField Class import javax.swing.*;
import java.awt.*;
class MyFrame {
public static void main(String args[]){
JFrame frame = new JFrame("My Frame - HIST");
frame.setSize(300,300);
frame.setLayout(null);
JTextField text = new JTextField();
text.setBounds(10,10,100,30);
frame.add(text);
frame.setVisible(true);
}
}
import javax.swing.*;
import java.awt.*;
class MyFrame {
public static void main(String args[]){
JFrame frame = new JFrame("My Frame - HIST");
frame.setSize(300,300);
frame.setLayout(null);
JLabel label = new JLabel("My Label - HIST");
label.setBounds(10,10,100,30);
frame.add(label);
frame.setVisible(true);
}
}
The JLabel Class
The JTextArea Class import javax.swing.*;
import java.awt.*;
class MyFrame {
public static void main(String args[]){
JFrame frame = new JFrame("My Frame - HIST");
frame.setSize(300,300);
frame.setLayout(null);
JTextArea tArea = new JTextArea();
tArea.setBounds(10,10,200,100);
frame.add(tArea);
frame.setVisible(true);
}
}
`
The JMenuBar, JMenu & JMenuItem Classes import javax.swing.*;
import java.awt.*;
class MyFrame {
public static void main(String args[]){
JFrame frame = new JFrame("My Frame - HIST");
frame.setSize(300,300);
frame.setLayout(null);
JMenuItem New = new JMenuItem("New");
JMenuItem Open = new JMenuItem("Open");
JMenuItem Save = new JMenuItem("Save");
JMenu file = new JMenu("File");
file.add(New);
file.add(Open);
file.add(Save);
JMenuBar mbar = new JMenuBar();
mbar.add(file);
frame.setJMenuBar(mbar);
frame.setVisible(true);
}
}
The JList Class import javax.swing.*;
import java.awt.*;
class MyFrame {
public static void main(String args[]){
JFrame frame = new JFrame("My Frame - HIST");
frame.setSize(300,300);
frame.setLayout(null);
String subj[] = {"ASP","PHP","Java"};
JList<String> list = new JList<>(subj);
list.setBounds(20,20,100,150);
frame.add(list);
frame.setVisible(true);
}
}
The JComboBox Class import javax.swing.*;
import java.awt.*;
class MyFrame {
public static void main(String args[]){
JFrame frame = new JFrame("My Frame - HIST");
frame.setSize(300,300);
frame.setLayout(null);
String subj[] = {"ASP","PHP","Java"};
JComboBox<String> combo = new JComboBox<>(subj);
combo.setBounds(20,20,100,20);
frame.add(combo);
frame.setVisible(true);
}
}
`
The JCheckBox Class import javax.swing.*;
import java.awt.*;
class MyFrame {
public static void main(String args[]){
JFrame frame = new JFrame("My Frame - HIST");
frame.setSize(300,300);
frame.setLayout(null);
JCheckBox chkb1 = new JCheckBox("JAVA");
chkb1.setBounds(20,20,100,20);
frame.add(chkb1);
JCheckBox chkb2 = new JCheckBox("ASP");
chkb2.setBounds(20,40,100,20);
frame.add(chkb2);
JCheckBox chkb3 = new JCheckBox("PHP");
chkb3.setBounds(20,60,100,20);
frame.add(chkb3);
frame.setVisible(true);
}
}
The JRadioButton Class import javax.swing.*;
import java.awt.*;
class MyFrame {
public static void main(String args[]){
JFrame frame = new JFrame("My Frame - HIST");
frame.setSize(300,300);
frame.setLayout(null);
JRadioButton r1 = new JRadioButton("JAVA");
r1.setBounds(20,20,100,20);
frame.add(r1);
JRadioButton r2 = new JRadioButton("ASP");
r2.setBounds(20,40,100,20);
frame.add(r2);
JRadioButton r3 = new JRadioButton("PHP");
r3.setBounds(20,60,100,20);
frame.add(r3);
ButtonGroup group = new ButtonGroup();
group.add(r1);
group.add(r2);
group.add(r3);
frame.setVisible(true);
}
}
The JTabbedPane Class import javax.swing.*;
import java.awt.*;
class MyFrame {
public static void main(String args[]){
JFrame frame = new JFrame("My Frame - HIST");
frame.setSize(300,300);
JPanel p1 = new JPanel();
p1.setBackground(Color.YELLOW);
JPanel p2 = new JPanel();
p2.setBackground(Color.GREEN);
JPanel p3 = new JPanel();
p3.setBackground(Color.BLUE);
JTabbedPane tpane = new JTabbedPane();
tpane.add("Java",p1);
tpane.add("Asp",p2);
tpane.add("Php",p3);
frame.add(tpane);
frame.setVisible(true);
}
}
The JTable Class import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class MyTable {
public static void main(String args[]){
JFrame frame = new JFrame("Table");
String heading[]={"Name","Course","Shift"};
Object data[][]={{"Arif","Java","M"},{"Zahid","Php","E"},{"Wasi","Asp","M"}};
JTable table = new JTable(data,heading);
JScrollPane scroll = new JScrollPane(table);
frame.add(scroll); frame.setSize(400,400); frame.setVisible(true);
}
}
The JInternalFrame Class import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class MyInternalFrame {
public static void main(String args[]){
JFrame frame = new JFrame("Frame");
frame.setLayout(null);
JInternalFrame iframe = new JInternalFrame("Internal Frame",true,true,true,true);
iframe.setSize(200,200);
iframe.setVisible(true);
frame.add(iframe);
frame.setSize(400,400); frame.setVisible(true);
}
}
The JSpinner Class import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class Main {
public static void main(String args[]){
JFrame frame = new JFrame("Frame");
frame.setLayout(null);
JSpinner spinner = new JSpinner();
spinner.setBounds(20,40,60,20);
frame.add(spinner);
frame.setSize(400,400);
frame.setVisible(true);
}
}