Java内部窗体的实现
JInternalFrame是一个轻量级窗口,不能单独出现,必须依附在最上层组件上。一般我们会将JInternalFrame加入DesktopPane方便管理,DesktopPane是一种特殊的Layered pane,用来建立虚拟桌面,它可以显示并管理众多JInternalFrame之间的层次关系
代码块1
实现方法1
public class JInternalFrameDemo01 {
public static void main(String args[]){
JFrame frame = new JFrame("Welcome TO CSDN") ; // 实例化窗体对象
Container cont = frame.getContentPane() ;
JDesktopPane desk = new JDesktopPane() ;// 定义窗体的容器
cont.add(desk,BorderLayout.CENTER) ;// 设置显示样式
cont.add(new JLabel("内部窗体"),BorderLayout.SOUTH) ;
JInternalFrame jif = null ;
String picPath = "d:" + File.separator + "csdn.gif" ;
Icon icon = new ImageIcon(picPath) ;// 实例化Icon对象
JPanel pan = null ;
for(int i=0;i<3;i++){
jif = new JInternalFrame("MLDN-" + i,true,true,true,true) ;
pan = new JPanel() ;
pan.add(new JLabel(icon)) ;// 加入标签
jif.setLocation(35 - i * 10 , 35 - i * 10) ; // 设置显示位置
jif.add(pan) ;
jif.pack() ;
jif.setVisible(true) ;
desk.add(jif) ;
}
frame.setSize(230,120) ;// 根据组件自动调整大小
frame.setLocation(300,200) ;
frame.setVisible(true) ;
}
};
代码块2
一般实现方法
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
public class Test extends JFrame {
private JDesktopPane desktopPane = null;
private JInternalFrame internalFrame = null;
public Test() {
super("内部窗口小例子");
init();
getContentPane().add(desktopPane);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(600, 500);
setLocationRelativeTo(null);
setVisible(true);
}
private void init(){
desktopPane = new JDesktopPane();
internalFrame = new JInternalFrame("内部窗体", true, true, true);
desktopPane.add(internalFrame);
internalFrame.setSize(300, 200);
internalFrame.setVisible(true);
}
public static void main(String[] args) {
new Test();
}
}