import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class JInternalFrame1 extends JFrame implements ActionListener{
JDesktopPane desktopPane;
int count = 1;
public JInternalFrame1() {
super("JInternalFrame1");}
Container contentPane = this.getContentPane();
contentPane.setLayout(new BorderLayout());
JButton b = new JButton("Create New Internal Frames");
b.addActionListener(this); //注册按钮监听器
contentPane.add(b, BorderLayout.SOUTH);
desktopPane = new JDesktopPane(); //建立一个desktopPane
contentPane.add(desktopPane); //加入到contentPane中
setSize(350, 350);
show();
addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0);}});}
public void actionPerformed(ActionEvent e) {
JInternalFrame internalFrame = new JInternalFrame( "Internal Frame "+(count++), true, true, true, true); //还是自己看API吧。(如何构造?)
internalFrame.setLocation( 20, 20);
internalFrame.setSize(200, 200);
internalFrame.setVisible(true);
Container icontentPane = internalFrame.getContentPane(); //用来加入新组件,(正如我们开始看的那样,Swing都这样的)JTextArea textArea = new JTextArea(); JButton b = new JButton("Internal Frame Button");
icontentPane.add(textArea,"Center");
icontentPane.add(b,"South");
desktopPane.add(internalFrame);//将internalFrame加入到desktopPane中。如此以来,即使产生很多的internalFrame ,desktopPane也能把他们的层次关系管理的相当良好。
try {internalFrame.setSelected(true);
} catch (java.beans.PropertyVetoException ex) {System.out.println("Exception while selecting");}}
public static void main(String[] args){new JInternalFrame1();}}
/****************************************************JInternalFrame和JFrame是几乎一样的,唯一不同的是JInternalFrame是lightweight(轻量级)的。它不能单独出现必须依附于最上层的组件上,呵呵,他能利用JAVA提供的LOOK and Feel的功能作出不同于操作系统的窗口外型,比JFrame更具弹性。****************************************************** */
Swing中JInternalFrame的使用
最新推荐文章于 2021-03-02 14:59:13 发布