W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗(yàn)值獎勵
MDI代表多文檔接口。在MDI應(yīng)用程序中,打開一個主窗口,并在主窗口中打開多個子窗口。
在MDI應(yīng)用程序中,我們可以打開多個框架,這些框架將是JInternalFrame類的實(shí)例。
我們可以以多種方式組織多個內(nèi)部框架。 例如,我們可以最大化和最小化它們; 我們可以以平鋪的方式并排查看它們,或者我們可以以級聯(lián)形式查看它們。
以下是我們將在MDI應(yīng)用程序中使用的四個類:
JInternalFrame類的實(shí)例充當(dāng)在其父窗口的區(qū)域內(nèi)顯示的子窗口。
JInternalFrame類的實(shí)例充當(dāng)在其父窗口的區(qū)域內(nèi)顯示的子窗口。...
要監(jiān)聽窗口事件(如激活,停用等),我們需要向JInternalFrame添加一個InternalFrameListener,而不是一個用于JFrame的WindowListener。
下面的代碼顯示了如何使用一個實(shí)例JInternalFrame類:
String title = "A Child Window"; Boolean resizable = true; Boolean closable = true; Boolean maximizable = true; Boolean iconifiable = true; JInternalFrame iFrame = new JInternalFrame(title, resizable, closable, maximizable, iconifiable);
使用添加組件到iFrame
iFrame.add(...)
打包eth框架,使其可見
iFrame.pack(); iFrame.setVisible(true);
JDesktopPane用作所有JInternalFrame的容器。它使用空布局管理器。
JDesktopPane desktopPane = new JDesktopPane(); // Add all JInternalFrames to the desktopPane desktopPane.add(iFrame);
我們可以使用其getAllFrames()方法獲取添加到JDesktopPane的所有JInternalFrames。
JInternalFrame[] frames = desktopPane.getAllFrames();
我們可以使用其getAllFrames()方法獲取添加到JDesktopPane的所有JInternalFrames。...
DefaultDesktopManager實(shí)現(xiàn)DesktopManager接口。
桌面管理器有很多有用的方法。 例如,要以編程方式關(guān)閉內(nèi)部框架,請使用其closeFrame()方法。
desktopPane.getDesktopManager().closeFrame(frame1);
以下代碼演示了如何開發(fā)MDI應(yīng)用程序。
import java.awt.BorderLayout; import java.awt.Dimension; /*from w w w. j av a2 s. co m*/ import javax.swing.JDesktopPane; import javax.swing.JFrame; import javax.swing.JInternalFrame; import javax.swing.JLabel; import javax.swing.SwingUtilities; import javax.swing.UIManager; public class Main extends JFrame { private final JDesktopPane desktopPane = new JDesktopPane(); public Main() { JInternalFrame frame1 = new JInternalFrame("Frame 1", true, true, true, true); JInternalFrame frame2 = new JInternalFrame("Frame 2", true, true, true, true); frame1.getContentPane().add(new JLabel("Frame 1 contents...")); frame1.pack(); frame1.setVisible(true); frame2.getContentPane().add(new JLabel("Frame 2 contents...")); frame2.pack(); frame2.setVisible(true); int x2 = frame1.getX() + frame1.getWidth() + 10; int y2 = frame1.getY(); frame2.setLocation(x2, y2); desktopPane.add(frame1); desktopPane.add(frame2); this.add(desktopPane, BorderLayout.CENTER); this.setMinimumSize(new Dimension(300, 300)); } public static void main(String[] args) { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (Exception e) { e.printStackTrace(); } SwingUtilities.invokeLater(() -> { Main frame = new Main(); frame.pack(); frame.setVisible(true); frame.setExtendedState(frame.MAXIMIZED_BOTH); }); } }
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: