21 - Overview CS133: Developing Programming Principles...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Overview CS133: Developing Programming Principles Lecture 21 Buttons, Text Fields, and Containers Last Day: graphics, drawing, mouse clicks, and the Board class Today: buttons, action listeners, container classes and text fields CS 133 Course Notes Lecture 21, Slide 2 1 2 Button Example import javax.swing.*; import java.awt.*; import java.awt.event.*; public class PushMe extends JFrame implements ActionListener { // on next two slides } } CS 133 Course Notes Lecture 21, Slide 3 CS 133 Button Example public PushMe() { JButton pushMe = new JButton("Push Me"); pushMe.addActionListener(this); Container content = getContentPane(); content.add(pushMe); setDefaultCloseOperation(EXIT_ON_CLOSE); pack(); setVisible(true); Course Notes Lecture 21, Slide 4 3 4 Button Example public void actionPerformed(ActionEvent e) { System.out.println("Stop that!"); } Class JButton The JButton class is a Swing component that provides a simple “push button”: public JButton(String label) When the button is pressed an event is delivered to its listener. CS 133 Course Notes Lecture 21, Slide 6 public static void main(String args) { PushMe test = new PushMe(); } CS 133 Course Notes Lecture 21, Slide 5 5 6 PushMe implements ActionListener In this example, the PushMe class handles events for the button by specifying itself as the button’s actionListener: pushMe.addActionListener(this) Implementing ActionListener Classes implementing the ActionListener interface must support the method: public void actionPerformed(ActionEvent e) { … } CS 133 Course Notes Lecture 21, Slide 7 CS 133 Course Notes Lecture 21, Slide 8 7 8 Two Buttons JButton pushMe = new JButton("Push Me"); pushMe.addActionListener(this); JButton pushMe2 = new JButton("Push Me Too"); pushMe2.addActionListener(this); JPanel buttonPanel = new JPanel(); buttonPanel.setLayout(new FlowLayout()); buttonPanel.add(pushMe); buttonPanel.add(pushMe2); Two Buttons public void actionPerformed(ActionEvent e) { if (e.getActionCommand().equals("Push Me")) { System.out.println("Stop that!"); } else if ( e.getActionCommand().equals("Push Me Too")) { System.out.println("Ouch!"); } } CS 133 Course Notes Lecture 21, Slide 10 CS 133 Course Notes Lecture 21, Slide 9 9 10 Layout Managers A layout manager is a special kind of object that is responsible for organizing the contents of a JFrame, JPanel, or other container class. Three useful layout managers are: FlowLayout BorderLayout GridLayout CS 133 Course Notes Lecture 21, Slide 11 FlowLayout • A FlowLayout arranges components in a line from left to right in the order they were added, until the line is full. • Lines are centered. • Each component is allowed to assume its preferred size. CS 133 Course Notes Lecture 21, Slide 12 11 12 BorderLayout A BorderLayout organizes components into one of five regions: NORTH WEST CENTER SOUTH EAST GridLayout A GridLayout organizes components into a rectangular NxM grid: public GridLayout(int rows, int cols) 0 0 1 1 2 3 4 5 CS 133 Course Notes Lecture 21, Slide 13 CS 133 Course Notes Lecture 21, Slide 14 13 14 ActionEvent An ActionEvent is generated by a component to describe an event, such as a button when it is pressed. public String getActionCommand() Method to return the command String associated with this action. For a button, this is usually the label. CS 133 Course Notes Lecture 21, Slide 15 Adding a Text Field public class PushMe extends JFrame implements ActionListener { private JTextField text; // etc. } CS 133 Course Notes Lecture 21, Slide 16 15 16 Adding a Text Field JPanel buttonPanel = new JPanel(); buttonPanel.setLayout(new FlowLayout()); buttonPanel.add(pushMe); buttonPanel.add(pushMe2); text = new JTextField(40); text.setEditable(false); Container content = getContentPane(); content.setLayout(new BorderLayout()); content.add(text, BorderLayout.NORTH); content.add(buttonPanel, BorderLayout.SOUTH); CS 133 Course Notes Lecture 21, Slide 17 Adding a Text Field public void actionPerformed(ActionEvent e) { if (e.getActionCommand().equals("Push Me")){ text.setText("Stop that!"); } else if ( e.getActionCommand().equals("Push Me Too")){ text.setText("Ouch!"); } } CS 133 Course Notes Lecture 21, Slide 18 17 18 JTextField A JTextField component provides for the display and editing of a single line of text. public public public public JTextField(int columns) String getText() void setText(String text) void setEditable(boolean b) Component Class Hierarchy Component Container Window Frame JFrame JComponent CS 133 Course Notes Lecture 21, Slide 19 CS 133 Course Notes Lecture 21, Slide 20 19 20 JComponent Class Hierarchy JComponent JLabel A JLabel component provides for the display (but not editing) of a single line of text. public JLabel(String text) public String getText() public void setText(String text) JPanel JLabel AbstractButton JTextComponent JMenuBar JTextArea JTextField JButton CS 133 Course Notes Lecture 21, Slide 21 CS 133 Course Notes Lecture 21, Slide 22 21 22 JTextArea A JTextArea component provides for the display and editing of multiple lines of text. public public public public JTextArea(int rows,int columns) String getText() void setText(String text) void setEditable(boolean b) Summary • • • • • Buttons Action listeners Layout managers Text Fields The Swing class hierarchy public void setLineWrap(boolean wrap) CS 133 Course Notes Lecture 21, Slide 23 CS 133 Course Notes Lecture 21, Slide 24 23 24 ERROR: undefined OFFENDING COMMAND: STACK: ...
View Full Document

Ask a homework question - tutors are online