0

我正在编写一个程序,试图模拟一个物种的进化,它有一个看起来像这样的窗口: 在此处输入图像描述

最初右下角的空白区域是一个面板,它旨在绘制标本、位置和行进路径的视觉表示(并不重要)。但是,您将能够打开某种窗口,允许您创建/编辑不同的项目(如物种、位置和旅行路径)。最初我计划让那些只是弹出窗口。但是,我想我可能会使用 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);

在图形面板构造函数中:super("Graphic Project View",true,false,true,true);

这会将面板锁定到 BorderLayout.CENTER,并填满整个空间,不允许其他任何内容。我猜这是因为 JDesktopPanes 使用 OverlayLayout,并且当我将布局设置为覆盖 OverlayLayout 的 BorderLayout 时,因此我的 InternalFrame 刚刚被添加到中心。

所以问题是:我如何像现在一样布置 JMenuBar 和左侧面板之类的东西,同时仍然保持拥有 JInternalFrames 的能力?

现在我将通过 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);
4

1 回答 1

3

我会从一个单一的JPanel(让它全部成为基础窗格)开始,它将容纳其他容器。

使用边框布局,我将WEST在基本窗格的位置添加一个“控件”面板。在CENTER我要添加的位置上JDesktopPane

我会将主窗口布局设置为BorderLayout并将基本窗格添加到其中。这将允许您JFrame#setJMenuBar在保持布局结果的同时管理菜单栏。

这将允许您包含JInternalFrame在桌面上使用 s 而不会影响其余布局...

简单示例

这是一个过于简化的示例,用于演示上述基本概念......

在此处输入图像描述

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窗格分离到一个单独的类中。

于 2013-06-24T00:04:53.190 回答