展开全部
一般来说,我们常把JPanel[面板]放到JFrame窗体中
但是也有一种内部窗体JInternalFrame ,可以放到其他的容器JDesktopPane里,
效果图e69da5e887aa62616964757a686964616f31333363373731如下
代码如下import java.awt.*;
import java.awt.event.*;
import java.beans.PropertyVetoException;
import javax.swing.*;
public class FrameDemo extends JFrame implements ActionListener{
JButton jb;
JDesktopPane jdp;
public FrameDemo(){
jb =new JButton("创建一个内部窗体");
jb.addActionListener(this);
jdp = new JDesktopPane();
add(jdp);
add(jb,BorderLayout.SOUTH);
setSize(500,500);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String[] args) {
new FrameDemo();
}
int index = 1;
int x = 50;
int y=50;
public void actionPerformed(ActionEvent e) {
//内部窗口
JInternalFrame iframe = new JInternalFrame("第"+index+"个内部窗口"
, true, true, true, true);
index++;
iframe.setLocation(x+=10, y+=10);
iframe.setSize(210, 180);
iframe.setVisible(true);
jdp.add(iframe);
try {
iframe.setSelected(true);//被选中
} catch (PropertyVetoException e1) {
e1.printStackTrace();
}
}
}