0% found this document useful (0 votes)
17 views9 pages

Java Program

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views9 pages

Java Program

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Write java program display massage in various

fonts in a frame?
import java.awt.*;
import javax.swing.*;

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, SwingConstants.LEFT);
// Set the font: font name, style (BOLD), and size (24)
label.setFont(new Font(fontName, Font.BOLD, 24));
panel.add(label);
}

// Add a border around the panel (optional)


panel.setBorder(BorderFactory.createEmptyBorder(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 SwingUtilities.invokeLater to ensure thread safety
SwingUtilities.invokeLater(() -> new FontDisplayFrame());
}
}

2.Write java program draw various shapes like


rectangle ?
import javax.swing.*;
import java.awt.*;

public class ShapesDrawing extends JPanel {

@Override
public void paintComponent(Graphics g) {
super.paintComponent(g); // Clear previous drawings
Graphics2D g2d = (Graphics2D) g;

// Optional: Enable anti-aliasing for smoother shapes


g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);

// Draw a line from point (20, 20) to (150, 20)


g2d.setColor(Color.BLUE);
g2d.drawLine(20, 20, 150, 20);

// Draw a rectangle with top left corner at (20, 50) with width 100 and
height 60
g2d.setColor(Color.GREEN);
g2d.drawRect(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
g2d.setColor(Color.RED);
g2d.drawOval(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");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 200);
frame.setLocationRelativeTo(null); // Center the window on the screen

// Create an instance of our ShapesDrawing panel and add it to the frame


ShapesDrawing panel = new ShapesDrawing();
frame.add(panel);
frame.setVisible(true);
}
}

4. Write the Java Program Demonstrate


Window events ?
import javax.swing.*;
import java.awt.event.*;

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) {
System.out.println("Window opened.");
}

@Override
public void windowClosing(WindowEvent e) {
System.out.println("Window is closing.");
}

@Override
public void windowClosed(WindowEvent e) {
System.out.println("Window closed.");
}

@Override
public void windowIconified(WindowEvent e) {
System.out.println("Window minimized (iconified).");
}

@Override
public void windowDeiconified(WindowEvent e) {
System.out.println("Window restored (deiconified).");
}

@Override
public void windowActivated(WindowEvent e) {
System.out.println("Window activated.");
}

@Override
public void windowDeactivated(WindowEvent e) {
System.out.println("Window deactivated.");
}
});

// Add a simple label to the frame


JLabel label = new JLabel("Check the console for window event messages.",
SwingConstants.CENTER);
add(label);

// Make the frame visible


setVisible(true);
}

public static void main(String[] args) {


// Ensure GUI is created on the Event Dispatch Thread
SwingUtilities.invokeLater(() -> new WindowEventDemo());
}
}
5.Write the Java Program Demonstrate Mouse
Events ?
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

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",
SwingConstants.CENTER);
eventLabel.setFont(new Font("Arial", Font.PLAIN, 14));
add(eventLabel, BorderLayout.SOUTH);

// Panel to capture mouse events


JPanel panel = new JPanel();
panel.setBackground(Color.WHITE);
add(panel, BorderLayout.CENTER);

// Add a MouseListener using MouseAdapter to handle click, press,


release,
// mouse enter, and exit events.
panel.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
String text = "Mouse Clicked at (" + e.getX() + ", " + e.getY() +
")";
System.out.println(text);
eventLabel.setText(text);
}

@Override
public void mousePressed(MouseEvent e) {
String text = "Mouse Pressed at (" + e.getX() + ", " + e.getY() +
")";
System.out.println(text);
eventLabel.setText(text);
}

@Override
public void mouseReleased(MouseEvent e) {
String text = "Mouse Released at (" + e.getX() + ", " + e.getY()
+ ")";
System.out.println(text);
eventLabel.setText(text);
}

@Override
public void mouseEntered(MouseEvent e) {
String text = "Mouse Entered the panel";
System.out.println(text);
eventLabel.setText(text);
}

@Override
public void mouseExited(MouseEvent e) {
String text = "Mouse Exited the panel";
System.out.println(text);
eventLabel.setText(text);
}
});

// Add a MouseMotionListener using MouseMotionAdapter to handle movement


and dragging.
panel.addMouseMotionListener(new MouseMotionAdapter() {
@Override
public void mouseMoved(MouseEvent e) {
String text = "Mouse Moved at (" + e.getX() + ", " + e.getY() +
")";
System.out.println(text);
eventLabel.setText(text);
}

@Override
public void mouseDragged(MouseEvent e) {
String text = "Mouse Dragged at (" + e.getX() + ", " + e.getY() +
")";
System.out.println(text);
eventLabel.setText(text);
}
});
setVisible(true);
}

public static void main(String[] args) {


// Ensure the GUI is created on the Event Dispatch Thread for thread
safety.
SwingUtilities.invokeLater(() -> new MouseEventDemo());
}
}

6.Write the Java Program Demonstrate


Keyboard Events ?
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

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",
SwingConstants.CENTER);
messageLabel.setFont(new Font("Arial", Font.BOLD, 16));
add(messageLabel, BorderLayout.SOUTH);

// Create a panel and set it focusable to receive key events


JPanel panel = new JPanel();
panel.setBackground(Color.LIGHT_GRAY);
panel.setFocusable(true);
add(panel, BorderLayout.CENTER);

// Add KeyListener to the panel to capture key pressed and key released
events
panel.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
String msg = "Key Pressed: " +
KeyEvent.getKeyText(e.getKeyCode());
System.out.println(msg);
messageLabel.setText(msg);
}

@Override
public void keyReleased(KeyEvent e) {
String msg = "Key Released: " +
KeyEvent.getKeyText(e.getKeyCode());
System.out.println(msg);
messageLabel.setText(msg);
}
});

// Request focus on the panel so key events are triggered.


SwingUtilities.invokeLater(panel::requestFocusInWindow);

setVisible(true);
}

public static void main(String[] args) {


SwingUtilities.invokeLater(KeyboardEventDemo::new);
}
}

9.Write the Java Program demonstrate user


interface component label and button ?
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

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!");
label.setBounds(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");
button.setBounds(50, 60, 150, 25);
add(button);

// Add an ActionListener to the button to handle the click event


button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// When the button is clicked, update the label text
label.setText("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
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new SimpleUIExample();
}
});
}
}

You might also like