import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class JInternalFrame1 extends JFrame implements ActionListener {
JDesktopPane desktopPane=null;
int count = 1;
public JInternalFrame1() {
super("JInternalFrame1");
JPanel pane = new JPanel();
pane.setLayout(new BorderLayout());
// Container contentPane = this.getContentPane();
// contentPane.setLayout(new BorderLayout());
JButton b = new JButton("Create New Internal Frames");
b.addActionListener(this);// 當用戶(hù)按下按鈕時(shí),將運行actionPerformed()中的程序
pane.add(b, BorderLayout.SOUTH);
// contentPane.add(b, BorderLayout.SOUTH);
/*
* 建立一個(gè)新的JDesktopPane并加入于contentPane中
*/
desktopPane = new JDesktopPane();
pane.add(desktopPane);
setContentPane(pane);
setSize(350, 350);
setVisible(true);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
/*
* 產(chǎn)生一個(gè)可關(guān)閉、可改變大小、具有標題、可最大化與最小化的Internal Frame.
*/
public void actionPerformed(ActionEvent e) {
JInternalFrame internalFrame = new JInternalFrame("Internal Frame "
+ (count++), true, true, true, true);
internalFrame.setLocation(20, 20);
internalFrame.setSize(200, 200);
internalFrame.setVisible(true);
// 取得JInternalFrame的Content Pane,用以加入新的組件。
JPanel Internalpane2 = new JPanel();
// Container icontentPane = internalFrame.getContentPane();
JTextArea textArea = new JTextArea(3, 15);
textArea.setLineWrap(true);// 設置文本框是否會(huì )自動(dòng)換行
textArea.setWrapStyleWord(true);// 設置文本框按單詞換行
JScrollPane scroll = new JScrollPane(textArea);// 使textArea有滾動(dòng)條
JButton b = new JButton("Internal Frame Button");
/*
* 將JTextArea與JButton對象加入JInternalFrame中。由此得知,JInteranlFrame加入組件
* 的方式與JFrame是一模一樣。
*/
Internalpane2.add(scroll, "Center");
Internalpane2.add(b, "South");
internalFrame.setContentPane(Internalpane2);
// 將JInternalFrame加入JDesktopPane中,如此一來(lái),即使產(chǎn)生很多JInternalFrame,JDesktopPane也
// 能將它們之間的關(guān)系管理得相當良好。
desktopPane.add(internalFrame);
try {
internalFrame.setSelected(true);
} catch (java.beans.PropertyVetoException ex) {
System.out.println("Exception while selecting");
}
}
public static void main(String[] args) {
new JInternalFrame1();
}
}