Java 4th Unit Chat GPT
Java 4th Unit Chat GPT
Java 4th Unit Chat GPT
Components are individual elements of a graphical user interface (GUI) like buttons, text
fields, and labels. Containers are used to hold and organize components, such as
frames, panels, and applets.
Example:
java
Copy code
import java.awt.*;
import java.awt.event.*;
The Delegation Event Model involves event sources (like buttons) generating events,
which are handled by event listeners that implement specific event-handling methods.
Sketch:
lua
Copy code
+------------+ +-------------+ +--------------+
| Event | -----> | Event | -----> | Event |
| Source | | Listener | | Handler |
+------------+ +-------------+ +--------------+
Example Program:
java
Copy code
import java.awt.*;
import java.awt.event.*;
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Button clicked");
}
});
frame.add(button);
frame.setSize(300, 200);
frame.setLayout(new FlowLayout());
frame.setVisible(true);
}
}
Button:
java
Copy code
Button button = new Button("Click Me");
frame.add(button);
TextField:
java
Copy code
TextField textField = new TextField(20);
frame.add(textField);
4. Java Program Using Listeners for Handling Keyboard Events
java
Copy code
import java.awt.*;
import java.awt.event.*;
textField.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
System.out.println("Key Pressed: " + e.getKeyChar());
}
});
frame.add(textField);
frame.setSize(300, 200);
frame.setLayout(new FlowLayout());
frame.setVisible(true);
}
}
frame.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
System.out.println("Mouse Clicked at: " + e.getX() + ", " +
e.getY());
}
});
frame.addMouseMotionListener(new MouseMotionAdapter() {
public void mouseMoved(MouseEvent e) {
System.out.println("Mouse Moved to: " + e.getX() + ", " +
e.getY());
}
});
frame.setSize(300, 200);
frame.setLayout(new FlowLayout());
frame.setVisible(true);
}
}
java
Copy code
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Action performed");
}
});
java
Copy code
scrollbar.addAdjustmentListener(new AdjustmentListener() {
public void adjustmentValueChanged(AdjustmentEvent e) {
System.out.println("Adjustment made");
}
});
7. ActionEvent with Example
Example:
java
Copy code
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Button clicked");
}
});
Lists: Used to display a list of items. Panels: Containers for organizing components.
Example Program:
java
Copy code
import java.awt.*;
import java.awt.event.*;
java
Copy code
frame.setLayout(new GridLayout(2, 2));
java
Copy code
frame.setLayout(new FlowLayout());
java
Copy code
CardLayout cardLayout = new CardLayout();
frame.setLayout(cardLayout);
BorderLayout: Arranges components in five regions: north, south, east, west, and center.
java
Copy code
frame.setLayout(new BorderLayout());
frame.add(button, BorderLayout.NORTH);
java
Copy code
GridBagLayout layout = new GridBagLayout();
frame.setLayout(layout);
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
frame.add(button, gbc);