0

我想用 Java 进行 Windows GUI 模拟,但在将窗口定位在桌面图标上时遇到了问题。我尝试将桌面图标(JButtons)和窗口(使用 JInternalFrame 制作)放在同一个容器(JDesktopPane)中,但窗口将图标推开。如果我在 JLayeredPane 中放置 2 个容器(1 个用于桌面,1 个用于 Windows),我猜我将无法单击桌面图标?

还有其他方法可以解决这个问题吗?

编辑:更多信息:

我想用桌面、桌面图标和窗口来模拟 Windows 资源管理器。我希望桌面(带图标)与窗口位于不同的图层上。我该怎么做呢?如果我使用 JLayeredPane,则无法单击图标,因为该层被 windows 层覆盖。

4

1 回答 1

1

您可以添加按钮,JDesktopPane只是不要忘记指定它们的大小和位置,因为JDesktopPane没有布局管理器。这是一个例子:

在此处输入图像描述

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.Rectangle;
import javax.swing.*;

public class TestInternalFrame {

    private static void createAndShowUI() {
        JDesktopPane pane = new JDesktopPane() {
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(300, 200);
            }
        };
        pane.setBackground(Color.WHITE);

        addFrame(pane, new Point(50, 20), "Window1");
        addFrame(pane, new Point(60, 30), "Window2");
        addIcon(pane, new Point(5, 5), "Action", 
                UIManager.getIcon("OptionPane.informationIcon"));

        JFrame frame = new JFrame("Desktop");
        frame.add(pane);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    private static void addFrame(JDesktopPane pane, Point p, String title) {
        JInternalFrame frame = new JInternalFrame(title);
        frame.setSize(200, 100);
        frame.setLocation(p);
        frame.setResizable(true);
        frame.setMaximizable(true);
        frame.setIconifiable(true);
        frame.setClosable(true);
        frame.setVisible(true);
        pane.add(frame);
    }

    private static void addIcon(JDesktopPane pane, Point p, String text, Icon icon) {
        JButton button = new JButton(text, icon);
        button.setVerticalTextPosition(SwingConstants.BOTTOM);
        button.setHorizontalTextPosition(SwingConstants.CENTER);
        button.setBounds(new Rectangle(p, button.getPreferredSize()));
        pane.add(button);
    }

    public static void main(String[] args) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    createAndShowUI();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
}
于 2013-11-25T22:29:49.077 回答