JAVA 1- What happens when you place multiple buttons in the SOUTH of a container
ID: 3826048 • Letter: J
Question
JAVA
1- What happens when you place multiple buttons in the SOUTH of a container that uses a BorderLayout manager?
2- What happens when you add a component directly to a JFrame and not to the content pane?
3- What is the name of the event method used to register a window event onto a JFrame?
4- In what ways do the GridBagLayout manager differ from the FlowLayout manager?
5 Given the following program, draw a diagram showing the output from it.
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.Container;
public class Flow extends JFrame
{
static String[] arr = {"First", "Second", "Third", “ Fourth”};
public static void main(String[] arg)
{
Flow flow = new Flow("Review");
Container c = flow.getContentPane();
c.setLayout(new FlowLayout(3));
for (int i = arr.length - 1; i >= 0; i--)
c.add(new JButton(arr[i]));
flow.setSize(500, 100);
flow.setVisible(true);
}
Flow(String str)
{
super(str);
}
}
6- Assume that the variable frame is a JFrame object, write appropriate java codes to register a window event object onto frame, such that when the window is about to close down, the message - Window is closing down – appears on the DOS screen.
7- Name the method found in the ActionListener interface.
8- Differentiate between a Container object and a Component object. Support your answer with one example of each.
9-
Using the program segment given below, write appropriate Java code that would:
(a) Draw a polygon defined by the given arrays, and color the polygon blue.
(b) Draw the string “Hello There!” beginning at location x = 250, y = 150, on the application. Use courier font for lettering style. Bold the letters and use pitch 36 for the size of each of the letters. Color the letters magenta.
public void paint(Graphics g)
{
int X[] = {10, 30, 40, 50, 70, 50, 40, 30};
int Y[] = {40, 30, 10, 30, 40, 50, 70, 50};
// Put part (a) here
// Put part (b) here
}
10- Create a class called myTextArea inherited from JFrame. Create a text area object whose size is 300 x 250 pixels. Place the text area in a scrollable pane and place this scrollable pane in the content pane of the frame. Also, initialize the text area with the string Review - Fall 2004.
11- Design a class called MyGui. Incorporate the following features in the class.
Part I
Create a frame of your own by deriving it from the class JFrame.
Add the title Computer Science
Part II
Add a menu bar containing the only menu choice, Student
Add a pull down menu item to contain menu items Name and Course. Treat menu item Course as a menu, and add to it two menu items – COP3337 and COP2210.
Add a button to the SOUTH of the screen and label it Exit
Part III
Add an anonymous mouse event listener class to the button such that when the button Exit is pressed the entire program (when built) closes down.
Explanation / Answer
[1]
IF you place multiple buttons in the SOUTH of a container Only the last button that was added shows on the container. Our Frame will have two panels say topPanel, bottomPanel. TopPanel will be on the NORTH and BottomPanel will be on SOUTH. On the main frame we use BorderLayout using which we will place the two panels one at NORTH and the other at SOUTH.
[2]
JFrames have a content pane, which holds the components. These components are sized and positioned by the layout manager. IF not to the content pane Problem is Unable to set border.
[3] addWindowListener(WindowListener lis) Method use.
[4] GridLayout is used to arrange the components in rectangular grid. One component is displayed in each rectangle.GridLayout has rules that positions the components in a grid where each cell is the same size and all are stretched equally.
FlowLayout is used to arrange the components in a line, one after another (in a flow). It is the default layout of applet or panel. FlowLayout has rules for positioning components one after another, horizontally (and maybe vertically, I don't remember offhand), and 'wrapping' them if the container is made too small to contain them.
[7]
void actionPerformed(ActionEvent e) : Invoked when an action occurs.
[8]
A container is a component that holds and manages other components. Containers display components using a layout manager.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.