Advertisement
Guest User

Untitled

a guest
May 23rd, 2017
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.47 KB | None | 0 0
  1. import javax.swing.JFrame;
  2. import javax.swing.JPanel;
  3. import javax.swing.JButton;
  4. import javax.swing.JMenu;
  5. import javax.swing.JMenuItem;
  6. import javax.swing.JMenuBar;
  7. import java.awt.GridLayout;
  8. import java.awt.Color;
  9. import java.awt.event.ActionListener;
  10. import java.awt.event.ActionEvent;
  11.  
  12. public class MenuDemo extends JFrame implements ActionListener {
  13.  
  14.     public static final int WIDTH = 300;    // extending JFrame because MenuDemo needs a JFrame
  15.                                           // gui needs a JFrame..the outline.
  16.                                           // implementing ActionListener whenever we need to perform actions
  17.                                           // implements are for interfaces and interfaces have abstract methods no complete methods
  18.     public static final int HEIGHT = 200;
  19.                                         private static final JButton JButton = null;
  20.    
  21.     private JPanel greenPanel; // creating JPanels
  22.     private JPanel whitePanel;
  23.     private JPanel grayPanel;
  24.    
  25.     public static void main(String[] args)
  26.     {
  27.         MenuDemo gui = new MenuDemo();  
  28.         gui.setVisible(true);
  29.            
  30.     }
  31.    
  32.     public MenuDemo() // MenuDemo constructor
  33.     {
  34.         super("Menu Demonstration");  //
  35.         setSize(WIDTH, HEIGHT);
  36.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  37.         setLayout(new GridLayout(1, 3));
  38.        
  39.         greenPanel = new JPanel();
  40.         greenPanel.setBackground(Color.LIGHT_GRAY);
  41.         add(greenPanel);
  42.        
  43.         whitePanel = new JPanel();
  44.         whitePanel.setBackground(Color.LIGHT_GRAY);
  45.         add(whitePanel);
  46.        
  47.         grayPanel = new JPanel();
  48.         grayPanel.setBackground(Color.LIGHT_GRAY);
  49.         add(grayPanel);
  50.        
  51.         JMenu colorMenu = new JMenu("Add Colors");
  52.        
  53.         JMenuItem greenChoice = new JMenuItem("Green");
  54.         greenChoice.addActionListener(this);
  55.         colorMenu.add(greenChoice);
  56.        
  57.         JMenuItem whiteChoice = new JMenuItem("White");
  58.         whiteChoice.addActionListener(this);
  59.         colorMenu.add(whiteChoise);
  60.        
  61.         JMenuItem grayChoice = new JMenuItem("Gray");
  62.         grayChoice.addActionListener(this);
  63.         colorMenu.add(grayChoice);
  64.        
  65.         JMenuBar bar = new JMenuBar();
  66.         bar.add(colorMenu);
  67.         setJMenuBar(bar);
  68.        
  69.     }
  70.    
  71.     public void actionPerformed(ActionEvent e)
  72.     {
  73.         String buttonSring = e.getActionCommand();
  74.        
  75.         if (buttonString.equals("Green"))
  76.             greenPanel.setBckground(Color.GREEN);
  77.         else if (buttonString.equals("White"))
  78.             whitePanel.setBackground(Color.WHITE);
  79.         else if (buttonString.equals("Gray"))
  80.             grayPanel.setBackground(Color.GRAY);
  81.         else
  82.             System.out.println("Unexpected error.");
  83.     }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement