我已经在
metal和
Windows L&F上进行了测试,您可能需要与
其他一些测试。
基本上,当组件失效并 调用该doLayout方法时
,我们将检查是否存在任何
JInternalframe.JDesktopIcon
组件。然后,我们将它们取出并按照自己的意愿进行布局…
public class TestInternalframe { public static void main(String[] args) { new TestInternalframe(); } private int xpos = 0; private int ypos = 0; public TestInternalframe() { EventQueue.invokeLater(new Runnable() { @Override public void run() { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (Exception exp) { exp.printStackTrace(); } DesktopPane pane = new DesktopPane(); pane.add(newInternalframe()); pane.add(newInternalframe()); pane.add(newInternalframe()); Jframe frame = new Jframe(); frame.add(pane); frame.setDefaultCloseOperation(Jframe.EXIT_ON_CLOSE); frame.setSize(400, 400); frame.setLocationRelativeTo(null); frame.setVisible(true); } }); } public JInternalframe newInternalframe() { JInternalframe inf = new JInternalframe("Blah", true, true, true, true); inf.setLocation(xpos, ypos); inf.setSize(100, 100); inf.setVisible(true); xpos += 50; ypos += 50; return inf; } public class DesktopPane extends JDesktopPane { @Override public void doLayout() { super.doLayout(); List<Component> icons = new ArrayList<Component>(25); for (Component comp : getComponents()) { if (comp instanceof JInternalframe.JDesktopIcon) { icons.add(comp); } } int x = 0; for (Component icon : icons) { int y = getHeight() - icon.getHeight(); icon.setLocation(x, y); x += icon.getWidth(); } } }}Make no mistake, this is a rough hack
Updated
int x = 0;for (Component icon : icons) { int y = getHeight() - icon.getHeight(); icon.setLocation(x, y); x += icon.getWidth(); setLayer(icon, 10); // <--- Add me}对于另一个问题,您只需要将图标移到更高的层即可。在
这个问题中,你确实需要找到一个层足够高的。您可以
使用Integer.MAX_VALUE,但这有点苛刻(并且您可能想要
在其顶部放置一些东西),相反,您可以计算最大
层数,并在其上放置+1…
public void doLayout() { super.doLayout(); List<Component> icons = new ArrayList<Component>(25); int maxLayer = 0; for (Component comp : getComponents()) { if (comp instanceof JInternalframe.JDesktopIcon) { icons.add(comp); maxLayer = Math.max(getLayer(comp), maxLayer); } } maxLayer++; int x = 0; for (Component icon : icons) { int y = getHeight() - icon.getHeight(); icon.setLocation(x, y); x += icon.getWidth(); setLayer(icon, maxLayer); }}您真的需要花时间研究如何使用内部
框架
和如何使用分层
窗格,
因为(至少最后一部分)涵盖了这些……



