Java Swing MDI

2018-01-09 19:23 更新

Java Swing教程 - Java Swing MDI


MDI代表多文檔接口。在MDI應(yīng)用程序中,打開一個(gè)主窗口,并在主窗口中打開多個(gè)子窗口。

在MDI應(yīng)用程序中,我們可以打開多個(gè)框架,這些框架將是JInternalFrame類的實(shí)例。

我們可以以多種方式組織多個(gè)內(nèi)部框架。 例如,我們可以最大化和最小化它們; 我們可以以平鋪的方式并排查看它們,或者我們可以以級(jí)聯(lián)形式查看它們。

以下是我們將在MDI應(yīng)用程序中使用的四個(gè)類:

  • JInternalFrame
  • JDesktopPane
  • DesktopManager
  • JFrame

JInternalFrame類的實(shí)例充當(dāng)在其父窗口的區(qū)域內(nèi)顯示的子窗口。

JInternalFrame類的實(shí)例充當(dāng)在其父窗口的區(qū)域內(nèi)顯示的子窗口。...

要監(jiān)聽窗口事件(如激活,停用等),我們需要向JInternalFrame添加一個(gè)InternalFrameListener,而不是一個(gè)用于JFrame的WindowListener。

下面的代碼顯示了如何使用一個(gè)實(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)部框架,請(qǐng)使用其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);
    });
  }
}


以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)