簡體   English   中英

JDesktopPane和BorderLayout

[英]JDesktopPane and BorderLayout

我正在編寫一個試圖模擬物種進化的程序,它的窗口如下所示: 在此處輸入圖片說明

最初,右下角的空白區域是一個面板,用於繪制標本,位置和行進路徑的視覺表示(並不重要)。 但是,您將能夠打開某種窗口,該窗口使您可以創建/編輯不同的項目(例如物種,位置和行進路徑)。 最初,我計划將它們簡單地設置為彈出窗口。 但是,我想我可能將JInternal窗格用於彈出窗口和可視表示屏幕。

因此,在我的JFrames構造函數中:

JDesktopPane pane = new JDesktopPane();
this.setContentPane(pane);
setLayout(new BorderLayout());//To layout the menubar, and the items on the left

panel = new GraphicsPanel(manager);
panel.setVisible(true);

在Graphics Panel構造函數中: super("Graphic Project View",true,false,true,true);

這會將面板鎖定到BorderLayout.CENTER,並填滿整個空間,不允許其他任何東西。 我的猜測是,因為JDesktopPanes使用OverlayLayout,並且當我將布局設置為BorderLayout時會覆蓋OverlayLayout,因此我的InternalFrame剛被添加到了中心。

所以問題是:如何在不保留JInternalFrames功能的情況下布局JMenuBar和左側病房面板之類的東西?

現在,我將通過JFrame.setJMenuBar(JMenuBar)而不是JFrame.add(menuBar,BorderLayout.NORTH)添加JMenuBar,然后將左側的面板更改為JInternal框架,但如果可能的話,我希望照原樣。 如果我可以將DesktopPane添加到BorderLayout.CENTER的JFrame中,然后將其添加到“桌面”窗格中,就可以了。 如果將InternalFrame限於該區域,那么我不在乎,只要它仍可移動即可。

編輯:我如何添加JInternalFrame(對不起,它仍然顯示面板,但它已轉換為JInternalFrame):

panel = new GraphicsPanel(manager);
panel.setSize(desktop.getSize());
panel.setLocation(0,0);
panel.setVisible(true);
desktop.add(panel);

我將從一個JPanel (將其全部JPanel在基本窗格中)開始,它將容納其他容器。

使用邊框布局,我將在基礎窗格的WEST位置添加一個“控件”面板。 CENTER位置,我將添加JDesktopPane

我將主窗口布局設置為BorderLayout並將基本窗格添加到其中。 這將允許您使用JFrame#setJMenuBar來管理菜單欄,同時保持布局結果。

這將允許您包含在桌面上使用JInternalFrame ,而不會影響其余的布局...

簡單的例子

這是一個過於簡化的示例,用於演示上述基本概念。

在此處輸入圖片說明

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridLayout;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class SimpleLayout {

    public static void main(String[] args) {
        new SimpleLayout();
    }

    public SimpleLayout() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JMenuBar mb = new JMenuBar();
                mb.add(new JMenu("File"));
                mb.add(new JMenu("Add"));
                mb.add(new JMenu("Edit"));
                mb.add(new JMenu("Analize"));
                mb.add(new JMenu("About"));

                JFrame frame = new JFrame("Testing");
                frame.setJMenuBar(mb);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new BasePane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class BasePane extends JPanel {

        private JTextArea species;
        private JTextArea locations;
        private JTextArea travelPaths;

        private JDesktopPane desktopPane;

        public BasePane() {                
            setLayout(new BorderLayout());

            desktopPane = new JDesktopPane();

            species = new JTextArea("Species");
            locations = new JTextArea("Locations");
            travelPaths = new JTextArea("TravelPaths");

            JPanel controls = new JPanel(new GridLayout(3, 0));
            controls.add(new JScrollPane(species));
            controls.add(new JScrollPane(locations));
            controls.add(new JScrollPane(travelPaths));

            add(controls, BorderLayout.WEST);
            add(desktopPane);

        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }            
    }        
}

您的要求可能會稍有不同,但是基本概念應該可以使您感動。

根據您的應用程序的結構,我可能還會嘗試將“ Controls窗格分為一個單獨的類。

暫無
暫無

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

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