Java Reference
In-Depth Information
//Add the buttons to the frame.
JPanel south = new JPanel();
south.add(red);
south.add(blue);
south.add(white);
contentPane.add(south, BorderLayout.SOUTH);
//Add the progress bar.
progress = new JProgressBar(0,3);
contentPane.add(progress, BorderLayout.NORTH);
//Register the event listener.
MenuColorChanger changer = new MenuColorChanger(contentPane,
progress);
red.addActionListener(changer);
blue.addActionListener(changer);
white.addActionListener(changer);
createMenu(changer);
}
public void createMenu(MenuColorChanger changer)
{
//Create a menu bar and attach it to this JFrame.
JMenuBar menuBar = new JMenuBar();
this.setJMenuBar(menuBar);
//Create three menus, and add them to the menu bar.
JMenu fileMenu = new JMenu(“File”);
JMenu colorMenu = new JMenu(“Color”);
JMenu helpMenu = new JMenu(“Help”);
menuBar.add(fileMenu);
menuBar.add(colorMenu);
menuBar.add(helpMenu);
//Add three menu items to the “Color” menu.
JMenuItem redMenuItem = new JMenuItem(“Red”);
JMenuItem blueMenuItem = new JMenuItem(“Blue”);
JMenuItem whiteMenuItem = new JMenuItem(“White”);
colorMenu.add(redMenuItem);
colorMenu.add(blueMenuItem);
colorMenu.add(whiteMenuItem);
redMenuItem.addActionListener(changer);
Search WWH ::




Custom Search