Вы находитесь на странице: 1из 29

`  Æ

@  
   
µ   


   

  

| 
   

         !"  " # $µ"" # #  

Objectives:
ƕ Get a more systematic introduction to basic swing
components, their methods, and events they
generate.
ƕ Components discussed:
ƕ JLabel JSlider
ƕ JButton JTextField
ƕ JToggleButton JPasswordField
JTextArea
ƕ JCheckBox
ƕ JComboBox
2
GUI Components
ƕ Components are created using constructors:
JLabel guest = new JLabel ("GuestƑ);

ƕ To be usable a component must be added to the


applicationƎs Ɛcontent paneƑ or to another
component:

JPanel scorePanel = new JPanel();


scorePanel.add (guest);

<
GUI Events
ƕ Components (except JLabel) can generate events.
ƕ Events are captured and processed by ƐlistenersƑ
ƌ objects equipped to handle a particular type of
events.
ƕ Different types of events are processed by
different types of listeners.

R
Interfaces
ƕ An interface declares one or several public
methods.
ƕ An interface is similar to an abstract class
but none of its methods can have any
code.
ƕ A class declared to implement an interface
must have all the methods of that
interface defined.
ƕ A class can implement several interfaces.

Interfaces (contƎd)
ƕ An object of a class that implements an interface
gets a secondary data type of that interface (in
addition to the primary data type of the class).
ƕ Different types of ƐlistenersƑ are interfaces. The
same object can serve as different types of
listeners (as long as its class implements all the
corresponding interfaces).

~
Listeners Objects of this class are
public class GoHandler ƐGoHandlersƑ but also
implements ActionListener ActionListeners
{
...
public void actionPerformed
(ActionEvent e) Æ
  
  
{     
...  

} ¯¯¯
} JButton go = new JButton (ƐGoƑ);
go.addActionListener (new GoHandler ());
Æ 
  
 
ActionListener; a GoHandler
object qualifies. •
Listeners
ƕ When implementing an event listener,
programmers often use a private embedded
class that has access to all the fields of the
surrounding public class.
ƕ These, so called   classes can have
constructors and private fields, like any other
class.
ƕ Inner classes are accessible only in their outer
class.

ñ
Listeners (contƎd)
public class MyPanel extends JPanel{
private JButton go;
...
go = new JButton (ƐGoƑ);
go.addActionListener (new GoHandler ());
...
private class GoHandler implements ActionListener {
public void actionPerformed (ActionEvent e) {
go.setText(ƐStopƑ);
...
} go is accessible in constructors and
}
} methods of the inner class


Listeners
ƕ You donƎt have to capture all events.
ƕ If you donƎt want to deal with events from a
component, just donƎt attach a listener to it.
ƕ If you do want to capture events but forget to
add a listener, no events will be captured (a
common omission).

ô
Listeners
ƕ Listener methods take a corresponding
type of event as an argument.
ƕ Event objects have useful methods. For
example, getSource returns the object
that produced this event.
ƕ A MouseEvent has methods getX, getY.

ôô
Menus
ƕ You can add a JMenuBar object to JFrame or
JApplet.
ƕ You can add JMenu objects to a JMenuBar.
ƕ You can add other JMenus, JMenuItems,
JCheckBoxMenuItems,
JRadioButtonMenuItems, etc. to a JMenu.

ô2
GUI Components Review
ƕ Java Methods Appendix C contains brief
summaries of a few Swing components, their
constructors, methods, and events.
ƕ For a complete specification refer to the Java
Swing API docs.

ô<
JLabel
Constructors:
JLabel(String text);
JLabel(ImageIcon icon);
JLabel(String text, ImageIcon icon,
SwingConstants.LEFT);
// or CENTER,RIGHT,LEADING,TRAILING.
Methods:
void setText(String text);
void setIcon(ImageIcon icon);
Events: None

ôR
JButton
Constructors:
JButton(String text);
JButton(ImageIcon picture);
JButton(String text, ImageIcon picture);
Methods:
void addActionListener(ActionListener object)
void setText(String text);
void setActionCommand(String cmd);
void setIcon(ImageIcon icon);
void requestFocus();
Events:
class ... implements ActionListener{
public void actionPerformed(ActionEvent e) {
JButton b = (JButton)e.getSource();
String s = e.getActionCommand();
}
} ô
JCheckBox
Constructors:
JCheckBox(String text, boolean checked);
JCheckBox(ImageIcon icon, boolean checked);
JCheckBox(String text, ImageIcon icon,
boolean checked);
Methods:
void addActionListener(ActionListener object)
boolean isSelected()
void setSelected(boolean checked)
void setText(String text);
void setIcon(ImageIcon icon);
Events:
class ... implements ActionListener{
public void actionPerformed(ActionEvent e) {
JCheckBox b = (JCheckBox)e.getSource();
if (b == checkBoxô && b.isSelected())
...
} ô~
}
Layouts
ƕ A layout manager is a kind of ƐstrategyƑ for
placing components on the content pane or
another component (usually a panel).
ƕ In Java, the content pane and any GUI
component is a Container.
ƕ A layout is chosen by calling the containerƎs
setLayout method.

ô•
Layouts
ƕ Layouts are used to achieve some degree of
platform independence and scalability.
ƕ AWT/Swing support several layout managers.
Here we consider four: FlowLayout, GridLayout,
BorderLayout, BoxLayout.
ƕ These classes implement the
java.awt.LayoutManager interface.

ôñ
FlowLayout
ƕ Places components in a line as long as they fit,
then starts the next line.
ƕ Uses Ɛbest judgementƑ in spacing components.
Centers by default.
ƕ Lets each component assume its natural
(preferred) size.
ƕ Often used for placing buttons on panels.

ô
FlowLayout
Container c = getContentPane();
c.setLayout (new FlowLayout() );
c.add (new JButton ("Next Slide"));
c.add (new JButton ("Previous Slide"));
c.add (new JButton ("Back to Start"));
c.add (new JButton ("Exit"));

2
GridLayout
ƕ Splits the panel into a grid with given number of
rows and columns.
ƕ Places components into the grid cells.
ƕ Forces the size of each component to occupy the
whole cell.
ƕ Allows additional spacing between cells.


GridLayout (contƎd)
Container c = getContentPane();
c.setLayout (new GridLayout(<, 2, ô , 2
));
c.add (new JButton ("Next Slide"));   
 
  
c.add (new JButton ("Previous Slide")); 

c.add (new JButton ("Back to Start"));


c.add (new JButton (ƑLast Slide"));
c.add (new JButton ("Exit"));

22
BorderLayout
ƕ Divides the area into five regions and adds a component
to the specified region.

’ Æ

ƕ Forces the size of each


 Æ component to occupy the whole
 ’Æ  Æ
region.
 Æ

2<
BorderLayout (contƎd)
      !"

 #  $ #  !!"
¯  %$  &’  &!'$ #  ¯’ Æ!"
¯  %$  &
 &!'$ #  ¯ Æ!"
¯  %$  &$    &!'$ #  ¯ Æ!"
¯  %$  &#
 &!'$ #  ¯ Æ!"
¯  %$  &  &!'$ #  ¯ ’Æ !"

2R
BoxLayout
ƕ In a horizontal box, components are placed
horizontally, left to right.

ƕ In a vertical box, components are placed


vertically, top to bottom.

î  (
î  (

    

 ) 

)¯
2
BoxLayout (contƎd)
ƕ BoxLayout is the default type of layout for a Box
container.
ƕ The idiom for working with boxes is slightly
different:

$ $¯   $ !"


 ¯  ¯¯¯!"

**  
 '+,

 ¯ $¯      +,!"

$-$¯ .  $ !"


¯¯¯
2~
BoxLayout (contƎd)
      !"

 #  /#  !!"
$$¯ .  $ !"
¯  %$  &’  &!!"
¯  %$  &
 &!!"
¯  $¯ .     -,!!"
¯  %$  &  &!!"
¯  !"


Default Layouts
ƕ Each component has a default layout manager,
which remains in effect until the componentƎs
setLayout method is called.
ƕ Some of the defaults are:
Content pane BorderLayout
JPanel FlowLayout
Box BoxLayout


Review:
ƕ Can a container contain another container?
ƕ Is an action listener a class, an interface,
an object, or a method?
ƕ What type of objects can you add to a
JMenu?
ƕ How do FlowLayout and GridLayout deal
with the sizes of components?
ƕ What is the default layout manager for the
content pane?

2

Вам также может понравиться