forked from patniemeyer/learningjava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPopUpColorMenu2.java
More file actions
59 lines (52 loc) · 1.61 KB
/
Copy pathPopUpColorMenu2.java
File metadata and controls
59 lines (52 loc) · 1.61 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
//file: PopUpColorMenu.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class PopUpColorMenu2 implements ActionListener
{
public PopUpColorMenu2() {
JFrame frame = new JFrame("PopUpColorMenu v2.0");
JPanel panel = new JPanel();
JButton button = new JButton("Uno");
button.setInheritsPopupMenu(true);
panel.add(button);
button = new JButton("Due");
button.setInheritsPopupMenu(true);
panel.add(button);
button = new JButton("Tre");
button.setInheritsPopupMenu(true);
panel.add(button);
final JPopupMenu colorMenu = new JPopupMenu("Color");
colorMenu.add(makeMenuItem("Red"));
colorMenu.add(makeMenuItem("Green"));
colorMenu.add(makeMenuItem("Blue"));
panel.setComponentPopupMenu( colorMenu );
panel.setBackground(Color.BLUE);
frame.setBackground( Color.RED );
frame.add( BorderLayout.CENTER, panel );
frame.setSize(200,50);
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.setVisible(true);
}
public void actionPerformed( ActionEvent e ) {
System.out.println( e );
System.out.println( e.getSource() );
/*
String color = e.getActionCommand();
if (color.equals("Red"))
selectedComponent.setBackground(Color.red);
else if (color.equals("Green"))
selectedComponent.setBackground(Color.green);
else if (color.equals("Blue"))
selectedComponent.setBackground(Color.blue);
*/
}
private JMenuItem makeMenuItem(String label) {
JMenuItem item = new JMenuItem(label);
item.addActionListener( this );
return item;
}
public static void main(String[] args) {
new PopUpColorMenu2();
}
}