Programming and Problem Solving with Java

(やまだぃちぅ) #1

724


14.Declare a variable of the JButtonclass, instantiate a JButtonobject and assign its
address to the variable, and add the object to the frame’s content pane using the
addmethod.
17.Classes that implement the ActionListenerinterface must have an actionPerformed
method with an ActionEventparameter.
20.There is a default event handler provided for the window closing.

Chapter 8 Programming Warm-Up Exercises


  1. a.ButtonHandler action;
    JButton enter;
    JButton quit;
    b.enter = newJButton("Enter");
    quit = newJButton("Quit");
    action = newButtonHandler();
    c. outPane.add(enter);
    outPane.add(quit);
    d.enter.addActionListener(action);
    quit.addActionListener(action);
    e.String name;
    name = event.getActionCommand();
    f. private classButtonHandler implementsActionListener
    {
    public voidactionPerformed(ActionEvent event)
    {
    String name;
    name = event.getActionCommand();
    if(name.equals("Enter"))
    out.add(newJLabel("Enter was pressed."));
    else if(name.equals("Quit"))
    out.add(newJLabel("Quit was pressed."));
    else
    out.add(newJLabel("An unexpected event occurred.")):
    }
    }

  2. a.JFrame exampleFrame;
    JLabel exampleLabel;
    JTextField exampleField;
    staticJButton exampleButton;
    container outPane;

Free download pdf