0% found this document useful (0 votes)
8 views2 pages

Ajp Exp 5 Print

The document contains Java code for two GUI applications using AWT and Swing. The first application, 'ColorMenuExample', allows users to change the background color of labels through a menu. The second application, 'MenuDemo1', demonstrates a simple menu bar with options for file operations.

Uploaded by

mohitrohramusic
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)
8 views2 pages

Ajp Exp 5 Print

The document contains Java code for two GUI applications using AWT and Swing. The first application, 'ColorMenuExample', allows users to change the background color of labels through a menu. The second application, 'MenuDemo1', demonstrates a simple menu bar with options for file operations.

Uploaded by

mohitrohramusic
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/ 2

X. Program Code. selectedColor = Color.

BLUE;
import java.awt.*; }
import java.awt.event.*; getContentPane().setBackground(selectedColor);
import javax.swing.*; l.setBackground(selectedColor);
public class ColorMenuExample extends JFrame l1.setBackground(selectedColor);
implements ActionListener { l2.setBackground(selectedColor);
JMenuBar menuBar; l3.setBackground(selectedColor);
JMenu colorMenu; }
JMenuItem redItem, greenItem, blueItem, public static void main(String[] args) {
blackItem; new ColorMenuExample();
Label l, l1, l2, l3; }}
public ColorMenuExample() {
setTitle("Color Menu Example");
setSize(400, 400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new GridLayout(4, 1));
menuBar = new JMenuBar();
colorMenu = new JMenu("Colors");
redItem = new JMenuItem("Red");
greenItem = new JMenuItem("Green");
blueItem = new JMenuItem("Blue");
blackItem = new JMenuItem("Black");
redItem.addActionListener(this);
greenItem.addActionListener(this);
blueItem.addActionListener(this);
blackItem.addActionListener(this);
blackItem.setEnabled(false);
colorMenu.add(redItem);
colorMenu.add(greenItem);
colorMenu.add(blueItem);
colorMenu.add(blackItem);
menuBar.add(colorMenu);
setJMenuBar(menuBar);
add(l);
add(l1);
add(l2);
add(l3);
getContentPane().setBackground(Color.WHITE);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
Color selectedColor = Color.WHITE;
if (e.getSource() == redItem) {
selectedColor = Color.RED;
} else if (e.getSource() == greenItem) {
selectedColor = Color.GREEN;
} else if (e.getSource() == blueItem) {
XIII. Exercise
1.
import java.awt.*;
import java.awt.event.*;

public class MenuDemo1 extends Frame {


MenuBar mb;
MenuItem m1, m2, m3, m4;
Menu mn;
MenuShortcut ms;

MenuDemo1() {
setTitle("MenuBar Demo");
setSize(500, 500);
ms = new MenuShortcut(KeyEvent.VK_X);
mn = new Menu("File");
mb = new MenuBar();
m1 = new MenuItem("New...");
m2 = new MenuItem("Open...");
m3 = new MenuItem("Save As...");
m4 = new MenuItem("Exit", ms);
mn.add(m1);
mn.add(m2);
mn.add(m3);
mn.addSeparator();
mn.add(m4);
mb.add(mn);
setMenuBar(mb);
}

public static void main(String[] args) {


MenuDemo1 md = new MenuDemo1();
md.setVisible(true);
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy