Java Reference
In-Depth Information
f.setDefaultCloseOperation (JFrame.EXIT - ON - CLOSE);
// Add the DrawingPanel object, set dimensions.
f.getContentPane ().add (drawing - panel);
f.setSize (new Dimension (240,160));
f.setVisible (true);
} // main
} // class FrameApp
/** This JPanel subclass display an image.**/
class DrawingPanel extends JPanel
{
Image fImg;
DrawingPanel (Image img) {
fImg = img;
}
public void paintComponent (Graphics g) {
super.paintComponent (g);
// Put the image at the center of the panel
int img - x = getSize ().width/2 - fImg.getWidth (this)/2;
int img - y = getSize ().height/2 - fImg.getHeight (this)/2;
//Draw image at centered in the middle of the panel
g.drawImage (fImg, img - x, img - y, this);
} // paintComponent
} // class DrawingPanel
7.6.2 A menu bar for a frame
For most applications with frames, you will want to add a menu bar. Creating a
useful menu bar involves at least three classes. The JMenuBar class represents
the menu bar itself while JMenu holds instances of JMenuItem . The following
code shows how to create the frame and menu bar shown in Figure 7.14(b):
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/** This app displays an image and includes a menu bar.**/
public class FrameMenuApp extends JFrame
implements ActionListener
Search WWH ::




Custom Search