Java Code: Find the Error - Swing Components 1. The following statement is in a
ID: 3559403 • Letter: J
Question
Java Code: Find the Error - Swing Components
1. The following statement is in a class that uses Swing components:
import java.swing.*;
2. The following is an inner class that will be registered as an action listener for a JButton component:
private class ButtonListener implements ActionListener
{
public void actionPerformed( )
{
// Code appears here
}
}
3. The intention of the following statement is to give the panel object a GridLayout manager with 10 columns and 5 rows:
panel.setLayout(new GridLayout(10, 5) );
4. The panel variable references a JPanel governed by a BorderLayout manager. The following statement attempts to add the button component to the north region of panel:
panel.add(button, NORTH);
5. The panel variable references a JPanel object. The intention of the following statement is to create a titled border around panel:
panel.setBorder(new BorderFactory(*Choices*) );
Explanation / Answer
1) There is NO java.swing package in Java
But there is a package which is used for Swing components is
javax.swing.*;
2) There is No Error in this code, creating the Listener class when ever button's are clicked (We have private class in inner class)
private class ButtonListener implements ActionListener
{
public void actionPerformed( )
{
// Code appears here
}
}
for the first time i didn't recognize the paramater for actionPerformed() so we have ActionEvent as a parameter for actionPerformed(),
so finally code look's like
private class ButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent ae)
{
// Code appears here
}
}
3) GridLayout(int rows ,int columns);
so error is 10 columns and 5 rows.
correct ans is 10 rows and 5 columns.
if you want 10 columns and 5 rows use following
panel.setLayout(new GridLayout(5,10));
4) if panel is governed by BorderLayout but still you need to mention the BorderLayout.NORTH to place the button in North region of Layout.
panel.add(button,BorderLayout.NORTH);
Sysntax : panel.add(BUTTON, Region);
5) BorderFactory class is used to sets the Border(Bounds) of layout.
there is no String argument for BorderFactory class
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.