Join Regular Classroom : Visit ClassroomTech

JAVA – codewindow.in

Related Topics

JAVA Programing

import java.awt.Button;
import java.awt.Frame;
import java.awt.Label;
import java.awt.Panel;

public class MyPanel extends Panel {
    
    public MyPanel() {
        Button myButton = new Button("Click Me");
        Label myLabel = new Label("Hello, World!");
        
        add(myButton);
        add(myLabel);
    }
    
    public static void main(String[] args) {
        Frame myFrame = new Frame("My Panel");
        MyPanel myPanel = new MyPanel();
        
        myFrame.add(myPanel);
        myFrame.setSize(200, 200);
        myFrame.setVisible(true);
    }
}

In this example, we create a custom MyPanel class that extends the Panel class. In the constructor, we create a Button and a Label, and then add them to the Panel using the add() method. Finally, we create a Frame object and add an instance of the MyPanel class to it. We set the size of the Frame and make it visible.

When the code is executed, the Frame will display the Panel with the Button and the Label added to it. You can use a similar approach to add buttons and labels to other containers, such as Frames or Dialogs.

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class MyMouseListener extends MouseAdapter {

    public void mouseClicked(MouseEvent e) {
        System.out.println("Mouse clicked!");
    }
}

In this example, we create a custom MyMouseListener class that extends the MouseAdapter class. We override the mouseClicked() method to print a message to the console when the mouse is clicked.

By extending the MouseAdapter class, we can create a mouse listener object that responds to mouse clicks, without having to implement all the methods defined in the MouseListener interface. This can make the code simpler and easier to read.

import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class MyFrame extends Frame {

    private Image buffer;

    public MyFrame() {
        setSize(400, 400);
        setVisible(true);

        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }

    public void paint(Graphics g) {
        if (buffer == null) {
            buffer = createImage(getWidth(), getHeight());
        }

        Graphics bg = buffer.getGraphics();
        bg.setColor(Color.WHITE);
        bg.fillRect(0, 0, getWidth(), getHeight());
        bg.setColor(Color.BLACK);
        bg.drawString("Hello, World!", 100, 100);

        g.drawImage(buffer, 0, 0, null);
    }

    public static void main(String[] args) {
        new MyFrame();
    }
}

In this example, we create a custom MyFrame class that extends the Frame class. In the paint() method, we create an off-screen buffer using the createImage() method, and then get the Graphics object for the buffer using the getGraphics() method. We draw our graphics to the buffer using the same graphics calls we would use to draw to the screen.

Finally, we get the Graphics object for the screen using the getGraphics() method of our component, and then use the drawImage() method to copy the off-screen buffer to the screen in one step.

By using double-buffering, we can eliminate flicker when rendering graphics in AWT.

import java.awt.Frame;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.awt.CheckboxMenuItem;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class MyFrame extends Frame implements ActionListener {

    private CheckboxMenuItem checkboxMenuItem;

    public MyFrame() {
        setSize(400, 400);
        setVisible(true);

        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

        MenuBar menuBar = new MenuBar();
        setMenuBar(menuBar);

        Menu menu = new Menu("File");
        menuBar.add(menu);

        MenuItem menuItem = new MenuItem("Open");
        menuItem.addActionListener(this);
        menu.add(menuItem);

        checkboxMenuItem = new CheckboxMenuItem("Show hidden files");
        checkboxMenuItem.addActionListener(this);
        menu.add(checkboxMenuItem);
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == checkboxMenuItem) {
            System.out.println("CheckboxMenuItem is " + (checkboxMenuItem.getState() ? "checked" : "unchecked"));
        } else {
            System.out.println("MenuItem clicked");
        }
    }

    public static void main(String[] args) {
        new MyFrame();
    }
}

In this example, we create a custom MyFrame class that extends the Frame class. In the constructor, we create a menu bar using the MenuBar class, and then create a File menu using the Menu class. We add a MenuItem called “Open” to the File menu using the MenuItem class, and a CheckboxMenuItem called “Show hidden files” using the CheckboxMenuItem class.

We register an ActionListener on both the MenuItem and the CheckboxMenuItem using the addActionListener() method. When the user clicks on a MenuItem or a CheckboxMenuItem, the actionPerformed() method is called. In this method, we check if the event was triggered by the CheckboxMenuItem using the getSource() method. If it was, we get the current state of the CheckboxMenuItem using the getState() method, and print a message to the console indicating whether it is checked or unchecked. Otherwise, we simply print a message indicating that the MenuItem was clicked.

import java.awt.*;
import java.awt.event.*;

public class MyFrame extends Frame implements ActionListener, MouseListener {

    private PopupMenu popupMenu;

    public MyFrame() {
        setSize(400, 400);
        setVisible(true);

        addMouseListener(this);

        popupMenu = new PopupMenu("Edit");

        MenuItem cutItem = new MenuItem("Cut");
        MenuItem copyItem = new MenuItem("Copy");
        MenuItem pasteItem = new MenuItem("Paste");

        popupMenu.add(cutItem);
        popupMenu.add(copyItem);
        popupMenu.add(pasteItem);

        cutItem.addActionListener(this);
        copyItem.addActionListener(this);
        pasteItem.addActionListener(this);

        add(popupMenu);
    }

    public void actionPerformed(ActionEvent e) {
        String command = e.getActionCommand();

        if (command.equals("Cut")) {
            System.out.println("Cut item selected");
        } else if (command.equals("Copy")) {
            System.out.println("Copy item selected");
        } else if (command.equals("Paste")) {
            System.out.println("Paste item selected");
        }
    }

    public void mouseClicked(MouseEvent e) {
        if (e.getButton() == MouseEvent.BUTTON3) {
            popupMenu.show(this, e.getX(), e.getY());
        }
    }

    public void mousePressed(MouseEvent e) {
    }

    public void mouseReleased(MouseEvent e) {
    }

    public void mouseEntered(MouseEvent e) {
    }

    public void mouseExited(MouseEvent e) {
    }

    public static void main(String[] args) {
        new MyFrame();
    }
}

In this example, we create a custom MyFrame class that extends the Frame class. In the constructor, we add a MouseListener to the frame to listen for mouse events. We also create a PopupMenu called “Edit” and add three MenuItems to it: “Cut”, “Copy”, and “Paste”. We register an ActionListener on each MenuItem that simply prints a message to the console indicating which item was selected.

In the mouseClicked() method of the MouseListener, we check if the right mouse button was clicked using the getButton() method. If it was, we show the PopupMenu at the location of the mouse click using the show() method.

When you run the example and right-click on the frame, the PopupMenu will appear at the location of the mouse click. If you select one of the items, a message will be printed to the console indicating which item was selected.

      

Go through our study material. Your Job is awaiting.

Recent Posts
Categories