1. Explain the purpose of a layout manager in Java GUI programming.
Answer: A layout manager is an object that can be associated with a container, such as a JPanel. The layout manager is responsible for setting the sizes and positions of components that are added to the container. Different types of layout manager, such as BorderLayout and GridLayout, implement different policies for laying out the components.
2. JButton is one of the classes in Java that represent GUI components. List three additional classes that represent GUI components, and state briefly the purpose of each type of component.
Answer: For example:
(Other components you should know include JPanel, JTextArea, JSlider, and JRadioButton.)
3. The following class defines a panel that displays a rectangle. Add all the necessary code to this class so that when the user clicks the mouse on the panel, the upper-left corner of the rectangle is moved to the point where the user clicked.
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class DisplayRect extends JPanel implements MouseListener { private int x,y; // Upper-left corner of the rectangle. public DisplayRect() { } protected void paintComponent(Graphics g) { super.paintComponent(g); g.fillRect(x,y,100,50); } public void mousePressed(Event evt) { } public void mouseReleased(Event evt) { } public void mouseClicked(Event evt) { } public void mouseEntered(Event evt) { } public void mouseExited(Event evt) { } }
Answer: Four lines have to be added. In the constructor, the panel must be registered to listen for mouse events from itself. This can be done with addMouseListener(this) [or with this.addMouseListener(this)]. Then the response to the mouse must be programmed in the mousePressed() method. When the user clicks at a point (a,b), the rectangle must be moved so that its upper left corner is at (a,b). Inspecting the paintComponent method, we see that the instance variables x and y are used as the upper left corner of the rectangle in the fillRect command. So, we must set x and y to the point where the user clicked the mouse. The mouse coordinates are given in mousePressed as evt.getX() and evt.getY(), so we just have to set x = evt.getX() and y = evt.getY(). Finally, in order to make the change visible on the screen, we need to call repaint() to make sure that the panel is redrawn using the new position of the rectangle.
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class DisplayRect extends JPanel implements MouseListener { private int x,y; // Upper-left corner of the rectangle. public DisplayRect() { addMouseListener(this); } protected void paintComponent(Graphics g) { super.paintComponent(g); g.fillRect(x,y,100,50); } public void mousePressed(Event evt) { x = evt.getX(); y = evt.getY(); repaint(); } public void mouseReleased(Event evt) { } public void mouseClicked(Event evt) { } public void mouseEntered(Event evt) { } public void mouseExited(Event evt) { } }
4. Explain why the class in the preceding problem includes the empty methods mouseReleased, mouseClicked, mouseEntered, and mouseExited.
Answer: They are specified by the MouseListener interface, so they have to be there in order for the class to properly implement MouseListener, even if they don't do anything.