Hey;
I am following CoreJava and came to Event Handling chapter. I am trying to show a popup menu when the red button is clicked, but not works. Background color changes but no popup menu is shown. What is the problem?
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package corejava_eventhandling;
import java.awt.EventQueue;
import javax.swing.JFrame;
/**
*
* @author melt
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
Frame frame = new Frame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
);
}
}
package corejava_eventhandling;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Frame extends JFrame
{
private static final int DEFAULT_WIDTH = 300;
private static final int DEFAULT_HEIGHT = 200;
private JPanel panel;
public Frame()
{
setTitle("Button Test");
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
JButton yellowButton = new JButton("Yellow");
JButton blueButton = new JButton("Blue");
JButton redButton = new JButton("Red");
panel = new JPanel();
panel.add(yellowButton);
panel.add(blueButton);
panel.add(redButton);
add(panel);
ColorAction yellowAction = new ColorAction(Color.YELLOW);
ColorAction blueAction = new ColorAction(Color.BLUE);
ColorAction redAction = new ColorAction(Color.RED);
Panic panicAction = new Panic();
yellowButton.addActionListener(yellowAction);
blueButton.addActionListener(blueAction);
redButton.addActionListener(redAction);
redButton.addActionListener(panicAction);
}
private class Panic implements ActionListener
{
private String message = "Warning ! Nuclear Missile Launched!!!";
public void actionPerformed(ActionEvent event)
{
PopupMenu pop = new PopupMenu(message);
Frame.this.panel.add(pop);
panel.setVisible(true);
}
}
private class ColorAction implements ActionListener
{
private Color backgroundColor;
public ColorAction(Color c)
{
backgroundColor = c;
}
public void actionPerformed(ActionEvent event)
{
panel.setBackground(backgroundColor);
}
}
}