Write java program display massage in various
fonts in a frame?
import [Link].*;
import [Link].*;
public class FontDisplayFrame extends JFrame {
public FontDisplayFrame() {
// Set up the frame
setTitle("Display Message in Various Fonts");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(600, 400);
setLocationRelativeTo(null); // center the frame on the screen
// Create a panel using GridLayout to display each label on a new row
JPanel panel = new JPanel(new GridLayout(0, 1, 10, 10));
// Define the message you want to display
String message = "Hello, World!";
// Define a selection of fonts to use
String[] fontNames = {"Serif", "SansSerif", "Monospaced", "Dialog",
"DialogInput"};
// Create and add a JLabel for each font
for (String fontName : fontNames) {
JLabel label = new JLabel(message, [Link]);
// Set the font: font name, style (BOLD), and size (24)
[Link](new Font(fontName, [Link], 24));
[Link](label);
}
// Add a border around the panel (optional)
[Link]([Link](20, 20, 20, 20));
// Add the panel to the frame
add(panel);
// Make the frame visible
setVisible(true);
}
public static void main(String[] args) {
// Use the [Link] to ensure thread safety
[Link](() -> new FontDisplayFrame());
}
}
[Link] java program draw various shapes like
rectangle ?
import [Link].*;
import [Link].*;
public class ShapesDrawing extends JPanel {
@Override
public void paintComponent(Graphics g) {
[Link](g); // Clear previous drawings
Graphics2D g2d = (Graphics2D) g;
// Optional: Enable anti-aliasing for smoother shapes
[Link](RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
// Draw a line from point (20, 20) to (150, 20)
[Link]([Link]);
[Link](20, 20, 150, 20);
// Draw a rectangle with top left corner at (20, 50) with width 100 and
height 60
[Link]([Link]);
[Link](20, 50, 100, 60);
// Draw a circle by drawing an oval with equal width and height.
// Here, the circle's bounding box has top left corner at (150, 50) with
a diameter of 60
[Link]([Link]);
[Link](150, 50, 60, 60);
}
public static void main(String[] args) {
// Create a new JFrame to hold the drawing panel
JFrame frame = new JFrame("Draw Geometric Shapes");
[Link](JFrame.EXIT_ON_CLOSE);
[Link](300, 200);
[Link](null); // Center the window on the screen
// Create an instance of our ShapesDrawing panel and add it to the frame
ShapesDrawing panel = new ShapesDrawing();
[Link](panel);
[Link](true);
}
}
4. Write the Java Program Demonstrate
Window events ?
import [Link].*;
import [Link].*;
public class WindowEventDemo extends JFrame {
public WindowEventDemo() {
// Set up the frame
setTitle("Window Event Demo");
setSize(400, 300);
setLocationRelativeTo(null); // Center on screen
// Default close operation is set to exit on close
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Add a WindowListener using WindowAdapter to handle specific events
addWindowListener(new WindowAdapter() {
@Override
public void windowOpened(WindowEvent e) {
[Link]("Window opened.");
}
@Override
public void windowClosing(WindowEvent e) {
[Link]("Window is closing.");
}
@Override
public void windowClosed(WindowEvent e) {
[Link]("Window closed.");
}
@Override
public void windowIconified(WindowEvent e) {
[Link]("Window minimized (iconified).");
}
@Override
public void windowDeiconified(WindowEvent e) {
[Link]("Window restored (deiconified).");
}
@Override
public void windowActivated(WindowEvent e) {
[Link]("Window activated.");
}
@Override
public void windowDeactivated(WindowEvent e) {
[Link]("Window deactivated.");
}
});
// Add a simple label to the frame
JLabel label = new JLabel("Check the console for window event messages.",
[Link]);
add(label);
// Make the frame visible
setVisible(true);
}
public static void main(String[] args) {
// Ensure GUI is created on the Event Dispatch Thread
[Link](() -> new WindowEventDemo());
}
}
[Link] the Java Program Demonstrate Mouse
Events ?
import [Link].*;
import [Link].*;
import [Link].*;
public class MouseEventDemo extends JFrame {
public MouseEventDemo() {
super("Mouse Event Demo");
setSize(600, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null); // Center the window
// Label to display mouse event information
JLabel eventLabel = new JLabel("Interact with the panel",
[Link]);
[Link](new Font("Arial", [Link], 14));
add(eventLabel, [Link]);
// Panel to capture mouse events
JPanel panel = new JPanel();
[Link]([Link]);
add(panel, [Link]);
// Add a MouseListener using MouseAdapter to handle click, press,
release,
// mouse enter, and exit events.
[Link](new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
String text = "Mouse Clicked at (" + [Link]() + ", " + [Link]() +
")";
[Link](text);
[Link](text);
}
@Override
public void mousePressed(MouseEvent e) {
String text = "Mouse Pressed at (" + [Link]() + ", " + [Link]() +
")";
[Link](text);
[Link](text);
}
@Override
public void mouseReleased(MouseEvent e) {
String text = "Mouse Released at (" + [Link]() + ", " + [Link]()
+ ")";
[Link](text);
[Link](text);
}
@Override
public void mouseEntered(MouseEvent e) {
String text = "Mouse Entered the panel";
[Link](text);
[Link](text);
}
@Override
public void mouseExited(MouseEvent e) {
String text = "Mouse Exited the panel";
[Link](text);
[Link](text);
}
});
// Add a MouseMotionListener using MouseMotionAdapter to handle movement
and dragging.
[Link](new MouseMotionAdapter() {
@Override
public void mouseMoved(MouseEvent e) {
String text = "Mouse Moved at (" + [Link]() + ", " + [Link]() +
")";
[Link](text);
[Link](text);
}
@Override
public void mouseDragged(MouseEvent e) {
String text = "Mouse Dragged at (" + [Link]() + ", " + [Link]() +
")";
[Link](text);
[Link](text);
}
});
setVisible(true);
}
public static void main(String[] args) {
// Ensure the GUI is created on the Event Dispatch Thread for thread
safety.
[Link](() -> new MouseEventDemo());
}
}
[Link] the Java Program Demonstrate
Keyboard Events ?
import [Link].*;
import [Link].*;
import [Link].*;
public class KeyboardEventDemo extends JFrame {
private JLabel messageLabel;
public KeyboardEventDemo() {
super("Keyboard Event Demo");
setSize(500, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
// Create a label to display key event messages
messageLabel = new JLabel("Press or release a key",
[Link]);
[Link](new Font("Arial", [Link], 16));
add(messageLabel, [Link]);
// Create a panel and set it focusable to receive key events
JPanel panel = new JPanel();
[Link](Color.LIGHT_GRAY);
[Link](true);
add(panel, [Link]);
// Add KeyListener to the panel to capture key pressed and key released
events
[Link](new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
String msg = "Key Pressed: " +
[Link]([Link]());
[Link](msg);
[Link](msg);
}
@Override
public void keyReleased(KeyEvent e) {
String msg = "Key Released: " +
[Link]([Link]());
[Link](msg);
[Link](msg);
}
});
// Request focus on the panel so key events are triggered.
[Link](panel::requestFocusInWindow);
setVisible(true);
}
public static void main(String[] args) {
[Link](KeyboardEventDemo::new);
}
}
[Link] the Java Program demonstrate user
interface component label and button ?
import [Link].*;
import [Link];
import [Link];
public class SimpleUIExample extends JFrame {
public SimpleUIExample() {
// Set the title of the JFrame
super("Simple UI Example");
// Set the default close operation so the application exits when the
window is closed
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Use no layout manager (absolute positioning) for this example
setLayout(null);
// Create a label and set its initial text and position
JLabel label = new JLabel("Hello, World!");
[Link](50, 20, 150, 25); // x, y, width, height
add(label);
// Create a button and set its text and position
JButton button = new JButton("Click Me");
[Link](50, 60, 150, 25);
add(button);
// Add an ActionListener to the button to handle the click event
[Link](new ActionListener() {
public void actionPerformed(ActionEvent e) {
// When the button is clicked, update the label text
[Link]("Button Clicked!");
}
});
// Set the size of the window and center it on the screen
setSize(250, 150);
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String[] args) {
// Ensure the GUI is created on the Event Dispatch Thread
[Link](new Runnable() {
public void run() {
new SimpleUIExample();
}
});
}
}