1 / 13

Programming Review

Programming Review. Lab 2 EECS 448 Dr Fengjun Li and Meenakshi Mishra. Software Development. Science or Art? Science Parts of it have fixed objective which must be met Building new technology to help mankind Art Most clients just give a rough idea what they want

Download Presentation

Programming Review

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Programming Review Lab 2 EECS 448 DrFengjun Li and Meenakshi Mishra

  2. Software Development • Science or Art? • Science • Parts of it have fixed objective which must be met • Building new technology to help mankind • Art • Most clients just give a rough idea what they want • The final product is result of programmer’s imaginations • The more you practice, the better you get at it • There are basic techniques that can be taught; other than that you are on your own

  3. Task for Today • Build a java application for a basic calculator • The user selects the basic operations from drop-down menu • The options should be addition, subtraction, multiplication, devision • The user enters the numbers in two text fields provided • The result appears in third text field

  4. A tip that always helps • Approach the problem in small steps • Breaking the problem down in steps helps debugging • For example in this case • First write an application just to launch the application window • Change your program to include the textfields and labels • Write a code to just do addition • Plug in the drop-down menu and adjust the program accordingly

  5. Working with swing • A package written for developing sophisticated GUI • Adopts style of the operating system or style unique to Java • Each style is called look and feel • Java 7 introduces new look and feel called Nimbus, that is unique to Java • Must avail the swing package first • import javax.swing.*; • Other packages supporting GUI programming • java.awt (Abstract Windowing Toolkit) • java.awt.event (class handling user input) • Swing generally more powerful than awt • Not implemented by platform specific code, but written entirely in java • Supplies plenty of exciting additions like scroll panes, trees, tables etc • When using Swing component, you work with object of that Swing class • All Swing components are subclasses of abstract class JComponent

  6. Creating Interface • Create a class that represents the main GUI • In most projects, main interface is a frame (JFrame class) • Frame is a window that appears whenever you open an application • Frame has a title bar, Maximize, Minimize, Close buttons and other features • Users of graphical environments expect to move, resize and close the windows • To create a graphical Java application, make the interface a subclass of JFrame • public class MyFirstGui extends Jframe • Set title by either setTitle(String) or super(String) in the constructor • Set size by setSize(int, int) where first number denotes width and second height in pixels • Make your frame visible by setVisible(true); • Set default close operation to exit on closing setDefaultCloseOption(EXIT_ON_CLOSE) • Other close options • DISPOSE_ON_CLOSE • HIDE_ON_CLOSE • DO_NOTHING_ON CLOSE

  7. Adding Components • Class JButton • Three constructors • JButton(String) Label on the button • JButton(Icon) Icon on the button • JButton(String,Icon) Both icon and Label on the button • JButton b = new JButton(“OK”); • Class JPanel • After creating components, need to add them to a container • Simplest container • JPanel p = new JPanel(); • p.add(b); //add button b to panel p

  8. Adding Components • Class JTextField • JTextField() Empty Text field • JTextField(int)  Text Field with specified width • JTextField(String, int)  Text Field with specified width and text • setEditable(False) for text fields that do not accept user input • Class JLabel • Three Constructors • JLabel(String) Label with specified text • JLabel(String, int)  Label with specified text and alignment • SwingConstants.LEFT • SwingConstants.RIGHT • SwingConstants.CENTER • JLabel(String, Icon, int) Label with specified text, icon and alignment

  9. Adding Components • Class JComboBox • Creates a list of items that are hidden when menu is not being used • addItem(Object) Add item to the list • getItemAt(int)  Returns the text of the list item at specified index position • getSelectedIndex()  Returns the index position of selected item • getSelectedItem()  Returns the text of currently selected item • setSelectedIndex(int)  Selects the item at indicated index position • setSelectedIndex(object)  Selects specified object in the list

  10. Responding to User Inputs • Use java.awt.event package • Useful listener interfaces • ActionListener When user performs an action on a component. • AdjustmentListener When component is adjusted • FocusListener Textfield or area loses or gains focus • ItemListener When items in items such as combo box are changed • KeyListener When user enters text using keyboard • MouseListener Generated by mouse clicks, mouse entering or leaving a component area • MouseMotionListener track all movement by mouse over component • WindowListener When window is minimized, maximized, moved or closes • Implement listener in class definition • public class t extends JFrame implements ActionListener, ItemListener { //…}

  11. Setting up Components • Need to add the attribute of listening to specific objects • addActionListener()  JButton, JCheckBox, JComboBox, JTextField, JRadioButton and JMenuItem • addFocusListener()  all Swing Component • addItemListener()  JButton, JCheckBox, JComboBox and JRadioButton • addKeyListener()  all Swing Components • addMouseListener()  all Swing Components • addMouseMotionListener()  all Swing Components • addTextListener()  JTextField and JTextArea • addWindowListener()  JWindow and JFrame • JButton b = new JButton(‘OK”); • b.addActionListener();

  12. Event Handling Methods • Action Events • ActionListener actionPerformed(ActionEvent) • Focus Events • FocustListener focusGained(FocusEvent) and focusLost(FocusEvent) • Item Events • ItemListener itemStateChanged(ItemEvent) • When a class implements an event interface, must define the corresponding event handling methods • public void actionPerformed(ActionEvent e) { //..} • To know which for which object the event was generated • Object source=e.getSource(); • If the event was triggered by button b, you can check using if (source==b) { //..}

  13. Summary of designing application • Import packages • Create a Class that extends JFrame and implements all the user interfaces required • Make sure to set the frame to visible, set title, declare size, the application exits on closing • Create the components • Add property of user input listening to the components • Add the components to a simple container (optional) • Add the container to the Frame • Define what to do when the user enters required input • Define event handling methods

More Related