Day 14 A little more GUI - PowerPoint PPT Presentation

1 / 14
About This Presentation
Title:

Day 14 A little more GUI

Description:

Radio Buttons. Recall. Recall the MenuExample.java program from yesterday. ... The extra step that we need to perform for radio buttons is to put them in a group. ... – PowerPoint PPT presentation

Number of Views:23
Avg rating:3.0/5.0
Slides: 15
Provided by: foxr
Category:
Tags: gui | day | more

less

Transcript and Presenter's Notes

Title: Day 14 A little more GUI


1
Day 14 A little more GUI
  • Today well take a few minutes to say more about
  • Text fields
  • Checkboxes
  • Radio Buttons

2
Recall
  • Recall the MenuExample.java program from
    yesterday.
  • It used a dialog box to read in text, a submenu
    of radio buttons to determine the font name, and
    a submenu of checkboxes to determine the font
    style.

3
An alternative
  • Today well look at how to read in a string from
    a text field
  • Also, well use JCheckBox instead of
    JCheckBoxMenuItem
  • and JRadioButton instead of JRadioButtonMenuItem.
  • Our program will display the check boxes and
    radio buttons directly on the screen rather than
    hiding them under a menu

4
Download TextFieldExample.java
5
JTextField
  • Weve created JTextFields before, but we have not
    given them action listeners. This can be done as
    followsprivate JTextField jtfNamejtfName
    new JTextField(12)
  • jtfName.addActionListener(this)
  • controlPanel.add(jtfName)

6
JTextField
  • An ActionEvent will be sent to the listener
    whenever focus is on the text field and the enter
    key is hit. We handle this event in our
    actionPerformed methodpublic void
    actionPerformed(ActionEvent e)
  • if (e.getSource() jtfName)
  • userName jtfName.getText()
  • panel.repaint()

7
JCheckBox
  • A JCheckBox is used like a JCheckBoxMenuItem.
  • The main difference is that it extends JComponent
    rather than JMenuItem, so it is intended for use
    outside of menus.JPanel checkBoxPanel new
    JPanel()
  • checkBoxPanel.setLayout(new FlowLayout())jcbBol
    d new JCheckBox("Bold")
  • jcbBold.setMnemonic('B')checkBoxPanel.add(jcbBo
    ld)jcbBold.addActionListener(this)

8
JCheckBox
  • As with a JCheckBoxMenuItem, the action listener
    will be notified when the JCheckBox is selected
    or deselectedpublic void actionPerformed(Action
    Event e)
  • if (e.getSource() jcbBold)
  • boldFont jcbBold.isSelected()
  • panel.repaint()

9
JRadioButton
  • JRadioButton is likewise very similar to
    JRadioButtonMenuItem.
  • Again, the main difference is that you use
    JRadioButtons outside of menus.

10
JRadioButton
  • JPanel radioBoxPanel new JPanel()
  • radioBoxPanel.setLayout(new FlowLayout())
  • jrbFontCourierNew new JRadioButton("Courier
    New")
  • jrbFontCourierNew.setMnemonic('C')
  • jrbFontDialog new JRadioButton(Dialog")jrbFo
    ntDialog.setMnemonic('D')
  • jrbFontTimesNewRoman
  • jrbFontVivaldi

11
JRadioButton
  • This code adds the four radio buttons to a panel.
    It also draws a border around the
    panelradioBoxPanel.add(jrbFontCourierNew)
  • radioBoxPanel.add(jrbFontDialog)
  • radioBoxPanel.add(jrbFontTimesNewRoman)
  • radioBoxPanel.add(jrbFontVivaldi)
  • radioBoxPanel.setBorder(
  • BorderFactory.createLineBorder(Color.BLACK))

12
JRadioButton
  • As always, we need to add action
    listenersjrbFontCourierNew.addActionListener(th
    is)
  • jrbFontDialog.addActionListener(this)
  • jrbFontTimesNewRoman.addActionListener(this)
  • jrbFontVivaldi.addActionListener(this)

13
JRadioButton
  • The extra step that we need to perform for radio
    buttons is to put them in a group.ButtonGroup
    btg new ButtonGroup()
  • btg.add(jrbFontCourierNew)
  • btg.add(jrbFontDialog)
  • btg.add(jrbFontTimesNewRoman)
  • btg.add(jrbFontVivaldi)
  • jrbFontDialog.setSelected(true)

14
JRadioButton
  • The action listener code can be written as
    usualpublic void actionPerformed(ActionEvent
    e)
  • if (source jrbFontCourierNew
    jrbFontCourierNew.isSelected())
  • fontName "Courier New" else if
  • panel.repaint()
Write a Comment
User Comments (0)
About PowerShow.com