Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Java Swing-Java Programming-Lecture Slides, Slides of Computer Engineering and Programming

This lecture was delivered by Aniruddh Parmar at B. R. Ambedkar Bihar University for Data Transfer Programming course. It includes: Swing, Abstract, Windowing, Toolkit, Extends, Event, Listeners, Properties, Methods, Configure, Create, Layout

Typology: Slides

2011/2012

Uploaded on 07/11/2012

dhananad
dhananad 🇮🇳

4

(2)

38 documents

1 / 31

Related documents


Partial preview of the text

Download Java Swing-Java Programming-Lecture Slides and more Slides Computer Engineering and Programming in PDF only on Docsity! Java Swing Docsity.com AWT to Swing • AWT: Abstract Windowing Toolkit • import java.awt.* • Swing: new with Java2 • import javax.swing.* • Extends AWT • Tons o‟ new improved components • Standard dialog boxes, tooltips, … • Look-and-feel, skins • Event listeners Docsity.com Using a GUI Component 1. Create it • Instantiate object: b = new JButton(“press me”); 2. Configure it • Properties: b.text = “press me”; [avoided in java] • Methods: b.setText(“press me”); 3. Add it • panel.add(b); 4. Listen to it • Events: Listeners JButton Docsity.com Anatomy of an Application GUI JPanel JButton JFrame JLabel GUI Internal structure JFrame JPanel JButton JLabel containers Docsity.com Using a GUI Component 2 1. Create it 2. Configure it 3. Add children (if container) 4. Add to parent (if not JFrame) 5. Listen to it order important Docsity.com Application Code import javax.swing.*; class hello { public static void main(String[] args){ JFrame f = new JFrame(“title”); JPanel p = new JPanel(); JButton b = new JButton(“press me”); p.add(b); // add button to panel f.setContentPane(p); // add panel to frame f.show(); } } press me Docsity.com Layout Managers • Automatically control placement of components in a panel • Why? • Docsity.com Layout Manager Heuristics Left to right, Top to bottom c n s e w FlowLayout GridLayout BorderLayout none, programmer sets x,y,w,h null One at a time CardLayout GridBagLayout JButton Docsity.com Code: null layout JFrame f = new JFrame(“title”); JPanel p = new JPanel( ); JButton b = new JButton(“press me”); b.setBounds(new Rectangle(10,10, 100,50)); p.setLayout(null); // x,y layout p.add(b); f.setContentPane(p); press me Docsity.com Code: FlowLayout JFrame f = new JFrame(“title”); JPanel p = new JPanel( ); FlowLayout L = new FlowLayout( ); JButton b1 = new JButton(“press me”); JButton b2 = new JButton(“then me”); p.setLayout(L); p.add(b1); p.add(b2); f.setContentPane(p); Set layout mgr before adding components press me then me Docsity.com Applets • JApplet is like a JFrame • Already has a panel • Access panel with JApplet.getContentPane( ) import javax.swing.*; class hello extends JApplet { public void init(){ JButton b = new JButton(“press me”); getContentPane().add(b); } } JApplet contentPane JButton Docsity.com Applet Security • No read/write on client machine • Can‟t execute programs on client machine • Communicate only with server • “Java applet window” Warning Docsity.com 21 Text Fields and an Introduction to Event Handling with Nested Classes • GUIs are event-driven – A user interaction creates an event • Common events are clicking a button, typing in a text field, selecting an item from a menu, closing and window and moving the mouse – The event causes a call to a method called an event handler Docsity.com 22 Text Fields and an Introduction to Event Handling with Nested Classes • Class JTextComponent – Superclass of JTextField • Superclass of JPasswordField – Adds echo character to hide text input in component – Allows user to enter text in the component when component has the application‟s focus Docsity.com 25 Outline • TextFieldFra me .java • (3 of 3) 57 // user pressed Enter in JTextField textField1 58 if ( event.getSource() == textField1 ) 59 string = String.format( "textField1: %s", 60 event.getActionCommand() ); 61 62 // user pressed Enter in JTextField textField2 63 else if ( event.getSource() == textField2 ) 64 string = String.format( "textField2: %s", 65 event.getActionCommand() ); 66 67 // user pressed Enter in JTextField textField3 68 else if ( event.getSource() == textField3 ) 69 string = String.format( "textField3: %s", 70 event.getActionCommand() ); 71 72 // user pressed Enter in JTextField passwordField 73 else if ( event.getSource() == passwordField ) 74 string = String.format( "passwordField: %s", 75 new String( passwordField.getPassword() ) ); 76 77 // display JTextField content 78 JOptionPane.showMessageDialog( null, string ); 79 } // end method actionPerformed 80 } // end private inner class TextFieldHandler 81 } // end class TextFieldFrame Test if the source of the event is the first text field Get text from text field Get text from text field Get text from text field Get password from password field Test if the source of the event is the second text field Test if the source of the event is the third text field Test if the source of the event is the password field Docsity.com 26 1 // Fig. 11.10: TextFieldTest.java 2 // Testing TextFieldFrame. 3 import javax.swing.JFrame; 4 5 public class TextFieldTest 6 { 7 public static void main( String args[] ) 8 { 9 TextFieldFrame textFieldFrame = new TextFieldFrame(); 10 textFieldFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); 11 textFieldFrame.setSize( 325, 100 ); // set frame size 12 textFieldFrame.setVisible( true ); // display frame 13 } // end main 14 } // end class TextFieldTest Docsity.com 27 Steps Required to Set Up Event Handling for a GUI Component • Several coding steps are required for an application to respond to events – Create a class for the event handler – Implement an appropriate event-listener interface – Register the event handler Docsity.com 30 Registering the Event Handler for Each Text Field • Registering an event handler – Call method addActionListener to register an ActionListener object – ActionListener listens for events on the object Docsity.com 31 Details of Class TextFieldHandler‟s actionPerformed Method • Event source – Component from which event originates – Can be determined using method getSource – Text from a JTextField can be acquired using getActionCommand – Text from a JPasswordField can be acquired using getPassword Docsity.com
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved