GUI-based Applets

Information about GUI-based Applets

Published on July 7, 2014

Author: nantha98

Source: authorstream.com

Content

PowerPoint Presentation: 1 Developing GUI-based Applet by Nantha Kumar Subramaniam PhD CITP Learning Outcomes: Learning Outcomes At the end of the lesson, you should be able to: Identify various GUI components supported by Java; Declare and create GUI components in Java; Differentiate various layout managers in Java; Use layout managers in GUI-based Applet program; and Develop GUI-based Applet programs. Common GUI components in Java: Common GUI components in Java JLabel label ; label = new JLabel (“ Label ”); Label Button JButton btn ; btn = new JButton (“ Cancel ”); TextField JTextField txt ; txt = new JTextField (); Declare the GUI component create the GUI component create the GUI component Declare the GUI component How to declare in Java? Other GUI components in Java: Other GUI components in Java TextArea ( JTextArea ) CheckBox ( JCheckBox ) RadioButton ( JRadioButton ) Slider ( JSlider ) Layout Managers: Layout Managers Used to arrange the GUI components according to our preference There are EIGHT layout managers in Java In this lesson, we will focus on three commonly used layout managers: FlowLayout() GridLayout() BorderLayout() Layout managers are created in Applet by using the setLayout() method FlowLayout() Layout Manager: FlowLayout() Layout Manager Used to arrange the GUI components from left to right FlowLayout ( FlowLayout.CENTER ) OR FlowLayout () FlowLayout ( FlowLayout.LEFT ) FlowLayout ( FlowLayout.RIGHT ) GridLayout() Layout Manager: GridLayout() Layout Manager Used to arrange the GUI components according to the rows and columns GridLayout ( 4 , 4 ) 4 rows 4 columns rows columns BorderLayout() Layout Manager: BorderLayout() Layout Manager Used to arrange the GUI components into five different areas of north, south, east, west and center. Command: add( button1 , BorderLayout.NORTH ); Command: add( button2 , BorderLayout.SOUTH ); Command: add( button3 , BorderLayout.WEST ); Command: add( button4 , BorderLayout.CENTER ); Command: add( button5 , BorderLayout.EAST ); Problem 1: Applet with simple GUI components: Problem 1: Applet with simple GUI components 9 public class myGUI extends JApplet { //declare the GUI components (the 2 buttons) //declare the init() method //inside the init() method, create the GUI components //create the container //set the layout for the container //put the GUI components into the container one-by-one } JButton b1 , b2 ; public void init() { b1 = new JButton (“ Save ”); b2 = new JButton (“ Cancel ”); Container con = getContentPane () ; con. setLayout (new FlowLayout ( FlowLayout.LEFT )); con. add ( b1 ); con. add ( b2 ); } import javax.swing .*; import java.awt.*; What’s the layout?? FlowLayout ( FlowLayout.LEFT ) Problem 2: Using multiple layout managers: Problem 2: Using multiple layout managers 10 In the above interface, we cannot use one layout manager. We need to use multiple layout managers Your first task is to split the components according to different panel, in which each panel will accommodate the appropriate layout manager. Treat the panels as sub-containers. We will call this area as pane1 . It will have GridLayout (3,2) as its layout manager panel1 We will call this area as pane12 . It will have FlowLayout (CENTER) as its layout manager panel2 panel1: GridLayout (3,2) panel2: FlowLayout.CENTER What is the layout for the main container? 3 rows 2 columns Problem 2: Using multiple layout managers: Problem 2: Using multiple layout managers 11 panel1: GridLayout (3,2) panel2: FlowLayout.CENTER What is the layout for the main container? 2 rows 1 column So, the main container will have GridLayout (2,1) as its layout manager Solution Steps: Solution Steps 12 public class myGUI2 extends JApplet { //declare the GUI components (the 1 button 3 textfields , 3 labels) //declare the init() method //inside the init() method, create the GUI components //create the container //set the layout for the container //put the GUI appropriate components into the respective panels } import javax.swing .*; import java.awt.*; //declare the two panels (panel1, panel2) using JPanel //inside the init() method, create the two JPanels //set the layout for the two panels //put panel1 into container //put panel2 into container All these steps must be performed inside the init() method Complete Solution: Complete Solution import javax.swing.*; import java.awt.*; public class myGUI2 extends JApplet { JLabel label1, label2, label3; //declare the components JTextField txt1, txt2, txt3; JButton b1; JPanel panel1, panel2; //declare the JPanel public void init(){ //declare the init() method label1= new JLabel("Enter Number 1:"); //create the GUI components label2= new JLabel("Enter Number 2:"); label3= new JLabel("Total is:"); txt1= new JTextField(); txt2= new JTextField(); txt3= new JTextField(); b1= new JButton("Total"); panel1= new JPanel(); //create the panels panel2= new JPanel(); panel1.setLayout(new GridLayout(3,2)); //set the layout for panel1 panel2.setLayout(new FlowLayout(FlowLayout.CENTER)); //set the layout for panel2 Container con = getContentPane(); //declare the container con.setLayout(new GridLayout(2,1)); //set the layout for the container panel1.add(label1); p1.add(txt1); //add the appropriate GUI components into panel1 panel1.add(label2); p1.add(txt2); panel1.add(label3); panel1.add(txt3); panel2.add(b1); //add the appropriate GUI components into panel2 con.add(panel1); //put the panel1 into the container con.add(panel2); //put the panel2 into the container } } 13 Important Notes: Important Notes If you do not set the layout for the JPanel of an Applet, the FlowLayout.CENTER will be used as the default layout manager If you do not set the layout for the main container of an Applet, the BorderLayout will be used as the default layout manager 14 PowerPoint Presentation: 15 Thank you

Related presentations


Other presentations created by nantha98

Using access modifiers
07. 07. 2014
0 views

Using access modifiers

Object and classes
09. 07. 2014
0 views

Object and classes

09. 07. 2014
0 views

"while" loops

switch statements
09. 07. 2014
0 views

switch statements

Java Operators
29. 07. 2014
0 views

Java Operators

Data Types and Variables
29. 07. 2014
0 views

Data Types and Variables