Java applets are small, self-contained Java programs that run within a web browser. They are designed to be executed within a web page, dynamically loaded and embedded within an HTML document.
Here’s an example of a simple Java applet that displays a message:
import java.awt.; import java.applet.;
public class HelloApplet extends Applet {
public void paint(Graphics g) {
g.drawString(“Hello, World!”, 50, 25);
}
}
To run the applet, you would need to include the following HTML code in a web page:
<applet code=”HelloApplet.class” width=”300″ height=”300″> </applet>
When the web page loads, the Java applet is executed within the browser window, and the message “Hello, World!” is displayed.
AWT controls
Abstract Window Toolkit (AWT) is a set of Java classes that provides a platform-independent way of creating graphical user interfaces (GUIs) for Java applications. It was the first GUI toolkit available for the Java platform and is part of the Java Standard Edition (SE).
Here are some of the commonly used AWT controls and their functions:
- Button: A button is a control that triggers an action when clicked. It is represented by the
Button
class. - Label: A label is used to display text or an image. It is represented by the
Label
class. - TextField: A text field is a control used to input a single line of text. It is represented by the
TextField
class. - TextArea: A text area is a control used to input or display multiple lines of text. It is represented by the
TextArea
class. - Checkbox: A checkbox is a control that allows the user to make a binary choice. It is represented by the
Checkbox
class. - RadioButton: A radio button is used to present a mutually exclusive set of options to the user. It is represented by the
RadioButton
class. - Choice: A choice is a control used to present a drop-down list of options to the user. It is represented by the
Choice
class. - List: A list is a control that displays a scrollable list of items. It is represented by the
List
class. - Scrollbar: A scrollbar is a control used to allow the user to scroll through a range of values. It is represented by the
Scrollbar
class. - Menu: A menu is a control used to present a list of options to the user. It is represented by the
Menu
andMenuItem
classes.
These are some of the basic controls provided by AWT. There are also more advanced controls, such as panels, dialogs, and frames, that can be used to create more complex GUIs
button
A button in Java is a control component that triggers an action when clicked. It is represented by the java.awt.Button
class in the Abstract Window Toolkit (AWT).
Here’s an example of how you could create a button in Java using the AWT:
import java.awt.; import java.awt.event.;
public class ButtonExample extends Frame implements ActionListener {
private Button btn;
private Label lbl;
public ButtonExample() {
setLayout(new FlowLayout());
btn = new Button("Click me");
add(btn);
btn.addActionListener(this);
lbl = new Label("Welcome to Java");
add(lbl);
setSize(300, 300);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
lbl.setText(“Button clicked!”);
}
public static void main(String[] args) {
new ButtonExample();
}
}
In the example, we first create a Button
object and add it to the Frame
. Then, we implement the ActionListener
interface and override the actionPerformed
method to handle the button click event. In the actionPerformed
method, we update the text of a Label
component to indicate that the button has been clicked.
When the program is run, a window containing the button and label will be displayed. When the button is clicked, the text of the label will change to “Button clicked!
Labels
Labels in java with example
A label in Java is a control component that displays text or an image. It is represented by the java.awt.Label
class in the Abstract Window Toolkit (AWT).
Here’s an example of how you could create a label in Java using the AWT:
import java.awt.*;
public class LabelExample extends Frame {
public LabelExample() {
setLayout(new FlowLayout());
Label label1 = new Label("This is label 1");
add(label1);
Label label2 = new Label("This is label 2");
label2.setAlignment(Label.RIGHT);
add(label2);
setSize(300, 300);
setVisible(true);
}
public static void main(String[] args) {
new LabelExample();
}
}
In the example, we create two Label
objects and add them to the Frame
. The first label displays the text “This is label 1”. The second label displays the text “This is label 2” and its alignment is set to right.
When the program is run, a window containing the two labels will be displayed, with the first label aligned to the left and the second label aligned to the right.
combo box
A combo box in Java is a control component that allows the user to select an item from a drop-down list of options. It is represented by the java.awt.Choice
class in the Abstract Window Toolkit (AWT).
Here’s an example of how you could create a combo box in Java using the AWT:
import java.awt.; import java.awt.event.;
public class ComboBoxExample extends Frame implements ItemListener {
private Choice choice;
private Label lbl;
public ComboBoxExample() {
setLayout(new FlowLayout());
choice = new Choice();
choice.add("Option 1");
choice.add("Option 2");
choice.add("Option 3");
add(choice);
choice.addItemListener(this);
lbl = new Label("Welcome to Java");
add(lbl);
setSize(300, 300);
setVisible(true);
}
public void itemStateChanged(ItemEvent e) {
lbl.setText(“You selected ” + choice.getSelectedItem());
}
public static void main(String[] args) {
new ComboBoxExample();
}
}
In the example, we create a Choice
object and add several options to it. Then, we implement the ItemListener
interface and override the itemStateChanged
method to handle the selection event. In the itemStateChanged
method, we update the text of a Label
component to indicate the selected option.
When the program is run, a window containing the combo box and label will be displayed. When an option is selected from the combo box, the text of the label will change to display the selected option.
list and other Listeners
Listeners in Java are components that respond to specific events, such as a user clicking a button or selecting an item from a list. The Java Abstract Window Toolkit (AWT) provides several types of listeners, including:
ActionListener
: triggers an action when a button or menu item is clicked.ItemListener
: triggers an action when an item is selected from a list or combo box.WindowListener
: triggers an action when a window is opened, closed, activated, or deactivated.KeyListener
: triggers an action when a key is pressed or released.MouseListener
: triggers an action when a mouse button is clicked or the mouse pointer is moved.
Here’s an example of how you could use the ActionListener
interface:
import java.awt.; import java.awt.event.;
public class ActionListenerExample extends Frame implements ActionListener {
private Button btn;
private Label lbl;
public ActionListenerExample() {
setLayout(new FlowLayout());
btn = new Button("Click me");
add(btn);
btn.addActionListener(this);
lbl = new Label("Welcome to Java");
add(lbl);
setSize(300, 300);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
lbl.setText(“Button clicked!”);
}
public static void main(String[] args) {
new ActionListenerExample();
}
}
In the example, we create a Button
object and add it to the Frame
. Then, we implement the ActionListener
interface and override the actionPerformed
method to handle the button click event. In the actionPerformed
method, we update the text of a Label
component to indicate that the button has been clicked.
Similarly, you could use the other listener interfaces in Java to handle different types of events. For example, you could use the ItemListener
interface to handle the selection event of a list or combo box, the WindowListener
interface to handle the window events, the KeyListener
interface to handle the key events, and the MouseListener
interface to handle the mouse events.
menu bar
A menu bar in Java is a component that provides a container for multiple menus. It is represented by the java.awt.MenuBar
class in the Abstract Window Toolkit (AWT).
Here’s an example of how you could create a menu bar in Java using the AWT:
import java.awt.; import java.awt.event.;
public class MenuBarExample extends Frame {
private MenuBar menuBar;
private Menu fileMenu;
private MenuItem exitItem;
public MenuBarExample() {
setLayout(new FlowLayout());
menuBar = new MenuBar();
setMenuBar(menuBar);
fileMenu = new Menu("File");
menuBar.add(fileMenu);
exitItem = new MenuItem("Exit");
fileMenu.add(exitItem);
exitItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
setSize(300, 300);
setVisible(true);
}
public static void main(String[] args) {
new MenuBarExample();
}
}
In the example, we create a MenuBar
object and set it as the menu bar for the Frame
. Then, we create a Menu
object and add it to the menu bar. Finally, we create a MenuItem
object, add it to the menu, and set an ActionListener
to handle the exit event. When the program is run, a window containing the menu bar will be displayed. When the “Exit” option is selected from the “File” menu, the program will exit.
Note that in the example, the ActionListener
is implemented using an anonymous inner class. Alternatively, you could implement the ActionListener
interface in a separate class and create an instance of that class to handle the event.
layout manager
A layout manager in Java is a component that controls the placement and size of components within a container. Java provides several layout managers, including FlowLayout
, BorderLayout
, GridLayout
, BoxLayout
, CardLayout
, and GridBagLayout
, among others.
Here’s an example of how you could use the FlowLayout
layout manager:
import java.awt.*;
public class FlowLayoutExample extends Frame {
private Button btn1, btn2, btn3;
public FlowLayoutExample() {
setLayout(new FlowLayout());
btn1 = new Button("Button 1");
add(btn1);
btn2 = new Button("Button 2");
add(btn2);
btn3 = new Button("Button 3");
add(btn3);
setSize(300, 300);
setVisible(true);
}
public static void main(String[] args) {
new FlowLayoutExample();
}
}
A layout manager in Java is a component that controls the placement and size of components within a container. Java provides several layout managers, including FlowLayout
, BorderLayout
, GridLayout
, BoxLayout
, CardLayout
, and GridBagLayout
, among others.
Here’s an example of how you could use the FlowLayout
layout manager:
javaCopy codeimport java.awt.*;
public class FlowLayoutExample extends Frame {
private Button btn1, btn2, btn3;
public FlowLayoutExample() {
setLayout(new FlowLayout());
btn1 = new Button("Button 1");
add(btn1);
btn2 = new Button("Button 2");
add(btn2);
btn3 = new Button("Button 3");
add(btn3);
setSize(300, 300);
setVisible(true);
}
public static void main(String[] args) {
new FlowLayoutExample();
}
}
In the example, we create a Frame
object and set its layout manager to a FlowLayout
object. Then, we create three Button
objects and add them to the frame. When the program is run, the buttons will be displayed in a flow layout, which means they will be placed one after the other in a row, left to right. If there isn’t enough space in the row for all the buttons, a new row will be started.
Similarly, you could use other layout managers in Java to arrange the components in different ways. For example, you could use the BorderLayout
layout manager to arrange components in the north, south, east, west, and center regions of a container, or you could use the GridLayout
layout manager to arrange components in a grid with a specified number of rows and columns.
string handling
String handling in Java involves performing operations on strings, such as concatenation, comparison, searching, replacing, and others. Here are some of the main string handling functions in Java along with examples:
- Concatenation:
String str1 = “Hello”;
String str2 = “World”;
String str3 = str1 + ” ” + str2;
System.out.println(str3); // Output: Hello World
- Comparison:
String str1 = “Hello”;
String str2 = “Hello”;
if (str1.equals(str2)) {
System.out.println(“The strings are equal.”);
}
else {
System.out.println(“The strings are not equal.”);
}
- Searching:
String str = “Hello World”;
int index = str.indexOf(“World”);
System.out.println(“Index of World: ” + index); // Output: 6
- Replacing:
String str = “Hello World”;
str = str.replace(“World”, “Java”);
System.out.println(str); // Output: Hello Java
String handling in Java involves performing operations on strings, such as concatenation, comparison, searching, replacing, and others. Here are some of the main string handling functions in Java along with examples:
- Concatenation:
javascriptCopy codeString str1 = "Hello";
String str2 = "World";
String str3 = str1 + " " + str2;
System.out.println(str3); // Output: Hello World
- Comparison:
csharpCopy codeString str1 = "Hello";
String str2 = "Hello";
if (str1.equals(str2)) {
System.out.println("The strings are equal.");
}
else {
System.out.println("The strings are not equal.");
}
- Searching:
perlCopy codeString str = "Hello World";
int index = str.indexOf("World");
System.out.println("Index of World: " + index); // Output: 6
- Replacing:
rustCopy codeString str = "Hello World";
str = str.replace("World", "Java");
System.out.println(str); // Output: Hello Java
- Substring:
String str = “Hello World”;
String sub = str.substring(6);
System.out.println(sub); // Output: World
- Trimming:
String str = ” Hello World “;
str = str.trim();
System.out.println(str); // Output: Hello World
7.length
String str = “Hello World”;
int len = str.length();
System.out.println(“Length of the string: ” + len); // Output: 11
- Conversion:
int i = 42;
String str = Integer.toString(i);
System.out.println(str); // Output: 42
These are some of the main string handling functions in Java. You can use these functions to manipulate strings in your Java programs as needed.