Desktop panel and internal form
Multiple windows need to be used in a GUI application. There can be two management strategies for these windows. One is that each window is an independent form. The advantage is that the buttons and shortcut keys on the main form of the system can be used. Browse all windows; the other is to provide a main form, and then put other windows in the main form, the advantage is that the windows are relatively neat.
When using the second strategy to manage windows, you must use the JDesktopPane class and the JInternalFrame class (desktop panel class and internal frame class). The JDesktopPane class is a container class used to create a virtual desktop; the JInternalFrame class is a lightweight object used to create an internal frame that supports dragging, closing, iconization, resizing, title display, and menu bar.
Common methods in the JDesktopPane class
method | Description |
---|---|
getAllFrames () | Return all JInternalFrame currently displayed in the desktop in the form of an array |
getSeletedFrame() | Get the currently selected desktop |
removeAll() | Remove all JInternalFrame from the desktop |
remove(int index) | Remove the JInternalFrame at the specified index position from the desktop |
setSelectedFrame(JInternalFrame f) | Set the specified JInternalFrame |
setDragMode(int dragMode) | Set the drag mode of the form, the optional static constants of the entry parameters are LIVE_DRAG_MODE and OUTLINE_DRAG_MODE |
JInternalFrame class
When the JInternalFrame class creates a construction method, the maximum number of entry parameters that can be set is 5.
JInternalFrame(String title, boolean resizable, boolean closable, boolean maximizable, boolean iconifiable)
Parameter description:
title : the title of the internal frame.
resizable: Set whether to allow free resizing, set to true to allow it, and false (default) to allow it.
closable: Set whether to provide a "close" button, set to true to provide, and false (default) to provide.
maximizable: Set whether to provide the "maximize" button, set to true to provide, and false (default) to provide.
iconifiable: Set whether to provide a "minimize" button, set to true to provide, and false (default) to provide.
Common methods in the JInternalFrame class
method | Description |
---|---|
setResizable(boolean b) | Set whether to allow free resizing |
setClosable(boolean b) | Set whether to provide a close button |
setMaximizable(boolean b) | Set whether to provide a "maximize" button |
setIconifiable(boolean b) | Set whether to provide a "minimize" button |
setSelected(boolean selected) | Set whether the form is activated, set to true to activate, set to false (default value) to deactivate the form |
isMaximum () | Check if the form is maximized |
isIcon() | Check if the form is minimized |
isClosed() | Check if the form has been closed |
setFrameIcon(Icon icon) | Set the icon displayed in the title bar of the form |
Create a desktop panel and internal form, the code is as follows:
package study.czm;
import java.awt.Container;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
public class Study {
public static void main(String[] args) {
JFrame jf = new JFrame();
jf.setTitle("表格组件");
jf.setSize(600, 500);
jf.setLocationRelativeTo(null);
jf.setDefaultCloseOperation(jf.EXIT_ON_CLOSE);
Container c = jf.getContentPane();
JDesktopPane desktopPan = new JDesktopPane();// 创建桌面面板对象
desktopPan.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE);// 设置内部窗体的拖拽方式
c.add(desktopPan);
// 创建内部窗体
JInternalFrame inFrame1 = new JInternalFrame("第一个内部窗体", true, true, true, true);
JInternalFrame inFrame2 = new JInternalFrame("第二个内部窗体", true, true, true, true);
JInternalFrame inFrame3 = new JInternalFrame("第三个内部窗体", true, true, true, true);
desktopPan.add(inFrame1);
inFrame1.setBounds(50, 50, 200, 200);
inFrame1.setVisible(true);
desktopPan.add(inFrame2);
inFrame2.setBounds(90, 90, 200, 200);
inFrame2.setVisible(true);
desktopPan.add(inFrame3);
inFrame3.setBounds(130, 130, 200, 200);
inFrame3.setVisible(true);
jf.setVisible(true);
}
}
running result: