import
java.awt.*;
import
java.awt.event.*;
import
javax.swing.*;
class
Popup
extends
JFrame
implements
ActionListener {
static
JButton b;
static
JFrame f;
static
JPopupMenu pm;
JLabel l;
Popup()
{
}
public
static
void
main(String[] args)
{
f =
new
JFrame(
"Popup"
);
f.setSize(
400
,
400
);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel p =
new
JPanel();
Popup pop =
new
Popup();
b =
new
JButton(
"click"
);
b.addActionListener(pop);
pm =
new
JPopupMenu(
"Message"
);
JMenuItem m1 =
new
JMenuItem(
"Item1"
);
JMenuItem m2 =
new
JMenuItem(
"Item2"
);
JMenuItem m3 =
new
JMenuItem(
"Item3"
);
JLabel l =
new
JLabel(
"nothing clicked"
);
pm.add(m1);
pm.add(m2);
pm.add(m3);
m1.addActionListener(
new
ActionListener() {
public
void
actionPerformed(ActionEvent e)
{
l.setText(
"Item 1 clicked."
);
}
});
m2.addActionListener(
new
ActionListener() {
public
void
actionPerformed(ActionEvent e)
{
l.setText(
"Item 2 clicked."
);
}
});
m3.addActionListener(
new
ActionListener() {
public
void
actionPerformed(ActionEvent e)
{
l.setText(
"Item 3 clicked."
);
}
});
p.add(b);
p.add(l);
f.add(p);
f.show();
}
public
void
actionPerformed(ActionEvent e)
{
String s = e.getActionCommand();
if
(s.equals(
"click"
)) {
pm.show(f,
200
,
200
);
}
}
}