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

Java Color Menu and MenuBar Examples

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)
27 views2 pages

Java Color Menu and MenuBar Examples

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

X. Program Code. selectedColor = Color.

BLUE;
import [Link].*; }
import [Link].*; getContentPane().setBackground(selectedColor);
import [Link].*; [Link](selectedColor);
public class ColorMenuExample extends JFrame [Link](selectedColor);
implements ActionListener { [Link](selectedColor);
JMenuBar menuBar; [Link](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");
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](false);
[Link](redItem);
[Link](greenItem);
[Link](blueItem);
[Link](blackItem);
[Link](colorMenu);
setJMenuBar(menuBar);
add(l);
add(l1);
add(l2);
add(l3);
getContentPane().setBackground([Link]);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
Color selectedColor = [Link];
if ([Link]() == redItem) {
selectedColor = [Link];
} else if ([Link]() == greenItem) {
selectedColor = [Link];
} else if ([Link]() == blueItem) {
XIII. Exercise
1.
import [Link].*;
import [Link].*;

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);
[Link](m1);
[Link](m2);
[Link](m3);
[Link]();
[Link](m4);
[Link](mn);
setMenuBar(mb);
}

public static void main(String[] args) {


MenuDemo1 md = new MenuDemo1();
[Link](true);
}
}

You might also like