Class Notes (838,990)
Canada (511,155)
York University (35,577)
ITEC 2610 (11)
Lecture 6

W6.doc

9 Pages
220 Views
Unlock Document

Department
Information Technology
Course
ITEC 2610
Professor
Radu Campeanu
Semester
Winter

Description
Week 6: Layout Management (ch.12.2 of Horstmann, ch. 9.4 of Lewis&Loftus and chs. 12 and 13 of Deitel&Deitel) Recap main ideas with GUI and Event Handling: 1. Create GUI object 2. Add it to the container 3. Create the event listeners 4. inner classes implementing Action/ItemListener - implement actionPerformed(ActionEvent e) or itemStateChanged(ItemEvent e) - e.getSource() refers to objects e.getActionCommand()/e.getStateChanged() refers to content of objects 5. Register/link event listeners with GUI objects using addAction/ItemListener() Organizing the screen requires layout managers. Java provides a variety of classes for this purpose, which all implement the interface LayoutManager. We shall discuss only the following classes: • FlowLayout (objects are placed left to right, row after row, in the order of addition) • BorderLayout (objects are placed in 5 possible places: N,S,W,E,Center). Note: (i) the order of adding GUI components is not important; (ii) one cannot more than one object in any of the 5 places. • GridLayout (objects are placed in a grid/2-dimensional array). Note: (i) the order of adding GUI components is important. Objects are added in row after row. http://docs.oracle.com/javase/1.4.2/docs/api/java/awt/GridLayout.html • Layout with the help of JPanel objects. Note: panels are used to subdivide the screen; each panel can have a different layout. • CardLayout (objects are placed on multiple cards, part of a deck of cards) The following example is the first example discussed last week where we change the layout from FlowLayout to BorderLayout. The output of the program is show below. This window can be modified in size but the display of the 5 GUI objects will be always the same. import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Test extends JFrame { private JLabel label; private JTextField t1, t2; private JPasswordField p1; private JButton plainButton; public Test() { super( "Testing GUI Components" ); Container container = getContentPane(); container.setLayout( new BorderLayout(5,5) ); // adds 5 pixels between the GUI components label = new JLabel( "Testing JLabel" ); label.setToolTipText( "Label with text" ); container.add( label , BorderLayout.NORTH); t1 = new JTextField( 10 ); t2 = new JTextField( "Enter text here" ); container.add( t1, BorderLayout.EAST ); container.add( t2, BorderLayout.WEST ); p1 = new JPasswordField( 10 ); container.add( p1, BorderLayout.SOUTH ); plainButton = new JButton( "Plain Button" ); container.add( plainButton, BorderLayout.CENTER ); // Create instances of inner classes for event handling - the event listeners TextFieldHandler h1 = new TextFieldHandler (); // Registers the event listeners with the event sources t1.addActionListener( h1 ); t2.addActionListener( h1 ); p1.addActionListener( h1 ); // For button use a no-name event listener plainButton.addActionListener( new ActionListener () { // note that the object is of interface type.. no need for another class name public void actionPerformed( ActionEvent event ){ JOptionPane.showMessageDialog( null,"You pressed: " + event.getActionCommand() ); } } ); setSize( 275, 100 ); setVisible( true ); } // end of constructor // inner class for textfield event handling private class TextFieldHandler implements ActionListener { public void actionPerformed( ActionEvent event ){ String s = ""; if (event.getSource() == t1) s = "textfield 1 has: " + event.getActionCommand() ; else if (event.getSource() == t2) s = "textfield 2 has: " + event.getActionCommand() ; else if (event.getSource() == p1) s = "password field has: " + event.getActionCommand() ; JOptionPane.showMessageDialog( null, s ); } } // execute application public static void main( String args[] ) { Test application = new Test(); application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } // end class Test The next program shows three things: 1) how to use a panel 2) the use of the GridLayout 3) how to avoid the use of inner classes for event listeners. Task 1. Write a GUI application which calculates the final mark as the average of the marks entered by the user through a textfield. The display should contain at the top of the screen a label containing "Write mark and hit Enter" and next to it a textfield. This pair of GUI objects should display at the top of the window independent of the size of the window. At the bottom of the display there is a button which takes the whole width of the display, with the label: "Calculate Final". The final mark is to be displayed as a label in the middle of the display; it should state "The final mark is xx", where xx is the average of the marks entered by the user (truncated as an integer). Make sure that the textfield for data entry is set blank before another number is entered. A sample output frame is shown below. import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Test extends JFrame implements ActionListener{ private JTextField t1; private JButton b1; private JLabel l1, l2; private JPanel p; int mark=0, count=0; public Test() { super( "Simple marks calculation" ); Container container = getContentPane(); container.setLayout( new BorderLayout() ); p = new JPanel(); p.setLayout( new GridLayout(1,2)); // a grid with one row and 2 columns l1 = new JLabel("Write mark and hit enter"); t1 = new JTextField( ); p.add(l1); p.add(t1); container.add( p , BorderLayout.NORTH ); l2= new JLabel(); container.add( l2, BorderLayout.CENTER ); b1 = new JButton( "Calculate Final" ); container.add( b1, BorderLayout.SOUTH ); t1.addActionListener(this ); b1.addActionListener( this); setSize( 375, 200 ); setVisible( true ); } public void actionPerformed( ActionEvent event ){ String s = ""; if (event.getSource() == t1) { s = event.getActionCommand() ; mark+=Integer.parseInt(s); count++; t1.setText(""); } else if (event.getSource() == b1) l2.setText("the final mark is "+ mark/count) ; } public static void main( String args[] ) { Test application = new Test(); application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } Note the followings: - the requirement was that we put BOTH the label and the textfield side by side at the top of the screen. FlowLayout management cannot be used as it is not going to enforce this requirement (as the user shrinks the display the textfield will be shifted to the second row). GridLayout will enforce this requirement. Using a panel organized with GridLayout allows us to put the two objects at the
More Less

Related notes for ITEC 2610

Log In


OR

Join OneClass

Access over 10 million pages of study
documents for 1.3 million courses.

Sign up

Join to view


OR

By registering, I agree to the Terms and Privacy Policies
Already have an account?
Just a few more details

So we can recommend you notes for your school.

Reset Password

Please enter below the email address you registered with and we will send you a link to reset your password.

Add your courses

Get notes from the top students in your class.


Submit