Questions before we Start - PowerPoint PPT Presentation

1 / 40
About This Presentation
Title:

Questions before we Start

Description:

FINISH EARLY to leave time for class project. Not responsible for. WindowListener ... JLabel patternLabel2 = new JLabel('select one from the list: ... – PowerPoint PPT presentation

Number of Views:19
Avg rating:3.0/5.0
Slides: 41
Provided by: davidh67
Category:

less

Transcript and Presenter's Notes

Title: Questions before we Start


1
Questions before we Start?
  • Questions from previous lecture?
  • Questions on HW?
  • Note to Self Check to be sure pictures are
    working in Olin 106
  • FINISH EARLY to leave time for class project
  • Not responsible for
  • WindowListener
  • Displaying Images in Labels
  • Changing Fonts

2
Week 08 - b
  • BordersDrawing in PanelsJCheckBox,
    ItemListenerJRadioButton, ButtonGroupJComboBox

3
Sample Programs Referenced(code located in
course folder)
  • BorderedTemperature
  • ColorfulDots
  • CheckBoxDemo
  • with without pictures
  • RadioButtonDemo
  • with without pictures
  • ComboBoxDemo
  • Restaurant Menu
  • Whiteboard

4
Adding Borders to Panels
  • Border object describes how to draw
  • Empty Border -- Empty space around panel
  • Line Border -- Plain, Etched, Beveled
  • Titled Border -- Add text to above
  • import javax.swing.border.
  • . . .
  • Border line BorderFactory.createLineBorder(c,w)
  • // c a Color, w width in pixels
  • Border etched BorderFactory.createEtchedBorder()
  • Border titled BorderFactory.createTitledBorder(l
    ine,"Title")
  • samplePanel.setBorder(titled)

5
Temperature Conversion(with Borders)
  • Run BorderedTemperature in BlueJ
  • Changed Etched border to Thick RED Line

6
Drawing in a JFrame (1 of 3)
  • Created JFrame in main program
  • Adding ColorfulDots Object to Frame
  • Instead of paint method used in Applets
  • Graphics g container.getGraphics()
  • import javax.swing.
  • /
  • Create ColorfulDots object.
  • _at_author Dave Hannay
  • _at_version 2/25/2004
  • /
  • public class ColorfulDotsClient
  • public static void main(String args)
  • JFrame myFrame new JFrame()
  • myFrame.setTitle("CONFETTI")
  • myFrame.setContentPane(new ColorfulDots())
  • myFrame.pack() // as opposed to setSize

7
Drawing in a JFrame (2 of 3)
  • import javax.swing.
  • import java.awt.event.
  • import java.awt.
  • // Dave Hannay 2/25/2004
  • class ColorfulDots extends JPanel implements
    ActionListener
  • JButton show new JButton("show")
  • JButton erase new JButton("erase")
  • JPanel theButtons new JPanel()
  • JPanel board new JPanel()
  • ColorfulDots()
  • setLayout(new BorderLayout())
  • theButtons.add(show) theButtons.add(erase)
  • add(theButtons, BorderLayout.NORTH)
  • board.setPreferredSize(new Dimension(200,
    200))
  • board.setBackground(Color.white)
  • add(board, BorderLayout.CENTER)
  • show.addActionListener(this)
  • erase.addActionListener(this)
  • // end of ColorfulDots constructor

8
Drawing in a JFrame (3 of 3)
  • public void actionPerformed(ActionEvent e)
  • Object source e.getSource()
  • if (source show)
  • Graphics g board.getGraphics()
  • int red (int)(256Math.random())
  • int green (int)(256Math.random())
  • int blue (int)(256Math.random())
  • g.setColor(new Color(red,green,blue))
  • g.fillOval((int)(200Math.random()),(int)(20
    0Math.random()),5,5)
  • // end of show button
  • else if (source erase)
  • board.repaint() // in this case, erases
    graphics
  • // end of actionPerformed method
  • // end of ColorfulDots class

9
CheckBoxes
  • Used to allow user to select one or more items
  • Checkbox always has a built-in label
  • Program often needs to react to the user
    selection(s)
  • Declaration example
  • JCheckBox brakes new JCheckBox(Power Brakes)
  • JCheckBox steering new JCheckBox(Power
    Steering)
  • JCheckBox ac new JCheckBox(Air Conditioning)
  • container.add(brakes)
  • container.add(steering)
  • container.add(ac)

10
Programming Checkboxes
  • Program is declared with implements ItemListener
  • Checkbox is registered by calling addItemListener
  • Event is handled using itemStateChanged argument
    type ItemEvent
  • The ItemEvent argument is used to tell which item
    triggered the event by calling getSource

11
Processing Checkboxes
  • CheckboxDemo
  • Run version without pictures first in BlueJ
  • Simplified version on next slides

12
CheckBoxDemo (1 of 3)
  • // Dave Hannay 2/22/2004
  • import java.awt.
  • import java.awt.event.
  • import javax.swing.
  • public class CheckBoxDemo extends JFrame
    implements ItemListener
  • JCheckBox chinButton new JCheckBox("Chin")
  • JCheckBox glassesButton new
    JCheckBox("Glasses")
  • JCheckBox hairButton new JCheckBox("Hair")
  • JCheckBox teethButton new JCheckBox("Teeth")
  • JTextField description new JTextField(20)
  • public CheckBoxDemo()
  • setDefaultCloseOperation( EXIT_ON_CLOSE )
  • Container frame this.getContentPane()
  • // Register a listener for the check boxes.
  • chinButton.addItemListener(this)
  • glassesButton.addItemListener(this)
  • hairButton.addItemListener(this)

13
CheckBoxDemo (2 of 3)
  • // Put the check boxes in a column in a panel
  • JPanel checkPanel new JPanel()
  • checkPanel.setLayout(new GridLayout(0, 1))
  • checkPanel.add(chinButton)
  • checkPanel.add(glassesButton)
  • checkPanel.add(hairButton)
  • checkPanel.add(teethButton)
  • frame.setLayout(new FlowLayout())
  • frame.add(checkPanel)
  • frame.add(description)
  • pack()
  • // end CheckBoxDemo constructor

14
CheckBoxDemo (3 of 3)
  • / Listens to the check boxes. /
  • public void itemStateChanged(ItemEvent e)
  • // don't care which one was selected in this
    case...
  • String descStr ""
  • if (chinButton.isSelected())
  • descStr " chin "
  • if (glassesButton.isSelected())
  • descStr " glasses "
  • if (hairButton.isSelected())
  • descStr " hair "
  • if (teethButton.isSelected())
  • descStr " teeth "
  • description.setText(descStr)
  • // end of itemStateChanged method
  • // end of CheckBoxDemo class

15
Radio ButtonsStation Presets
16
Radio Buttons
  • A group in which only one item can be selected at
    a time
  • Implemented using a Java ButtonGroup
  • Items are declared initially as selected (true)
    or unselected (false)
  • Example
  • ButtonGroup gender
  • JRadioButton maleCheck new JRadioButton(Male)
  • JRadioButton femaleCheck new JRadioButton(Femal
    e)
  • gender.add(maleCheck)
  • gender.add(femaleCheck)
  • maleCheck.setSelected(true) // only one can be
    true

17
Processing Radio Buttons
  • RadioButtonDemo
  • Run no images version first in BlueJ
  • Simplified version on next slides

18
RadioButtonDemo (1 of 3)
  • import java.awt.
  • import java.awt.event.
  • import javax.swing.
  • public class RadioButtonDemo extends JFrame
    implements ActionListener
  • private JRadioButton birdButton new
    JRadioButton("Bird")
  • private JRadioButton catButton new
    JRadioButton("Cat")
  • private JRadioButton dogButton new
    JRadioButton("Dog")
  • private JRadioButton rabbitButton new
    JRadioButton("Rabbit")
  • private JRadioButton pigButton new
    JRadioButton("Pig")
  • private JTextField description new
    JTextField(6)
  • public RadioButtonDemo()
  • Container frame this.getContentPane()
  • // Group the radio buttons.
  • ButtonGroup group new ButtonGroup()
  • group.add(birdButton)
  • group.add(catButton)

19
RadioButtonDemo (2 of 3)
  • // Register a listener for the radio buttons.
  • birdButton.addActionListener(this)
  • catButton.addActionListener(this)
  • dogButton.addActionListener(this)
  • rabbitButton.addActionListener(this)
  • pigButton.addActionListener(this)
  • // Put the radio buttons in a column in a
    panel
  • JPanel radioPanel new JPanel()
  • radioPanel.setLayout(new GridLayout(0, 1))
  • radioPanel.add(birdButton)
  • radioPanel.add(catButton)
  • radioPanel.add(dogButton)
  • radioPanel.add(rabbitButton)
  • radioPanel.add(pigButton)
  • frame.setLayout(new FlowLayout())
  • frame.add(radioPanel)
  • frame.add(picture)

20
RadioButtonDemo (3 of 3)
  • / Listens to the radio buttons. /
  • public void actionPerformed(ActionEvent e)
  • Object btn e.getSource()
  • if (birdButton.isSelected())
  • description.setText("BIRD")
  • else if (catButton.isSelected())
  • description.setText("CAT")
  • else if (dogButton.isSelected())
  • description.setText("DOG")
  • else if (rabbitButton.isSelected())
  • description.setText("RABBIT")
  • else if (pigButton.isSelected())
  • description.setText("PIG")
  • // end of actionPerformed method
  • // end of RadioButtonDemo class

21
Java BREAK
22
ComboBoxes
  • Similar to Radio Buttons
  • Allows selection of one of several choices
  • Features Beyond Radio Buttons
  • Choose text rather than true/false
  • Can enter new text at run time
  • See next slide then
  • run ComboBoxDemo in BlueJ

23
Using a ComboBox
  • String choices
  • "Chevy",
  • "Ford",
  • "Jeep",
  • "Toyota",
  • "Honda"
  • JComboBox choiceList new JComboBox(choices)
  • choiceList.addActionListener(this)
  • choiceList.setEditable(true) // false to
    restrict
  • In ActionListener
  • JComboBox cb (JComboBox)e.getSource()
  • String newSelection (String)cb.getSelectedItem()

24
ComboBoxDemo (1 of 4)
  • import java.awt.
  • import java.awt.event.
  • import javax.swing.
  • import javax.swing.border.
  • import java.util.
  • import java.text.
  • public class ComboBoxDemo extends JFrame
    implements ActionListener
  • JLabel result
  • String currentPattern
  • String patternExamples
  • "dd MMMMM yyyy",
  • "dd.MM.yy",
  • "MM/dd/yy",
  • "yyyy.MM.dd G 'at' hhmmss z",
  • "EEE, MMM d, ''yy",
  • "hmm a",
  • "HmmssSSS",
  • "Kmm a,z",

25
ComboBoxDemo (2 of 4)
  • public ComboBoxDemo()
  • Container frame getContentPane()
  • this.setSize(250,150)
  • currentPattern patternExamples0
  • // Set up the UI for selecting a pattern.
  • JLabel patternLabel1 new JLabel("Enter the
    pattern string or")
  • JLabel patternLabel2 new JLabel("select one
    from the list")
  • JComboBox patternList new
    JComboBox(patternExamples)
  • patternList.addActionListener(this)
  • patternList.setEditable(true)
  • patternList.setAlignmentX(Component.LEFT_ALIGN
    MENT)
  • // Create the UI for displaying result
  • JLabel resultLabel new JLabel("Current
    Date/Time")
  • result new JLabel(" ")

26
ComboBoxDemo (3 of 4)
  • // Lay out everything
  • JPanel patternPanel new JPanel()
  • patternPanel.setLayout(new GridLayout(0,1))
  • patternPanel.add(patternLabel1)
  • patternPanel.add(patternLabel2)
  • patternPanel.add(patternList)
  • JPanel resultPanel new JPanel()
  • resultPanel.setLayout(new GridLayout(0,1))
  • resultPanel.add(resultLabel)
  • resultPanel.add(result)
  • frame.setLayout(new FlowLayout())
  • patternPanel.setAlignmentX(Component.LEFT_ALIG
    NMENT)
  • resultPanel.setAlignmentX(Component.LEFT_ALIGN
    MENT)
  • frame.add(patternPanel)
  • frame.add(resultPanel)
  • reformat()

27
ComboBoxDemo (4 of 4)
  • / Formats and displays today's date. /
  • public void reformat()
  • Date today new Date()
  • SimpleDateFormat formatter new
    SimpleDateFormat(currentPattern)
  • try
  • String dateString formatter.format(today)
  • result.setForeground(Color.black)
  • result.setText(dateString)
  • catch (IllegalArgumentException iae)
  • result.setForeground(Color.red)
  • result.setText("Error "
    iae.getMessage())
  • // end try/catch
  • // end of reformat method
  • public void actionPerformed(ActionEvent e)
  • JComboBox cb (JComboBox)e.getSource()
  • String newSelection (String)cb.getSelectedIt
    em()
  • currentPattern newSelection
  • reformat()

28
Restaurant Menu
  • Check Boxes
  • Radio Buttons
  • Run in BlueJ
  • then look at code

29
Restaurant Menu (1 of 7)
  • import java.awt.
  • import java.awt.event.
  • import javax.swing.
  • import java.text.
  • /
  • Customer Selects Dinner from a Menu
  • _at_author Dave Hannay
  • _at_version 19 February 2003
  • /
  • public class Menu extends JFrame
  • implements ItemListener
  • JPanel p, p2
  • JRadioButton radSteak new JRadioButton("Steak"
    ),
  • radChicken new
    JRadioButton("Chicken"),
  • radTacos new JRadioButton("Tacos"
    ),
  • radVeggie new JRadioButton("Veget
    arian")
  • JTextField txtChicken new JTextField("9.95",6)
    ,
  • txtVeggie new JTextField("12.95",6)
    ,
  • txtSteak new JTextField("14.95",6),

30
Restaurant Menu (2 of 7)
  • JCheckBox chkSoup new JCheckBox("Soup"),
  • chkSalad new JCheckBox("Salad"),
  • chkBeverage new JCheckBox("Beverage"
    ),
  • chkDessert new JCheckBox("Dessert")
  • JTextField txtSoup new JTextField("2.50",6),
  • txtSalad new JTextField("3.00",6),
  • txtBeverage new JTextField("1.99",6
    ),
  • txtDessert new JTextField("5.50",6)
  • JLabel lblTotal new JLabel("Total Bill Comes
    Out Here!")
  • double mainCourse 0.00,
  • soup 0.00,
  • salad 0.00,
  • beverage 0.00,
  • dessert 0.00,
  • totalBill 0.00
  • NumberFormat currency
    NumberFormat.getCurrencyInstance()

31
Restaurant Menu (3 of 7)
  • public Menu()
  • setTitle("Chez UpperClass")
  • setFont(new Font("Helvetica", Font.BOLD,
    14))
  • Container frame this.getContentPane()
  • ButtonGroup grpEntree new ButtonGroup()
  • grpEntree.add(radChicken)
  • grpEntree.add(radSteak)
  • grpEntree.add(radTacos)
  • grpEntree.add(radVeggie)
  • frame.setLayout(new GridLayout(8, 1))
  • pan new JPanel()
  • pan.add(new JLabel(
  • "Select one Main Course, add
    Soup/Salad/Beverage/Dessert"))
  • frame.add(pan)

32
Restaurant Menu (4 of 7)
  • pan new JPanel()
  • pan.add(radChicken) pan.add(txtChicken)
  • frame.add(pan)
  • pan new JPanel()
  • pan.add(radVeggie) pan.add(txtVeggie)
  • frame.add(pan)
  • pan new JPanel()
  • pan.add(radSteak) pan.add(txtSteak)
  • frame.add(pan)
  • pan new JPanel()
  • pan.add(radTacos) pan.add(txtTacos)
  • frame.add(pan)
  • pan new JPanel()
  • pan.add(chkSoup) pan.add(txtSoup)
  • pan.add(chkSalad) pan.add(txtSalad)
  • frame.add(pan)
  • pan new JPanel()
  • pan.add(chkBeverage) pan.add(txtBeverage)
  • pan.add(chkDessert) pan.add(txtDessert)

33
Restaurant Menu (5 of 7)
  • // Register the Component Listener
  • radChicken.addActionListener(this)
  • radVeggie.addActionListener(this)
  • radSteak.addActionListener(this)
  • radTacos.addActionListener(this)
  • chkSoup.addItemListener(this)
  • chkSalad.addItemListener(this)
  • chkBeverage.addItemListener(this)
  • chkDessert.addItemListener(this)
  • // end of Menu constructor
  • // Respond to Action/Item Events clicking on
    buttons
  • public void actionPerformed (ActionEvent e)
  • JRadioButton choice (JRadioButton)(e.getSour
    ce())
  • if (choiceradChicken)
  • mainCourse Double.parseDouble(txtChicken.g
    etText())
  • if (choiceradSteak)
  • mainCourse Double.parseDouble(txtSteak.get
    Text())
  • if (choiceradVeggie)
  • mainCourse Double.parseDouble(txtVeggie.ge
    tText())

34
Restaurant Menu (6 of 7)
  • public void itemStateChanged (ItemEvent e)
  • JCheckBox box (JCheckBox) (e.getSource())
  • if (boxchkSoup)
  • if (box.isSelected())
  • soup Double.parseDouble(txtSoup.getText(
    ))
  • else
  • soup 0.00
  • if (boxchkSalad)
  • if (box.isSelected())
  • salad Double.parseDouble(txtSalad.getTex
    t())
  • else
  • salad 0.00
  • if (boxchkBeverage)
  • if (box.isSelected())
  • beverage Double.parseDouble(txtBeverage.
    getText())
  • else
  • beverage 0.00
  • if (boxchkDessert)
  • if (box.isSelected())

35
Restaurant Menu (7 of 7)
  • private void setTotal()
  • totalBill mainCourse
  • soup
  • salad
  • beverage
  • dessert
  • lblTotal.setText("Total (before Tax and Tip)
    "
  • currency.format(totalBill)
    )
  • // end of setTotal method
  • // end of Menu class

36
Virtual Whiteboard (1 of 3)Combining
RadioButtons with Drawing
  • import javax.swing.
  • import java.awt.event.
  • import java.awt.
  • // Dave Hannay 2/25/2004
  • class Whiteboard extends JPanel
  • implements MouseMotionListener
  • JRadioButton radRed new JRadioButton("Red")
  • JRadioButton radGreen new JRadioButton("Green"
    )
  • JRadioButton radBlue new JRadioButton("Blue")
  • JRadioButton radBlack new JRadioButton("Black"
    )
  • ButtonGroup colorGroup new ButtonGroup()
  • JPanel theButtons new JPanel()
  • JPanel board new JPanel()
  • Point start new Point()

37
Virtual Whiteboard (2 of 3)
  • public Whiteboard()
  • setLayout(new BorderLayout())
  • radRed.setBackground(Color.RED)
  • radGreen.setBackground(Color.GREEN)
  • radBlue.setBackground(Color.BLUE)
  • radBlack.setBackground(Color.BLACK)
  • radBlue.setForeground(Color.WHITE)
  • radBlack.setForeground(Color.WHITE)
  • radBlack.setSelected(true)
  • theButtons.add(radRed)
  • theButtons.add(radGreen)
  • theButtons.add(radBlue)
  • theButtons.add(radBlack)
  • colorGroup.add(radRed)
  • colorGroup.add(radGreen)
  • colorGroup.add(radBlue)
  • colorGroup.add(radBlack)
  • add(theButtons, BorderLayout.SOUTH)
  • board.setPreferredSize(new Dimension(400,
    400))

38
Virtual Whiteboard (3 of 3)
  • public void mouseDragged(MouseEvent e)
  • int x e.getX(), y e.getY()
  • Graphics g board.getGraphics()
  • if (radRed.isSelected())
  • g.setColor(Color.RED)
  • else if (radGreen.isSelected())
  • g.setColor(Color.GREEN)
  • else if (radBlue.isSelected())
  • g.setColor(Color.BLUE)
  • else if (radBlack.isSelected())
  • g.setColor(Color.BLACK)
  • g.fillOval(x,y,5,5)
  • g.dispose()
  • waitAWhile(5)
  • // end of mouseDragged method
  • public void mouseMoved(MouseEvent e)
  • // end of Whiteboard class

39
To the Computers...(may take 20 minutes)
  • Try the Whiteboard program
  • Drag from course folder to desktop
  • Add an Erase button
  • see ColorfulDots for how to implement it if you
    need help
  • Convert the RadioButtons in Restaurant to
    JComboBox
  • Will need to replace 4 textFields with one for
    price based on which Main Course was selected
  • Can use cboEntrees.getSelectedItem() for index to
    use in array of prices

40
Java BREAK
Write a Comment
User Comments (0)
About PowerShow.com