日期:2014-05-20 浏览次数:20818 次
import javax.swing.*;
import java.awt.*;
public class Hello extends JPanel {
private Image backgroundImage;
public Hello() {
String path = "/Users/Biao/Desktop/x.png";
backgroundImage = new ImageIcon(path).getImage();
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(backgroundImage, 0, 0, getWidth(), getHeight(), this);
}
private static void createGuiAndShow() {
JFrame frame = new JFrame("");
frame.getContentPane().add(new Hello());
// Set frame's close operation and location in the screen.
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 400);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
createGuiAndShow();
}
}