java 使用JDesktopPane

前提: java工程中src目录下创建文件夹res,其中添加图片文件

1.

public class TestJDesktopPane extends JFrame {


public TestJDesktopPane() {
setSize(400, 400);
setLocation(200, 100);

JPanel panel = new JPanel();

panel.setLayout(new BorderLayout());
TestDesktop desktop = new TestDesktop();
// JFrame 缺省布局使用的是边界布局管理器(BorderLayout).
//JPanel 缺省布局使用的是流式布局管理器(FlowLayout).
panel.add(desktop, BorderLayout.CENTER);

setContentPane(panel);

// setContentPane(desktop, BorderLayout.CENTER);

setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}


public static void main(String[] args) {
new TestJDesktopPane();
}
}


2.

public class TestDesktop extends JDesktopPane {

private Image img;

public TestDesktop() {
URL url = TestDesktop.class.getResource("/res/1.gif");
img = new ImageIcon(url).getImage();
System.out.println(img);
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);

// g.drawImage(img, 0, 0,getWidth(), getHeight(), this);

}


}


你可能感兴趣的:(java)