//DesktopTest.java
//Demonstating JDesktopPane
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class DesktopTest extends JFrame{
public DesktopTest(){
super("Using a JDesktopPane");
JMenuBar bar=new JMenuBar();
JMenu addMenu=new JMenu("Add");
JMenuItem newFrame=new JMenuItem("Internal Frame");
addMenu.add(newFrame);
bar.add(addMenu);
setJMenuBar(bar);
final JDesktopPane theDesktop=new JDesktopPane();
getContentPane().add(theDesktop);
newFrame.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){
JInternalFrame aframe=new JInternalFrame("Internal Frame",true,true,true,true);
//内部窗口标题栏,能否改变内部窗大小,能否关闭内部窗,能否最大化,能否最小化
Container c=aframe.getContentPane();
MyJPanel panel=new MyJPanel();
c.add(panel,BorderLayout.CENTER);
aframe.setSize(panel.getImageWidthHeight().width,panel.getImageWidthHeight().height);
aframe.setOpaque(true); //窗口不透明
theDesktop.add(aframe);
}
}
);
setSize(500,400);
show();
}
public static void main(String args[]){
DesktopTest app=new DesktopTest();
app.addWindowListener(
new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
}
);
}
}
class MyJPanel extends JPanel{
private ImageIcon imgIcon;
public MyJPanel(){
imgIcon=new ImageIcon("buganim.gif");
}
public void paintComponent(Graphics g){
super.paintComponent(g);
imgIcon.paintIcon(this,g,0,0);
}
public Dimension getImageWidthHeight(){
return new Dimension(imgIcon.getIconWidth(),imgIcon.getIconHeight());
}
}