... 67      private final static String[] images = { "yellowflowers.png",
68         "purpleflowers.png", "redflowers.png", "redflowers2.png",
69         "lavenderflowers.png" };
70
71        // load image
72        public MyJPanel()
73        {
74           int randomNumber = generator.nextInt(images.length);
75           picture = new ImageIcon(images[randomNumber]); // set icon
76        }
77
78        // display imageIcon on panel
79        @Override
80        public void paintComponent(Graphics g)
81        {
82           super.paintComponent(g);
83           picture.paintIcon(this, g, 0, 0); // display icon
84        }
85
86      // return image dimensions
87      public Dimension getPreferredSize()
88      {
89         return new Dimension(picture.getIconWidth(),
90            picture.getIconHeight());
91      }
92   } // end class MyJPanel

Lines 27–33 create a JMenuBar, a JMenu and a JMenuItem, add the ...

Get Java How to Program, Early Objects, 11th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.