如何在JPanel上加图片?
JButton和JLabel都可以上面加图标,想请问一下如何在JPanel上加图片?
[此贴子已经被作者于2006-11-3 17:20:18编辑过]
----------------解决方案--------------------------------------------------------
JPanal上加图片的问题!import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.*;
import java.awt.*;
public class Frame1 extends JFrame {
JPanel contentPane;
JLabel jLabel1 = new JLabel();
BorderLayout borderLayout1 = new BorderLayout();
BG background = new BG();
public Frame1() {
try {
setDefaultCloseOperation(EXIT_ON_CLOSE);
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
}
/**
* Component initialization.
*
* @throws java.lang.Exception
*/
private void jbInit() throws Exception {
contentPane = (JPanel) getContentPane();
contentPane.setLayout(borderLayout1);
setSize(new Dimension(400, 300));
setTitle("Frame Title");
jLabel1.setText("jLabel1");
contentPane.add(background, java.awt.BorderLayout.SOUTH);
}
class BG extends JPanel {
public void paintComponent(Graphics g) {
super.paintComponent(g);
Image img=Toolkit.getDefaultToolkit().getImage("F:\\1.jpg");
g.drawImage(img,0,0,this.getSize().width,this.getSize().height,this);
}
}
----------------解决方案--------------------------------------------------------
顶一下!~~HOHO学到了
----------------解决方案--------------------------------------------------------