Java Code
/** slight modification of ImgBackground to show use in JTabbedPane
* @author : E.S.Boese
*/
import java.awt.*;
import javax.swing.*;
public class ImgBackgroundInTabs extends JApplet
{
JLabel myplay;
JLabel name;
ImageIcon ic;
ImgPanel ipanel;
JLabel another;
public void init( )
{
myplay = new JLabel( "My Fun Playground" );
myplay.setFont( new Font( "Serif", Font.BOLD, 20 ) );
myplay.setForeground( Color.WHITE );
name = new JLabel( "Elsie" );
name.setForeground( Color.YELLOW );
ic = new ImageIcon( getImage( getCodeBase( ), "houseLake.png" ) );
another = new JLabel( "<HTML><CENTER>GO<BR>DO<BR>SOMETHING!</CENTER>", JLabel.CENTER );
ipanel = new ImgPanel( ic );
ipanel.setLayout( new GridLayout( 2,1 ) );
ipanel.add( myplay );
ipanel.add( name );
// NEW CODE
JTabbedPane pane = new JTabbedPane( );
pane.addTab( "one", ipanel );
pane.addTab( "two", another );
add( pane );
// END new code
}
}
public class ImgPanel extends JPanel
{
ImageIcon icon;
public ImgPanel()
{
super();
}
public ImgPanel( ImageIcon ic )
{
icon=ic;
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
if ( icon != null )
{
Image img = icon.getImage();
int imgWidth = img.getWidth(this); // find the width of the image
int panelWidth = this.getWidth( ); // find the width of the panel
int x = (panelWidth - imgWidth ) / 2; // calculate x to center the img
int imgHeight = img.getHeight( this ); // find height of image
int panelHeight = this.getHeight( ); // find height of panel
int y = (panelHeight - imgHeight ) / 2; // calculate y to center the img
g.drawImage(img,x,y,img.getWidth(this),img.getHeight(this),this);
}
}
}
©2006 by E.S.Boese. All Rights Reserved