Swing中JInternalFrame的使用

来源:互联网 发布:淘宝模版图片地址在吗 编辑:程序博客网 时间:2024/05/08 21:50

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(); //建立一个desktopPanecontentPane.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更具弹性。****************************************************** */


原创粉丝点击