我需要在JPanel的Area中的JInternalFrame中提供一些帮助。我有一个包含的JFrame 当我点击其中一个菜单项i时,JPanel添加到其ContentPane.JFrame包含菜单 需要在内容窗格之上添加JInternal Frame。到目前为止,我已经给出了代码,
JDesktopPane desktop = new JDesktopPane();
desktop.setLayout(new BorderLayout());
JPanel topPanel = new JPanel();
GridBagLayout gbl_contentPane = new GridBagLayout();
gbl_contentPane.columnWidths = new int[] { 0, 0, 0, 0 };
gbl_contentPane.rowHeights = new int[] { 0, 0, 0, 0 };
gbl_contentPane.columnWeights = new double[] { 1.0, 6.0, 1.0,
Double.MIN_VALUE };
gbl_contentPane.rowWeights = new double[] { 0.0, 8.0, 0.0,
Double.MIN_VALUE };
topPanel.setLayout(gbl_contentPane);
JPanel left = new JPanel();
GridBagConstraints gbc_left = new GridBagConstraints();
gbc_left.insets = new Insets(0, 0, 5, 5);
gbc_left.fill = GridBagConstraints.BOTH;
gbc_left.gridx = 0;
gbc_left.gridy = 1;
topPanel.add(left, gbc_left);
JPanel middle = new JPanel();
GridBagLayout gbl_middle = new GridBagLayout();
gbl_middle.columnWeights = new double[] { 1.0 };
middle.setLayout(gbl_middle);
GridBagConstraints gbc_middle = new GridBagConstraints();
gbc_middle.insets = new Insets(0, 0, 5, 5);
gbc_middle.fill = GridBagConstraints.BOTH;
gbc_middle.gridx = 1;
gbc_middle.gridy = 1;
topPanel.add(middle, gbc_middle);
GridBagConstraints gbc = new GridBagConstraints();
JPanel panel1 = new JPanel();
Border eBorder = BorderFactory.createEtchedBorder();
panel1.setBorder(BorderFactory.createTitledBorder(eBorder, "70pct"));
gbc.gridx = 0;
gbc.gridy = 0;
gbc.fill = GridBagConstraints.BOTH;
gbc.weightx = gbc.weighty = 30;
middle.add(panel1, gbc);
panel1.setLayout(new MigLayout("", "[944.00,grow][353.00]",
"[6.00][128.00,grow][]"));
/*lblHeader = new JLabel(
"<html>Indira Institute of Technology<br>Tatabad<br>Karpagam Complex Stop<br>Coimbatre</html>");
lblHeader.setIcon(new ImageIcon(
"C:\\Users\\Prakash\\Desktop\\images.jpg"));
panel1.add(lblHeader, "cell 0 1 2 1,alignx center,aligny center");*/
JPanel panel2 = new JPanel();
gbc = new GridBagConstraints();
panel2.setBorder(BorderFactory.createTitledBorder(eBorder, "30pct"));
gbc.gridy = 1;
gbc.gridwidth = gbc.gridheight = 1;
gbc.fill = GridBagConstraints.BOTH;
gbc.anchor = GridBagConstraints.NORTHWEST;
gbc.weightx = gbc.weighty = 70;
gbc.insets = new Insets(2, 2, 2, 2);
middle.add(panel2, gbc);
panel2.setLayout(new MigLayout(
"",
"[30px][69.00px][144.00][68.00][][159.00px][59.00px][65px][28.00][]",
"[20px:n,grow 50,shrink 50][20px:n,grow 50,shrink 50][20px:n,grow 50,shrink 50][20px:n,grow 50,shrink 50][30.00][48.00:n,grow 50,shrink 50]"));
getContentPane.add(topPanel);
我从未使用过DesktopPane(我不知道如何在这种情况下使用它)和屏幕到目前为止如下, a busy cat http://i60.tinypic.com/nmnvgi.png
现在我需要为之前的屏幕添加JInternalFrame作为Follows, a busy cat http://i59.tinypic.com/2vifih1.png
我知道我只能将一个JInternalFrame添加到DesktopPane.But i 已经用JPanel填充我的ContentPane以显示其内容。我如何实现Jinternal 要在此JFrame中添加的框架。请提供宝贵的建议。
答案 0 :(得分:2)
不是正确的方向。您的原始面板受布局管理器的控制,这意味着当您向其添加JInternalFrame
时,布局管理器想要尝试将其布局。
一般来说,JInternalFrame
想要驻留在一个不受管理的容器中,允许它独立于内容进行定位和调整。
可能的解决方案可能是利用JInternalFrame
的玻璃窗格,有关详细信息,请参阅How to Use Root Panes
另一种解决方案可能是使用JLayeredPane
。基本上,您首先要将JLayeredPane
的布局管理器设置为链接BorderLayout
添加第一个面板,然后在其上方添加第二个透明窗格,没有布局。您可以将JInternalFrame
添加到第二个面板。
有关详细信息,请参阅How to Use Layered Panes
虽然跳出来的问题是......为什么?你为什么不只是使用某种对话?有关详细信息,请参阅How to Make Dialogs
答案 1 :(得分:0)
你真正想要的是什么?
您写道,您已将内容窗格添加到框架中。 JDesktopPane
必须保留自己的空间。如果您没有或者您不想为主框架中的内部框架预留空间,那么您可能甚至不希望它成为主框架的一部分。在这种情况下,您可能希望使用子JDialog
而不是JInternalFrame
s。
因此,要么将JDesktopPane
添加到主框架(保留其空间),要么使用可以是模态的子JDialog
,并且可以重叠主框架的任何部分。 JInternalFrame
仅在JDesktopPane
的区域中可见,而子JDialog
可以&#34;浮动&#34;在你的主框架上。
查看此Oracle文档:How to Use Internal Frames