Friday, 8 August 2014

Swing (Graphics Programming in java):

Swing (Graphics Programming in java):

Swing is a part of JFC (Java Foundation Classes) that is used to create GUI application. It is built on the top of AWT and entirely written in java.

Advantage of Swing over AWT:

There are many advantages of Swing over AWT. They are as follows:
  • Swing components are Plateform independent.
  • It is lightweight.
  • It supports pluggable look and feel.
  • It has more powerful components like tables, lists, scroll panes, color chooser, tabbed pane etc.
  • It follows MVC (Model View Controller) architecture.

What is JFC ?

The Java Foundation Classes (JFC) are a set of GUI components which simplify the development of desktop 
applications
 .


Do You Know ?
  • How to display image on a button in swing?
  • How to change the component color by choosing a color from ColorChooser ?
  • How to display the digital watch in swing tutorial ?
  • How to create a notepad in swing?
  • How to create puzzle game and pic puzzle game in swing ?
  • How to create tic tac toe game in swing ?

Hierarchy of swing:

hierarchy of swing

Commonly used Methods of Component class:

1)public void add(Component c)
2)public void setSize(int width,int height)
3)public void setLayout(LayoutManager m)
4)public void setVisible(boolean)

Creating a Frame:

There are two ways to create a frame:
  • By creating the object of Frame class (association)
  • By extending Frame class (inheritance)

Simple example of Swing by Association:

  1. import javax.swing.*;  
  2. public class Simple {  
  3. JFrame f;  
  4. Simple(){  
  5.       
  6. f=new JFrame();  
  7.           
  8. JButton b=new JButton("click");  
  9. b.setBounds(130,100,10040);  
  10.           
  11. f.add(b);  
  12.           
  13. f.setSize(400,500);  
  14. f.setLayout(null);  
  15. f.setVisible(true);  
  16. }  
  17.   
  18. public static void main(String[] args) {  
  19. new Simple();  
  20. }  
  21. }  

public void setBounds(int xaxis, int yaxis, int width, int height); have been used in the above example that sets the position of the button.
simple example of swing

Simple example of Swing by inheritance:

  1. import javax.swing.*;  
  2. public class Simple2 extends JFrame{  
  3. JFrame f;  
  4. Simple2(){  
  5.       
  6. JButton b=new JButton("click");  
  7. b.setBounds(130,100,10040);  
  8.           
  9. add(b);  
  10.           
  11. setSize(400,500);  
  12. setLayout(null);  
  13. setVisible(true);  
  14. }  
  15.   
  16. public static void main(String[] args) {  
  17. new Simple2();  
  18. }  
  19. }  

    JButton class:

    The JButton class is used to create a button that have plateform-independent implementation.

    Commonly used Constructors:

  20. JButton(): creates a button with no text and icon.
  21. JButton(String s): creates a button with the specified text.
  22. JButton(Icon i): creates a button with the specified icon object.

Commonly used Methods of AbstractButton class:

1) public void setText(String s): is used to set specified text on button.
2) public String getText(): is used to return the text of the button.
3) public void setEnabled(boolean b): is used to enable or disable the button.
4) public void setIcon(Icon b): is used to set the specified Icon on the button.
5) public Icon getIcon(): is used to get the Icon of the button.
6) public void setMnemonic(int a): is used to set the mnemonic on the button.
7) public void addActionListener(ActionListener a): is used to add the action listener to this object.

Note: The JButton class extends AbstractButton class.

Example of displaying image on the button:

  1. import java.awt.event.*;  
  2. import javax.swing.*;  
  3.   
  4. public class ImageButton{  
  5. ImageButton(){  
  6. JFrame f=new JFrame();  
  7.                   
  8.           
  9. JButton b=new JButton(new ImageIcon("b.jpg"));  
  10. b.setBounds(130,100,10040);  
  11.       
  12. f.add(b);  
  13.           
  14. f.setSize(300,400);  
  15. f.setLayout(null);  
  16. f.setVisible(true);  
  17.           
  18. f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  19.           
  20.     }  
  21.       
  22. public static void main(String[] args) {  
  23.     new ImageButton();  
  24. }  
  25. }  

JRadioButton class:

The JRadioButton class is used to create a radio button.

Commonly used Constructors of JRadioButton class:

  • JRadioButton(): creates an unselected radio button with no text.
  • JRadioButton(String s): creates an unselected radio button with specified text.
  • JRadioButton(String s, boolean selected): creates a radio button with the specified text and selected status.

Commonly used Methods of AbstractButton class:

1) public void setText(String s): is used to set specified text on button.
2) public String getText(): is used to return the text of the button.
3) public void setEnabled(boolean b): is used to enable or disable the button.
4) public void setIcon(Icon b): is used to set the specified Icon on the button.
5) public Icon getIcon(): is used to get the Icon of the button.
6) public void setMnemonic(int a): is used to set the mnemonic on the button.
7) public void addActionListener(ActionListener a): is used to add the action listener to this object.

Note: The JRadioButton class extends the JToggleButton class that extends AbstractButton class.

example of JRadioButton class:

  1. import javax.swing.*;  
  2. public class Radio {  
  3. JFrame f;  
  4.   
  5. Radio(){  
  6. f=new JFrame();  
  7.   
  8. JRadioButton r1=new JRadioButton("A) Male");  
  9. JRadioButton r2=new JRadioButton("B) FeMale");  
  10. r1.setBounds(50,100,70,30);  
  11. r2.setBounds(50,150,70,30);  
  12.   
  13. ButtonGroup bg=new ButtonGroup();  
  14. bg.add(r1);
  15. bg.add(r2);  
  16.   
  17. f.add(r1);f.add(r2);  
  18.   
  19. f.setSize(300,300);  
  20. f.setLayout(null);  
  21. f.setVisible(true);  
  22. }  
  23. public static void main(String[] args) {  
  24.     new Radio();  
  25. }  
  26. }  

ButtonGroup class:

The ButtonGroup class can be used to group multiple buttons so that at a time only one button can be selected.
 

JTextArea class:

The JTextArea class is used to create a text area. It is a multi line area that displays the plain text only.

Commonly used Constructors:

  • JTextArea(): creates a text area that displays no text initially.
  • JTextArea(String s): creates a text area that displays specified text initially.
  • JTextArea(int row, int column): creates a text area with the specified number of rows and columns that displays no text initially..
  • JTextArea(String s, int row, int column): creates a text area with the specified number of rows and columnsthat displays specified text.

Commonly used methods of JTextArea class:

1) public void setRows(int rows): is used to set specified number of rows.
2) public void setColumns(int cols):: is used to set specified number of columns.
3) public void setFont(Font f): is used to set the specified font.
4) public void insert(String s, int position): is used to insert the specified text on the specified position.
5) public void append(String s): is used to append the given text to the end of the document.

Example of JTextField class:

  1. import java.awt.Color;  
  2. import javax.swing.*;  
  3.   
  4. public class TArea {  
  5.     JTextArea area;  
  6.     JFrame f;  
  7.     TArea(){  
  8.     f=new JFrame();  
  9.           
  10.     area=new JTextArea(300,300);  
  11.     area.setBounds(10,30,300,300);  
  12.       
  13.     area.setBackground(Color.black);  
  14.     area.setForeground(Color.white);  
  15.           
  16.     f.add(area);  
  17.       
  18.     f.setSize(400,400);  
  19.     f.setLayout(null);  
  20.     f.setVisible(true);  
  21. }  
  22.     public static void main(String[] args) {  
  23.         new TArea();  
  24.     }  
  25. }  

No comments:

Post a Comment