我已经使用 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();
}
}
}
}
别误会,这是一个粗暴的黑客攻击
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);
}
}
你真的需要花时间学习如何使用内部框架 and 如何使用分层窗格因为(至少最后一部分)包含在这些......