public class GUI2 extends JPanel implements ActionListener {
private static final long
serialVersionUID = 1L;
private Color color = Color.RED;
private Color nextColor = Color.GREEN;
public GUI2() {
Button b = new
Button("Change");
b.addActionListener(this);
add(b);
}
private JMenu makeMenu() {
JMenu result = new
JMenu("Commands");
result.setMnemonic('c');
JMenuItem item = new
JMenuItem("Change", 'h');
item.addActionListener(this);
result.add(item);
item = new
JMenuItem("Exit", 'x');
item.addActionListener(this);
result.add(item);
return result;
}
public void actionPerformed(ActionEvent
ae) {
String cmmd = ae.getActionCommand();
if (cmmd.equals("Change"))
{
Color temp = nextColor;
nextColor = color;
color = temp;
repaint();
} else if
(cmmd.equals("Exit")) {
System.exit(0);
}
}
public void paintComponent(Graphics gc)
{
super.paintComponent(gc);
Graphics2D gc2d = (Graphics2D) gc;
gc2d.setColor(color);
gc2d.fillOval(20, 20, 50, 50);
}
public static void main(String[] args)
{
AppFrame app = new AppFrame();
app.setTitle("GUI 2");
GUI2 gui = new GUI2();
JMenuBar menuBar = new JMenuBar();
menuBar.add(gui.makeMenu());
app.setJMenuBar(menuBar);
app.setContentPane(gui);
app.display();
}
}