Assignment 6 PowerPoint PPT Presentation

presentation player overlay
About This Presentation
Transcript and Presenter's Notes

Title: Assignment 6


1
Section 11
  • Assignment 6

2
Some engineering principles
  • Design first, code later.
  • Test, test, test. Use main in the tested classes
    to test them. Design a set of tests and run them
    all (grading guide for Assignment 2 can be a good
    example).
  • Separation of content from presentation.

3
GUI - quick reminder
  • JFrame - usually the main window. Can hold many
  • JPanels - hold
  • Components, i.e.
  • JButton
  • JTextFields
  • many more
  • using FlowLayouts to arrange them
  • FlowLayout, GridLayout, ...

4
Example
  • public class Ex extends JFrame
  • public Ex()
  • setTitle("Example 1")
  • setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
  • setSize(200, 200)
  • getContentPane().setLayout(new FlowLayout())
  • getContentPane().add(new JButton("OK"))
  • setVisible(true)
  • public static void main(String args)
  • Ex e new Ex()

5
Events
  • Event Objects sent by Components/Objects to other
    Objects.
  • Source Components register listeners for its
    events.
  • Listener objects must implement appropriate
    interfaces.

6
Example
  • public class Ex1 extends JFrame implements
    ActionListener
  • public Ex1()
  • JButton bt new JButton("OK")
  • setTitle("Example 1")
  • setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
  • setSize(200, 200)
  • getContentPane().setLayout(new FlowLayout())
  • bt.addActionListener(this)
  • getContentPane().add(bt)
  • setVisible(true)
  • public void actionPerformed(ActionEvent e)
  • if (e.getActionCommand().equals("OK"))
  • System.out.println("Someone pressed the
    button!")
  • public static void main(String args)

7
Example
  • Design a Counter program
  • Functionality
  • Two buttons, one increments the value of the
    counter, the other decrements it.
  • The value of the counter is displayed constantly.
Write a Comment
User Comments (0)
About PowerShow.com