-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJMenuBarExample2.java
More file actions
108 lines (94 loc) · 2.64 KB
/
JMenuBarExample2.java
File metadata and controls
108 lines (94 loc) · 2.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class JMenuBarExample2 extends JFrame {
public JMenuBarExample2()
{
super("JMenuBarExample");
setSize( 600, 600);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setLocation(400,50);
setResizable(true);
Practice2 pc = new Practice2();
setContentPane( pc );
setVisible(true);
}
public static void main(String[] args)
{
// create an object of the class
JMenuBarExample2 jmbe2 = new JMenuBarExample2();
}
}
class Practice2 extends JPanel
{
JMenuBar mb; // menubar
JMenu x, x1; // JMenu
JMenuItem m1, m2, m3, s1, s2; // Menu items
JFrame f; // create a frame
JLabel l; // a label
JPanel pa;
public Practice2()
{
setBackground( Color.WHITE );
runIt2();
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
}
public void runIt2()
{
pa = new JPanel();
l = new JLabel("no task ");
mb = new JMenuBar();
// create a menu
x = new JMenu("March");
x1 = new JMenu("2022");
Monthclass mclass = new Monthclass();
Yearclass yclass = new Yearclass();
// create menuitems
String months[] = {"January","Febuary","March","April","May","June","July","August","September","October","November","December"};
String nums[] = {"2012","2013","2014","2015","2016","2017","2018","2019","2020","2021","2022","2023","2024","2025","2026","2027","2028","2029","2030","2031","2032"};
JMenuItem[] items = new JMenuItem[12];
JMenuItem[] years = new JMenuItem[21];
for (int i = 0; i < 12; i++)
{
items[i] = new JMenuItem(months[i]);
items[i].addActionListener(mclass);
}
for (int k = 0; k < 21; k++)
{
years[k] = new JMenuItem(nums[k]);
years[k].addActionListener(yclass);
}
for (int j = 0; j < 12; j++)
{
x.add(items[j]);
}
for (int l = 0; l < 21; l++)
{
x1.add(years[l]);
}
mb.add(x);
mb.add(x1);
pa.setLayout(new BorderLayout());
pa.setLayout(new GridLayout(5,7));
pa.add(mb, BorderLayout.NORTH);
pa.add(l, BorderLayout.SOUTH);
add(pa);
}
class Monthclass implements ActionListener
{
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
x.setText(command);
}
}
class Yearclass implements ActionListener
{
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
x1.setText(command);
}
}
}