JPanel-এ background ছবি add করা


Example:
<pre>
/**
*
* @author Farsim,Department of CSE, Shahjalal University of Science and Technology
*/
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JPanel;

class ImagePanel extends JPanel {

private Image img;

public ImagePanel(String img) {
this(new ImageIcon(img).getImage());
}

public ImagePanel(Image img) {
this.img = img;
Dimension size = new Dimension(img.getWidth(null), img.getHeight(null));
setPreferredSize(size);
setMinimumSize(size);
setMaximumSize(size);
setSize(size);
setLayout(new FlowLayout());
}

public void paintComponent(Graphics g) {
g.drawImage(img, 0, 0, null);
}

}
</pre>

<pre>
/**
*
* @author Farsim,Department of CSE, Shahjalal University of Science and Technology
*/

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class ImageTest {

public static void main(String[] args) {
ImagePanel panel = new ImagePanel(new ImageIcon(“C:\\image.png”).getImage());

JFrame frame = new JFrame();
JButton butt=new JButton(“NEw Button”);
// frame.setSize(300,300);
panel.add(butt);
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
}</pre>

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s