如何让JInternalFrame填充Container并禁用拖动function?
我正在研究一个项目,大型机中有JInternalFrames。 现在,我们需要让它们成为JFrame。 我正在考虑使用JFrame来保存JInternalFrame。 问题是内部框架的标题栏在那里,用户可以拖动它。
有没有办法让内部框架像JFrame中的窗格一样工作? 在互联网上搜索后,我发现有人删除了标题栏。
你对此有什么好主意吗?
谢谢!
更新:也许我走错了路。 真正的问题是JInternal框架无法离开主框架,或者任何方式使它看起来像框架的外侧?
有没有办法让内部框架像JFrame中的窗格一样工作
我不确定你的窗格是什么意思,但我JPanel
? 当然你可以,但为什么,这将是我的问题,除非你想要某种快速浮动面板 ,但是你说你不想要它可拖动? 所以我不确定你的动机,让我厌倦回答……
问题是内部框架的标题栏就在那里
那么这是删除titlepane的代码(在这里找到):
//remove title pane http://www.coderanch.com/t/505683/GUI/java/JInternalframe-decoration BasicInternalFrameTitlePane titlePane =(BasicInternalFrameTitlePane)((BasicInternalFrameUI)jInternalFrame.getUI()).getNorthPane(); jInternalFrame.remove(titlePane);
并且用户可以拖动它。
我发现这通过删除使其可移动的MouseListener
使JInternalFrame
无法移动,但重要的是要注意它不必移除MouseListener
因为用于使其无法打开的方法将删除添加了MouseListener
的NorthPane
因此我们不必自己将其删除:
//remove the listeners from UI which make the frame move BasicInternalFrameUI basicInternalFrameUI = ((javax.swing.plaf.basic.BasicInternalFrameUI) jInternalFrame.getUI()); for (MouseListener listener : basicInternalFrameUI.getNorthPane().getMouseListeners()) { basicInternalFrameUI.getNorthPane().removeMouseListener(listener); }
根据你的标题:
如何使
JInternalFrame
填充Container
只需在JInternalFrame
上调用setSize(int width,int height)
,其参数为JDesktopPane
的width
和height
( JDesktopPane
将通过覆盖getPreferredSize()
来resize)。
哪个会给我们这个:
import java.awt.Dimension; import java.awt.HeadlessException; import java.awt.event.MouseListener; import javax.swing.JDesktopPane; import javax.swing.JFrame; import javax.swing.JInternalFrame; import javax.swing.SwingUtilities; import javax.swing.plaf.basic.BasicInternalFrameTitlePane; import javax.swing.plaf.basic.BasicInternalFrameUI; /** * * @author David */ public class Test { public Test() { createAndShowGUI(); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { new Test(); } }); } private void createAndShowGUI() throws HeadlessException { JFrame frame = new JFrame(); frame.setResizable(false); frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); final JDesktopPane jdp = new JDesktopPane() { @Override public Dimension getPreferredSize() { return new Dimension(300, 300); } }; frame.setContentPane(jdp); frame.pack(); createAndAddInternalFrame(jdp); frame.setVisible(true); } private void createAndAddInternalFrame(final JDesktopPane jdp) { JInternalFrame jInternalFrame = new JInternalFrame("Test", false, false, false, false); jInternalFrame.setLocation(0, 0); jInternalFrame.setSize(jdp.getWidth(), jdp.getHeight()); //remove title pane http://www.coderanch.com/t/505683/GUI/java/JInternalframe-decoration BasicInternalFrameTitlePane titlePane = (BasicInternalFrameTitlePane) ((BasicInternalFrameUI) jInternalFrame.getUI()).getNorthPane(); jInternalFrame.remove(titlePane); /* //remove the listeners from UI which make the frame move BasicInternalFrameUI basicInternalFrameUI = ((javax.swing.plaf.basic.BasicInternalFrameUI) jInternalFrame.getUI()); for (MouseListener listener : basicInternalFrameUI.getNorthPane().getMouseListeners()) { basicInternalFrameUI.getNorthPane().removeMouseListener(listener); } */ jInternalFrame.setVisible(true); jdp.add(jInternalFrame); } }
根据您的要求,我建议您在JFrame
内容窗格中使用简单的JPanel
。