Java Software Solutions - Chapter 5: Conditionals and Loops

Now we will examine programming statements that allow us to:

make decisions

repeat processing steps in a loop

Chapter 5 focuses on:

boolean expressions

the if and if-else statements

comparing data

while loops

iterators

more drawing techniques

more GUI components

 

pptx114 trang | Chuyên mục: Java | Chia sẻ: dkS00TYs | Lượt xem: 3015 | Lượt tải: 0download
Tóm tắt nội dung Java Software Solutions - Chapter 5: Conditionals and Loops, để xem tài liệu hoàn chỉnh bạn click vào nút "TẢI VỀ" ở trên
mension(200, 80)); setBackground (Color.cyan); add (label); add (buttonPanel); } continue Copyright © 2012 Pearson Education, Inc. continue //***************************************************************** // Represents a listener for both buttons. //***************************************************************** private class ButtonListener implements ActionListener { //-------------------------------------------------------------- // Determines which button was pressed and sets the label // text accordingly. //-------------------------------------------------------------- public void actionPerformed (ActionEvent event) { if (event.getSource() == left) label.setText("Left"); else label.setText("Right"); } } } Outline Boolean Expressions The if Statement Comparing Data The while Statement Iterators The ArrayList Class Determining Event Sources Check Boxes and Radio Buttons Copyright © 2012 Pearson Education, Inc. Check Boxes A check box is a button that can be toggled on or off It is represented by the JCheckBox class Unlike a push button, which generates an action event, a check box generates an item event whenever it changes state The ItemListener interface is used to define item event listeners A check box calls the itemStateChanged method of the listener when it is toggled Copyright © 2012 Pearson Education, Inc. Check Boxes Let's examine a program that uses check boxes to determine the style of a label's text string It uses the Font class, which embodies a character font's: family name (such as Times or Courier) style (bold, italic, or both) font size See StyleOptions.java See StyleOptionsPanel.java Copyright © 2012 Pearson Education, Inc. Copyright © 2012 Pearson Education, Inc. //******************************************************************** // StyleOptions.java Author: Lewis/Loftus // // Demonstrates the use of check boxes. //******************************************************************** import javax.swing.JFrame; public class StyleOptions { //----------------------------------------------------------------- // Creates and presents the program frame. //----------------------------------------------------------------- public static void main (String[] args) { JFrame frame = new JFrame ("Style Options"); frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); StyleOptionsPanel panel = new StyleOptionsPanel(); frame.getContentPane().add (panel); frame.pack(); frame.setVisible(true); } } Copyright © 2012 Pearson Education, Inc. //******************************************************************** // StyleOptions.java Author: Lewis/Loftus // // Demonstrates the use of check boxes. //******************************************************************** import javax.swing.JFrame; public class StyleOptions { //----------------------------------------------------------------- // Creates and presents the program frame. //----------------------------------------------------------------- public static void main (String[] args) { JFrame frame = new JFrame ("Style Options"); frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); StyleOptionsPanel panel = new StyleOptionsPanel(); frame.getContentPane().add (panel); frame.pack(); frame.setVisible(true); } } Copyright © 2012 Pearson Education, Inc. //******************************************************************** // StyleOptionsPanel.java Author: Lewis/Loftus // // Demonstrates the use of check boxes. //******************************************************************** import javax.swing.*; import java.awt.*; import java.awt.event.*; public class StyleOptionsPanel extends JPanel { private JLabel saying; private JCheckBox bold, italic; continue Copyright © 2012 Pearson Education, Inc. continue //----------------------------------------------------------------- // Sets up a panel with a label and some check boxes that // control the style of the label's font. //----------------------------------------------------------------- public StyleOptionsPanel() { saying = new JLabel ("Say it with style!"); saying.setFont (new Font ("Helvetica", Font.PLAIN, 36)); bold = new JCheckBox ("Bold"); bold.setBackground (Color.cyan); italic = new JCheckBox ("Italic"); italic.setBackground (Color.cyan); StyleListener listener = new StyleListener(); bold.addItemListener (listener); italic.addItemListener (listener); add (saying); add (bold); add (italic); setBackground (Color.cyan); setPreferredSize (new Dimension(300, 100)); } continue Copyright © 2012 Pearson Education, Inc. continue //***************************************************************** // Represents the listener for both check boxes. //***************************************************************** private class StyleListener implements ItemListener { //-------------------------------------------------------------- // Updates the style of the label font style. //-------------------------------------------------------------- public void itemStateChanged (ItemEvent event) { int style = Font.PLAIN; if (bold.isSelected()) style = Font.BOLD; if (italic.isSelected()) style += Font.ITALIC; saying.setFont (new Font ("Helvetica", style, 36)); } } } Radio Buttons A group of radio buttons represents a set of mutually exclusive options – only one can be selected at any given time When a radio button from a group is selected, the button that is currently "on" in the group is automatically toggled off To define the group of radio buttons that will work together, each radio button is added to a ButtonGroup object A radio button generates an action event Copyright © 2012 Pearson Education, Inc. Radio Buttons Let's look at a program that uses radio buttons to determine which line of text to display See QuoteOptions.java See QuoteOptionsPanel.java Copyright © 2012 Pearson Education, Inc. Copyright © 2012 Pearson Education, Inc. //******************************************************************** // QuoteOptions.java Author: Lewis/Loftus // // Demonstrates the use of radio buttons. //******************************************************************** import javax.swing.JFrame; public class QuoteOptions { //----------------------------------------------------------------- // Creates and presents the program frame. //----------------------------------------------------------------- public static void main (String[] args) { JFrame frame = new JFrame ("Quote Options"); frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); QuoteOptionsPanel panel = new QuoteOptionsPanel(); frame.getContentPane().add (panel); frame.pack(); frame.setVisible(true); } } Copyright © 2012 Pearson Education, Inc. //******************************************************************** // QuoteOptions.java Author: Lewis/Loftus // // Demonstrates the use of radio buttons. //******************************************************************** import javax.swing.JFrame; public class QuoteOptions { //----------------------------------------------------------------- // Creates and presents the program frame. //----------------------------------------------------------------- public static void main (String[] args) { JFrame frame = new JFrame ("Quote Options"); frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); QuoteOptionsPanel panel = new QuoteOptionsPanel(); frame.getContentPane().add (panel); frame.pack(); frame.setVisible(true); } } Copyright © 2012 Pearson Education, Inc. //******************************************************************** // QuoteOptionsPanel.java Author: Lewis/Loftus // // Demonstrates the use of radio buttons. //******************************************************************** import javax.swing.*; import java.awt.*; import java.awt.event.*; public class QuoteOptionsPanel extends JPanel { private JLabel quote; private JRadioButton comedy, philosophy, carpentry; private String comedyQuote, philosophyQuote, carpentryQuote; //----------------------------------------------------------------- // Sets up a panel with a label and a set of radio buttons // that control its text. //----------------------------------------------------------------- public QuoteOptionsPanel() { comedyQuote = "Take my wife, please."; philosophyQuote = "I think, therefore I am."; carpentryQuote = "Measure twice. Cut once."; quote = new JLabel (comedyQuote); quote.setFont (new Font ("Helvetica", Font.BOLD, 24)); continue Copyright © 2012 Pearson Education, Inc. continue comedy = new JRadioButton ("Comedy", true); comedy.setBackground (Color.green); philosophy = new JRadioButton ("Philosophy"); philosophy.setBackground (Color.green); carpentry = new JRadioButton ("Carpentry"); carpentry.setBackground (Color.green); ButtonGroup group = new ButtonGroup(); group.add (comedy); group.add (philosophy); group.add (carpentry); QuoteListener listener = new QuoteListener(); comedy.addActionListener (listener); philosophy.addActionListener (listener); carpentry.addActionListener (listener); add (quote); add (comedy); add (philosophy); add (carpentry); setBackground (Color.green); setPreferredSize (new Dimension(300, 100)); } continue Copyright © 2012 Pearson Education, Inc. continue //***************************************************************** // Represents the listener for all radio buttons //***************************************************************** private class QuoteListener implements ActionListener { //-------------------------------------------------------------- // Sets the text of the label depending on which radio // button was pressed. //-------------------------------------------------------------- public void actionPerformed (ActionEvent event) { Object source = event.getSource(); if (source == comedy) quote.setText (comedyQuote); else if (source == philosophy) quote.setText (philosophyQuote); else quote.setText (carpentryQuote); } } } Summary Chapter 5 focused on: boolean expressions the if and if-else statements comparing data while loops iterators more drawing techniques more GUI components Copyright © 2012 Pearson Education, Inc. 

File đính kèm:

  • pptxLESSON_05_Conditionals and Loops.pptx
Tài liệu liên quan