簡體   English   中英

如何告訴 JDesktopPane 填滿 JFrame 的整個屏幕

[英]How to tell a JDesktopPane to fill the entire screen of JFrame

任何機構請建議代碼如何告訴JDesktopPane在 Java Netbeans IDE填充JFrame的整個屏幕。

  1. JFrame的布局設置為BorderLayout
  2. JDesktopPane添加到 JFrame 的CENTER區域:

     JFrame f = new JFrame(); f.setBounds(50, 50, 500, 400); f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); f.setLayout(new BorderLayout()); JDesktopPane desktopPane = new JDesktopPane(); f.add(desktopPane, BorderLayout.CENTER); f.setVisible(true);

神奇之處在於BorderLayout如何管理其子組件的布局。 添加到BorderLayoutCENTER區域的任何內容都將填充從其容器中獲得的盡可能多的區域。

如果要最大化JDesktopPane內的JInternalFrame ,則應在將其添加到基礎JDesktopPane后對其調用setMaximum(true)

public class JDesktop {

    public static void main(String[] args) {
        JFrame f = new JFrame();
        f.setBounds(50, 50, 500, 400);
        f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        f.setLayout(new BorderLayout());

        JInternalFrame internalFrame1 = new JInternalFrame("Internal Frame 1", true, true, true, true);     
        internalFrame1.setSize(150, 150);
        internalFrame1.setVisible(true);        

        JDesktopPane desktopPane = new JDesktopPane();
        desktopPane.add(internalFrame1);
        try {
            internalFrame1.setMaximum(true);
        } catch (PropertyVetoException e) {
            e.printStackTrace();
        }
        f.add(desktopPane, BorderLayout.CENTER);
        f.setVisible(true);
    }
} 

既然您有了這個想法,那么了解默認值也不錯。 JFrame的默認布局管理器是BorderLayout ,當您向JFrame添加任何內容而不指定該區域的約束時,它將被添加到CENTER區域。 所以你可以在你的代碼中省略這些行:

f.setLayout(new BorderLayout());

您只需使用以下行即可添加desktopPane

f.add(desktopPane);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM