Related Topics
JAVA Programing
- Question 37
How do you display a button in an applet?
- Answer
To display a button in an applet, you can use the java.awt.Button
class. Here are the steps to do so:
Create a
Button
component.
Button button = new Button("Click me");
Add the
Button
to the applet.
add(button);
This will display the button with the label “Click me” in the applet. You can add an action listener to the button to handle user clicks using the addActionListener()
method. When the button is clicked, the action performed event is triggered and the actionPerformed()
method of the action listener is called.
- Question 38
How do you display a checkbox in an applet?
- Answer
To display a checkbox in an applet, you can use the java.awt.Checkbox
class. Here are the steps to do so:
Create a
Checkbox
component.
Checkbox checkbox = new Checkbox("Check me");
Add the
Checkbox
to the applet.
add(checkbox);
This will display the checkbox with the label “Check me” in the applet. You can add an item listener to the checkbox to handle user clicks using the addItemListener()
method. When the checkbox is checked or unchecked, the item state changed event is triggered and the itemStateChanged()
method of the item listener is called. You can get the state of the checkbox (checked or unchecked) using the getState()
method.
- Question 39
How do you display a list box in an applet?
- Answer
To display a list box in an applet, you can use the List
class in Java. Here’s an example:
import java.applet.Applet;
import java.awt.List;
public class MyListApplet extends Applet {
List myList;
public void init() {
myList = new List(3, false);
myList.add("Item 1");
myList.add("Item 2");
myList.add("Item 3");
add(myList);
}
}
In this example, we create a new List
object in the init
method and add three items to it. The List
constructor takes two arguments: the first argument is the number of visible rows in the list box, and the second argument specifies whether the list box should allow multiple selections. We then add the List
object to the applet using the add
method. When the applet is displayed, the list box will appear with the three items we added.
- Question 40
How do you display a choice box in an applet?
- Answer
To display a choice box (also known as a drop-down list) in an applet, you can use the Choice
class from the AWT (Abstract Window Toolkit) package in Java. Here is an example code snippet to create and add a choice box to an applet:
import java.applet.Applet;
import java.awt.Choice;
import java.awt.FlowLayout;
public class MyChoiceApplet extends Applet {
public void init() {
// Set the applet layout to flow layout
setLayout(new FlowLayout());
// Create a new Choice object
Choice myChoice = new Choice();
// Add items to the choice box
myChoice.add("Option 1");
myChoice.add("Option 2");
myChoice.add("Option 3");
// Add the choice box to the applet
add(myChoice);
}
}
In this example, the init()
method is overridden to create and add the choice box to the applet. The Choice
object is created using the new
keyword, and items are added to it using the add()
method. Finally, the choice box is added to the applet using the add()
method as well.
When the applet is run, the choice box will be displayed on the applet window, and the user can select an option from the list by clicking on the drop-down arrow and selecting an item.
- Question 41
How do you display a menu in an applet?
- Answer
To display a menu in an applet, you can use the Menu
and MenuBar
classes in Java. Here is an example code snippet that demonstrates how to create and display a simple menu in an applet:
import java.applet.*;
import java.awt.*;
public class MyMenuApplet extends Applet {
private MenuBar menuBar;
private Menu fileMenu, editMenu;
private MenuItem newItem, openItem, saveItem, exitItem;
private CheckboxMenuItem checkItem;
public void init() {
menuBar = new MenuBar();
fileMenu = new Menu("File");
editMenu = new Menu("Edit");
newItem = new MenuItem("New");
openItem = new MenuItem("Open");
saveItem = new MenuItem("Save");
exitItem = new MenuItem("Exit");
checkItem = new CheckboxMenuItem("Check");
fileMenu.add(newItem);
fileMenu.add(openItem);
fileMenu.add(saveItem);
fileMenu.addSeparator();
fileMenu.add(exitItem);
editMenu.add(checkItem);
menuBar.add(fileMenu);
menuBar.add(editMenu);
setMenuBar(menuBar);
}
}
In this example, the MenuBar
is created in the init()
method and added to the applet using the setMenuBar()
method. Two menus (fileMenu
and editMenu
) are created using the Menu
class, and several menu items are added using the MenuItem
and CheckboxMenuItem
classes. The add()
method is used to add the menu items to the appropriate menus, and the addSeparator()
method is used to add a separator between the “Save” and “Exit” menu items in the “File” menu. Finally, the menus are added to the MenuBar
using the add()
method.
- Question 42
How do you display a dialog box in an applet?
- Answer
To display a dialog box in an applet, you can use the JOptionPane
class provided by Swing. Here’s an example code snippet that shows how to display a dialog box with a message and an OK button:
import javax.swing.JOptionPane;
public class MyDialogApplet extends java.applet.Applet {
public void init() {
String message = "Hello, this is a dialog box!";
String title = "Dialog Box Example";
JOptionPane.showMessageDialog(null, message, title, JOptionPane.INFORMATION_MESSAGE);
}
}
In the example code above, the JOptionPane.showMessageDialog()
method is used to display a dialog box with the message “Hello, this is a dialog box!” and the title “Dialog Box Example”. The null
parameter indicates that the dialog box should be centered on the screen. The JOptionPane.INFORMATION_MESSAGE
constant specifies the type of message to display in the dialog box. There are other types of messages that can be displayed, such as JOptionPane.ERROR_MESSAGE
and JOptionPane.WARNING_MESSAGE
, among others.
- Question 43
How do you display a pop-up menu in an applet?
- Answer
To display a pop-up menu in an applet, you can use the PopupMenu
class in Java. Here are the basic steps:
Create a
PopupMenu
object.Create a listener object to handle the mouse events that trigger the pop-up menu.
Add the listener object to the component that will trigger the pop-up menu.
Override the
mousePressed
method in the listener object to display the pop-up menu at the location of the mouse click.
Here is some sample code that demonstrates how to display a pop-up menu in an applet:
import java.awt.*;
import java.awt.event.*;
public class MyApplet extends Applet {
PopupMenu popup;
public void init() {
// Create the pop-up menu
popup = new PopupMenu("My Pop-Up Menu");
MenuItem item1 = new MenuItem("Item 1");
MenuItem item2 = new MenuItem("Item 2");
popup.add(item1);
popup.add(item2);
// Create a listener to handle mouse events
MouseListener popupListener = new PopupListener();
// Add the listener to the applet
addMouseListener(popupListener);
}
// Override the paint method to draw a message
public void paint(Graphics g) {
g.drawString("Right-click to display the pop-up menu", 50, 50);
}
// Inner class to handle mouse events
class PopupListener extends MouseAdapter {
public void mousePressed(MouseEvent e) {
// Check for a right-click
if (e.isPopupTrigger()) {
// Display the pop-up menu
popup.show(e.getComponent(), e.getX(), e.getY());
}
}
}
}
In this example, the pop-up menu contains two items (“Item 1” and “Item 2”). The listener object is an inner class called PopupListener
, which extends MouseAdapter
and overrides the mousePressed
method. The addMouseListener
method is called to add the listener object to the applet. When the user right-clicks the applet, the mousePressed
method checks for a popup trigger and displays the pop-up menu at the location of the mouse click.