+ All Categories
Home > Education > 20- Graphical user interface java

20- Graphical user interface java

Date post: 15-Jan-2015
Category:
Upload: raja-kumar
View: 108 times
Download: 3 times
Share this document with a friend
Description:
 
Popular Tags:
26
Graphical User Interface JAVA FOUNDATION CLASS (JFC) – JAVAX.SWING Shahjahan Samoon
Transcript
Page 1: 20- Graphical user interface java

Graphical User InterfaceJAVA FOUNDATION CLASS (JFC) – JAVAX.SWING

Shahjahan Samoon

Page 2: 20- Graphical user interface java

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.

Page 3: 20- Graphical user interface java

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

Page 4: 20- Graphical user interface java

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

Page 5: 20- Graphical user interface java
Page 6: 20- Graphical user interface java

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();

}

}

Page 7: 20- Graphical user interface java

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);

}

}

Page 8: 20- Graphical user interface java

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);

}

}

Page 9: 20- Graphical user interface java

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();

}

}

Page 10: 20- Graphical user interface java

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

Page 11: 20- Graphical user interface java

The Absolute Layout

Page 12: 20- Graphical user interface java

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();

}

}

Page 13: 20- Graphical user interface java

The GridLayout

Page 14: 20- Graphical user interface java

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);

}

}

Page 15: 20- Graphical user interface java

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);

}

}

Page 16: 20- Graphical user interface java

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

Page 17: 20- Graphical user interface java

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);

}

}

`

Page 18: 20- Graphical user interface java

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);

}

}

Page 19: 20- Graphical user interface java

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);

}

}

Page 20: 20- Graphical user interface java

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);

}

}

`

Page 21: 20- Graphical user interface java

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);

}

}

Page 22: 20- Graphical user interface java

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);

}

}

Page 23: 20- Graphical user interface java

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);

}

}

Page 24: 20- Graphical user interface java

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);

}

}

Page 25: 20- Graphical user interface java

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);

}

}

Page 26: 20- Graphical user interface java

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);

}

}


Recommended