Question 1

Consider a frame with a labeled button as shown below.

We want the button to behave as follows: if it is clicked, its label changes to Goodbye World. If it is clicked again, its label changes back to Hello World and the process repeats. The code on the right shows the creation of the frame.

Write the code that creates the button and gives it an appropriate action listener that results in the correct behavior.

   JFrame frame = new JFrame("Greeting");
   ... // create a button and give it an action listener ...
   frame.add(button);
Consider a frame with a labeled button as shown below.

We want the button to behave as follows: if it is clicked, its label changes to Goodbye World. If it is clicked again, its label changes back to Hello World and the process repeats. The code on the right shows the creation of the frame.

Write the code that creates the button and gives it an appropriate action listener that results in the correct behavior.

   JFrame frame = new JFrame("Greeting");
   final JButton button = 
      new JButton(``Hello World'');  // note "final" declaration
   button.addActionListener(new ActionListener() {
       public void actionPerformed(ActionEvent e) {
           if ( button.getText().equals(``Hello World'')) {
               button.setText(``Goodbye World'');
           }
           else {
               button.setText(``Hello World'');
           }
       }
    });
   frame.add(button);
The program on the right creates the frame below.

As the mouse is moved within the frame, the x and y coordinates of the mouse point are displayed.

Write the code that adds the mouse motion listener.

class MouseComponent extends JPanel
{
    private JLabel xLabel;
    private JLabel yLabel;
    private Point mousePoint;

    public MouseComponent()
    {
        xLabel = new JLabel();
        yLabel = new JLabel();
        xLabel.setBorder(BorderFactory.createTitledBorder("x"));
        yLabel.setBorder(BorderFactory.createTitledBorder("y"));
        JPanel xyPanel = new JPanel();
        xyPanel.add(xLabel);
        xyPanel.add(yLabel);

        add(xyPanel);

        // Add a mouse motion listener here
    }

    public static void main(String[] args)
    {
        JFrame frame = new JFrame();
        frame.add(new MouseComponent());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400, 200);
        frame.setVisible(true);
    }
}
	  
The program on the right creates the frame below.

As the mouse is moved within the frame, the x and y coordinates of the mouse point are displayed.

Write the code that adds the mouse motion listener.

class MouseComponent extends JPanel
{
    private JLabel xLabel;
    private JLabel yLabel;
    private Point mousePoint;

    public MouseComponent()
    {
        xLabel = new JLabel();
        yLabel = new JLabel();
        xLabel.setBorder(BorderFactory.createTitledBorder("x"));
        yLabel.setBorder(BorderFactory.createTitledBorder("y"));
        JPanel xyPanel = new JPanel();
        xyPanel.add(xLabel);
        xyPanel.add(yLabel);

        add(xyPanel);

        addMouseMotionListener(new MouseMotionListener() {

            public void mouseMoved(MouseEvent event) {
                mousePoint = event.getPoint();
                xLabel.setText(Integer.toString((int) mousePoint.getX()));
                yLabel.setText(Integer.toString((int) mousePoint.getY()));
            }

            public void mouseDragged(MouseEvent event) {
            }
        });
    }

    public static void main(String[] args)
    {
        JFrame frame = new JFrame();
        frame.add(new MouseComponent());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400, 200);
        frame.setVisible(true);
    }
}