JAVA PROGRAMMING 1- What happens when you place a single button in the center of
ID: 3826056 • Letter: J
Question
JAVA PROGRAMMING
1- What happens when you place a single button in the center of a container that uses BorderLayout manager?
2- What happens when you place multiple buttons in the south of a container that uses BorderLayout manager?
3- What event method is used to register window event onto a JFrame object?
4- Name two methods found in the WindowAdapter class.
5- Given the following program segment.
public void paint(Graphics g)
{
Container c = f.getContentPane();
// A.
int x[] = {………}; // array of integers
int y[] = {……}; // array of integers
// B
}
Write appropriate code that:
(a) Set the background of the container blue
Typical response
(b) Creates a yellow polygon defined by the arrays
Typical response
g.fillPolygon(new Polygon(x,y, x.length));
6- Given that Flow represents a class that is a subclass of the class JFrame. Trace the code below and describe the output.
String arr[] = {“One”, “Two”, “Three”};
Flow f = new Flow(“Test”)
Container c = f.getContentPane();
c.setLayout(new FlowLayout(3));
for (int i = 0; I < arr.length; i++)
c.add(new JButton(arr[i]));
f.setSize(400, 300);
f.setVisible(true);
7- Given that the variable f is a reference to a JFrame object, write the requisite anonymous window closing event class that will cause the application to be terminate
(Typical response)
b.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
8- Study the following three classes as they pertain to the data structure called LinkedList, write the necessary Java code as described where indicated, by A, B, and C.
class Book class BookNode
{ {
String title; Book book;
BookNode next;
Book(String title)
{ BookNode(Book aBook)
this.title = title; {
} book = aBook;
next = null;
public String toString() }
{ }
return title;
}
}
class BookList
{
private BookNode list;
BookList() // Create an empty list
{
// A. Create an empty list
// Typical response
list = null;
}
void add(Book aBook) // Insert nodes at the end of the list
{
BookNode node = new BookNode(aBook);
// B. Append the node to the end of the list
// Typical response
BookNode current;
If(list == null)
list = node;
else
{
BookNode aux = list;
while (aux.next != null)
aux = aux.next;
aux.next = node;
}
}
public String toString()
{
String result = “”;
BookNode current = list;
// C. Write the code necessary to traverse the list, extract, and return the list of titles
// Typical response
while(currect != null)
{
result = result + current.book.getTitle();
current = current.next;
}
return result;
}
Explanation / Answer
1)Answer:
The button takes up the whole center part of the container.
2)Answer:
Only the last button that was added shows on the container.
3)Answer:
WindowListener, WindowFocusListener, and WindowStateListener. All three listeners handle WindowEvent objects
4)Answer:
a)void windowActivated(WindowEvent e)
Invoked when a window is activated.
b)void windowClosed(WindowEvent e)
Invoked when a window has been closed.
c)void windowClosing(WindowEvent e)
Invoked when a window is in the process of being closed,
d)void windowDeactivated(WindowEvent e)
Invoked when a window is de-activated.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.