(15 points) Choose the best answer from the following list of layout
managers:
Box Flow Grid Group GridBag Border Spring None
- ____________ I want to equally space a group of objects horizontally
across the window. If the window is re-sized, I want the objects
to continual to be equally spaced across the window.
- ____________ I want to design a new type of browser window that
has toolbars on both the left and right sides of the window and
I want the content displayed in the center of the window.
- ____________ I want to display a set of equal-sized color chips in a color
pallette using a tabular format.
- ____________ I want to display a set of command buttons vertically
at their natural size.
- ____________ I want to design a dialog box with a number of different
groups of widgets. What should I use as the top-level
layout manager if I am designing the dialog box
programmatically (i.e., I am not using an IDE).
(30 points) Finish the following class definition so that it implements the following graphical interface:
- When the user presses the mouse button, draw a blue rectangle
that completely
fills the JPanel and that has a 10 pixel black border (i.e., the
rectangle has the black border).
- When the user releases the mouse button, draw a green rectangle
that completely fills the JPanel and that has a 10 pixel black
border (i.e., the
rectangle has the black border).
- The JPanel should initially appear as a 100x100 pixel object but
it can be resized.
- The JPanel should be added to the center of its JFrame's content
pane.
Here is a sample gui for the interface:
| |
When the mouse is pressed, a blue rectangle is drawn that
completely fills the JPanel |
When the mouse is released, a green rectangle is drawn that
completely fills the JPanel |
Here are some helpful hints:
- You can use the command "new BasicStroke(10)" to get a stroke object
that is 10 pixels wide.
- Color.black, Color.blue, and Color.green are constants for the
colors you need.
- You will need a constructor method, a paintComponent method,
and a getPreferredSize method
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class code extends JPanel {
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new code();
}
});
}
JFrame window = new JFrame();
// Your code goes here--You may need to declare additional instance
// variables