Sie sind auf Seite 1von 26

Swings - Major Components

Major Components
1. JFrame
2. JButton
Object
3. JOptionPane
Component
4. JDialog
5. JPanel Container
6. JComboBox
Window
7. JScrollPane
8. JTabbedPane Frame
9. Jtable JFrame
10.JTree
Fig: JFrame Hierarchy 7
1. JFrame
JFrame is a predefined class
JFrame is a Container under swing package
JFrame is used to set the title and border of a
frame.
Syntax:
JFrame = new JFrame();

8
//Demo for Frames
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class FrameTester
{ public static void main (String args[ ])
{ JFrame f = new JFrame ("JFrame Example");
Container c = f.getContentPane();
c.setLayout (new FlowLayout());
for (int i = 0; i < 5; i++)
{
c.add (new JButton ("No"));
c.add (new Button ("Batter"));
}
c.add (new JLabel ("Swing"));
f.setSize (300, 200);
f.show();
}
}
9
2. JButton
JButton is a predefined class
JButton is a Container under swing package
JButton is used to simulate the appearance of a push
button in a frame.

Syntax:
JButton = new JButton();
10
//Demo for JButton is a predefined class under swing package
import java.awt.*;
import javax.swing.*;
class BorderTest
{ public static void main(String[ ] args)
{ JFrame window = new BorderTestGUI();
window.setTitle("BorderTest");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setVisible(true);
}
}
class BorderTestGUI extends JFrame
{ BorderTestGUI() { JButton north = new JButton("North");
JButton east = new JButton("East");
JButton south = new JButton("South");
JButton west = new JButton("West");
JButton center = new JButton("Center");
Container content = this.getContentPane();
content.setLayout(new BorderLayout());
content.add(north , BorderLayout.NORTH);
content.add(east , BorderLayout.EAST);
content.add(south , BorderLayout.SOUTH);
content.add(west , BorderLayout.WEST);
content.add(center, BorderLayout.CENTER);
this.pack();
}
}
11
3. JOptionPane
JOptionPane is a predefined class
JOptionPane is a Container under swing package
JOptionPane is used to convey the message
through dialog box (take inputs/show the output)
of a frame.
Syntax:
JOptionPane = new JOptionPane();

12
//Demo for Fibonacci Series using JOptionPane Class
import javax.swing.*;
class fibserjp
{ public static void main(String args[ ]) throws Exception
{ int n=Integer.parseInt(JOptionPane.showInputDialog("Enter n:"));
int a=-1;
int b=1;
JOptionPane.showMessageDialog(null,"Fibonacci Series");
for(int i=0;i<n;i++)
{
int c=a+b;
JOptionPane.showMessageDialog(null,c);
a=b;
b=c;
}
}
}

13
4. JDialog
JDialog is a predefined class
JDialog is a Container under swing package
JDialog is used to convey the message through
dialog box (take inputs/show the output) of a
frame.

Syntax:
JDialog = new JDialog();

14
//Demo for Dialog box Frame using JOptionPane class
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class DLG extends JFrame
{ DLG()
{ super("Example: Swing Dialog");
final JFrame jf = this;
final JButton jb = new JButton("Show a message dialog!");
jb.addActionListener(new ActionListener()
{ public void actionPerformed(ActionEvent ae)
{
JOptionPane.showMessageDialog(jf,"This is a simple message dialog");
}
});
add(jb);
setSize(250,100);
}
public static void main(String[ ] args)
{ new DLG().setVisible(true);
}
}

15
5. JPanel
JPanel is a predefined and simplest container
class.
JPanel is a Container under swing package
JPanel is used to provide space to include other
panels (like buttons, textboxes etc..)
Panel is the Syntax:
JPanel = new JPanel();

16
//Demo for Panel in SWINGS
import java.awt.*;
import java.awt.event.*;
class PNL extends Frame
{ PNL()
{ super("Example: Panel");
final Panel p = new Panel();
p.add(new Button("1"));
p.add(new Button("2"));
p.add(new Button("3"));
add(p);
setSize(250,100);
}
public static void main(String[ ] args)
{ new PNL().setVisible(true);
}
}
17
6. JComboBox
JComboBox is a predefined class
JComboBox is a Container under swing package
JComboBox is used to display the list of items to
be selected in the frame.
There are two types of JComboBox:
Uneditable (default)
Editable

Syntax:
JComboBox = new JComboBox();

18
//Demo for ComboBox in swings
import javax.swing.*; import java.awt.*;
import java.awt.event.*;
public class ComboBox
{ JComboBox combo; JTextField txt;
public static void main(String[ ] args)
{ ComboBox b = new ComboBox();
}
public ComboBox()
{ String course[ ] = {"BCA","MCA",“M.TECH",“PH.D"};
JFrame frame = new JFrame("Creating a JComboBox Component");
JPanel panel = new JPanel();
combo = new JComboBox(course);
combo.setBackground(Color.gray);
combo.setForeground(Color.red);
txt = new JTextField(10);
panel.add(combo); panel.add(txt);
frame.add(panel);
combo.addItemListener(new ItemListener(){
public void itemStateChanged(ItemEvent ie)
{ String str = (String)combo.getSelectedItem();
txt.setText(str);
}
});
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400,400); frame.setVisible(true);
}
}

19
7. JScrollPane
JScrollPane is a predefined class
JScrollPane is a Container under swing package
JScrollPane is used to increase the text area i.e.
top to bottom or left to right of a frame.

Syntax:
JScrollPane = new JScrollPane();

20
//Demo for JScrollPane in SWINGS
import javax.swing.*;
public class JScrollContainer
{ public static void main(String[ ] args)
{
String st = "Math_calculs_coordinate\n"
+ "Computer_Operator_Programer\n" + "Physics_Laboratry\n"
+ "Chemestru\n" + "Biology" + "Civics\n" +"History" + "Hindi\n"
+ "English\n" + "Tally" + "Coordinate\n"+ "Trigonometry"
+ "ram is a good boy\n" + "He has four cars and two computers.";
JFrame frame = new JFrame("Creating a JScrollPane Container");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
JTextArea area = new JTextArea(st,5,6);
JScrollPane spane = new JScrollPane(area);
panel.add(spane);
frame.add(panel);
frame.setSize(400,400);
frame.setVisible(true);
}
}
21
8. JTabbedPane
JTabbedPane is a predefined class
JTabbedPane is a Container under swing package
JTabbedPane is used to display the panels in
tabbed manner in the frame.
Syntax:
JTabbedPane = new JTabbedPane();

22
//Demo for JTabbedPane
import javax.swing.*;
import java.awt.*;
public class CreateTabbedPane
{ public static void main(String[ ] args)
{ JFrame frame = new JFrame("Tabbed Pane Frame");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTabbedPane tab = new JTabbedPane();
frame.add(tab, BorderLayout.CENTER);
JButton button = new JButton("1");
tab.add("Tab 1", button);
button = new JButton("2");
tab.add("Tab 2", button);
frame.setSize(400,400);
frame.setVisible(true);
}
}

23
9. JTable
JTable is a predefined class
JTable is a Container under swing package
JTable is used to display the table with rows and
columns along with header in the frame.
Syntax:
JTable = new JTable();

24
//Demo for JTable
import javax.swing.*; import javax.swing.table.*;
import java.awt.*;
public class GridLine
{ JTable table;
public static void main(String[ ] args)
{ new GridLine();
}
public GridLine()
{ JFrame frame = new JFrame("Grid Line in JTable Component!");
JPanel panel = new JPanel();
String data[ ][ ] = { {"Vinod","Computer","3"},
{"Rahul","History","2"},
{"Manoj","Biology","4"},
{"Sanjay","PSD","5"}};
String col [ ] = {"Name","Course","Year"};
DefaultTableModel model = new DefaultTableModel(data,col);
table = new JTable(model); table.setShowGrid(true);
table.setShowVerticalLines(true); table.setGridColor(Color.red);
JTableHeader header = table.getTableHeader(); header.setBackground(Color.yellow);
JScrollPane pane = new JScrollPane(table); panel.add(pane); frame.add(panel);
frame.setSize(500,150); frame.setUndecorated(true);
frame.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
25
10. JTree
JTree is a predefined class
JTree is a Container under swing package
JTree is used to display the hierarchical data like
files in the disk.
Syntax:
JTree = new JTree();

26
//Displaying System Files in JTree
import javax.swing.*;
import javax.swing.event.*;
import java.util.Properties;
import javax.swing.JTree.*;
public class FileDirectoryTree
{ public static void main(String[ ] args)
{ try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
JFrame fm = new JFrame("System file properties of tree ");
Properties p = System.getProperties();
JTree tree = new JTree(p);
tree.setRootVisible(true);
JScrollPane scrollpane = new JScrollPane(tree);
fm.getContentPane().add(scrollpane, "Center");
fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fm.setSize(450,400);
fm.setVisible(true);
} catch (Exception e) {}
}
}
27
AWT SWINGS

1. Mainly AWT is platform 1. swings are platform independent.


dependent. 2. Swing components are
2. AWT components are heavy- lightweight.
weight. 3. Swing works faster than AWT.
3. AWT works slower than Swing. 4. It doesn't invoke native methods.
4. it will invoke native methods. 5. swing has main() method.
5. AWT has main() method. 6. Can switch from one look and
6. Can’t switch from one look and feel to another at runtime.
feel to another at runtime. 7. All components in Swing, except
7. Heavy weight components JApplet, JDialog, JFrame and
depend on the local windowing JWindow are lightweight
toolkit. components.
8. AWT having html tags and java 8. Swings does not have html tags
code. but only java code.
9. AWT is a thin layer of code on 9. Swing is much larger layer of
top of the OS. code on top of OS.
10. AWT extension is SWINGS. 10. Swings are extension of AWT.
11. Example: java.awt.Button 11. Example:
javax.swing.JOptionpane
28
SWINGS(JFC) APPLETS
1. It is a package and contains 1. It is a package and contains
classes and methods. classes and methods.
2. It is a light weight component. 2. it is heavy weight Components
3. Using GUI Manager swing have 3. Applet Does not have look and
look and feel according to user feel according to user view u can
view u can change look and feel change look and feel
4. swing uses for stand lone 4. It need HTML code for Run the
Applications ,Swing have main Applet.
method to execute the program. 5. It doesn’t uses MVC Model view
5. uses MVC Model view Controller. Controller.
6. swing have its own Layout like 6. Applet uses AWT Layouts like
most popular Box Layout. flow layout Thread.
7. Swing have some Thread rules. 7. Applets doesn’t have Thread
8. We have to add container and rules.
maintain all action control with in 8. Applet doesn’t require main
frame container . method ,instead it needs init
9. swing components require method.
javax.swing package. 9. Applet needs web browser or tool
10. Swing creates its own known as “AppletViewer”, which
component. requires java.applet package.
10. components of applet are from
underlying operating system .
29
11.Swing methods are setsize(), setcolor(),
setbackground(), add() and setoreground.
11.Applet methods are init(), start(), paint(), stop() and
destroy()
Advantages
To develop GUI applications.
To develop Web applications.
To develop Electronic Gadgets.
To develop Client-Server Applications.
It is better than AWT and JFC package.
Swing components are lightweight (less resource
intensive than AWT).
Swing provides built-in double buffering.
Swing provides paint debugging support for when you
build your own components.
30
//Java Program To Display a Simple Tree
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.tree.*;
public class Tree1 extends JFrame
{ public static void main(String args[ ])
{ Tree1 frame = new Tree1(" A Tree");
frame.setSize(200,200);
frame.setVisible(true);
}
public Tree1(String title)
{ setTitle(title);
DefaultMutableTreeNode root=new DefaultMutableTreeNode ("Engineering");
DefaultMutableTreeNode style=new DefaultMutableTreeNode ("Information Technology");
root.add(style);
style=new DefaultMutableTreeNode("Electronics");
root.add(style);
style=new DefaultMutableTreeNode ("Computer Science");
root.add(style);
style=new DefaultMutableTreeNode ("Mechanical");
root.add(style);
style=new DefaultMutableTreeNode ("Electrical");
root.add(style);
style=new DefaultMutableTreeNode ("Sound");
root.add(style);
JTree jt=new JTree(root);
Container contentPane=getContentPane();
contentPane.add(new JScrollPane(jt));
}
}

31
/* Simple Java Program for the implementation of ProgressBar, you can also refer to
the progressbar code given beow and implement it in your programs */
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class ProgressBarr extends JFrame implements ActionListener
{ JButton jb;
JProgressBar jpb1;
int value =0;
ProgressBarr()
{ Container contentPane=getContentPane();
contentPane.setLayout(new FlowLayout());
jb = new JButton("Click");
contentPane.add(jb);
jb.addActionListener(this);
jpb1 = new JProgressBar(0,1000);
contentPane.add(jpb1);
jpb1.setStringPainted(true);
}
public static void main(String args[ ])
{ ProgressBarr pb1 = new ProgressBarr();
pb1.setSize(500,500);
pb1.setVisible(true);
}
public void actionPerformed(ActionEvent ae)
{ while(value<=1000)
{ value = value+10;
jpb1.setValue(value);
}
}
}

32

Das könnte Ihnen auch gefallen