JLayeredPane 与 JPanel

JLayeredPane with JPanel

问题:我无法控制在直方图包中实现更多内容,因此我创建了一个按钮数组并使用 JLayeredPane 将它们覆盖在直方图之上。但是,当 JFrame 放大或收缩时,我无法同时缩放直方图和按钮面板。 JLayedPane 由 2 个 JPanel 组成,参见 MWE。

要重复这个问题,只需 运行 编程并扩展 JFrame。

我已阅读有关 SO 帖子的以下内容; jlayeredpane-with-gridlayout, jlayeredpane-with-a-layoutmanager, jlayeredpane-not-resizing-with-jframe, , automatic-content-resizing-of-jlayeredpane, 以及有一些例子的Oracle page on JLayeredPane

尽管这些链接很有用,但我仍然无法使用 JFrame 将两个 JPanel 都设为 extend/contract。

问题:有没有办法让 JLayeredPane 中的两个 JPanel 在不实现新布局的情况下重新缩放?如果需要新的布局,有人可以提供有关如何做的 MWE 吗?

public class FrameDemo extends JPanel {

    private JLayeredPane layeredPane;
    private final int width = 800;
    private final int height = 800;
    private String[] layerStrings = { "Yellow (0)", "Magenta (1)", "Cyan (2)", "Red (3)", "Green (4)", "Blue (5)" };
    private Color[] layerColors = { Color.yellow, Color.magenta, Color.cyan, Color.red, Color.green, Color.blue };

    public FrameDemo() {
        setLayout(new BorderLayout());
        init();
        addPanels();
        add(layeredPane, BorderLayout.CENTER);
    }

    private void init() {
        this.layeredPane = new JLayeredPane();
        this.layeredPane.setPreferredSize(new Dimension(width, height));
        this.layeredPane.setBorder(BorderFactory.createTitledBorder("Histogram should go here"));
        this.layeredPane.setLayout(new BorderLayout());

    }

    private void addPanels() {
        this.layeredPane.add(createHistogramPanel(), BorderLayout.CENTER, new Integer(1));
        this.layeredPane.add(createButtonPanel(), BorderLayout.CENTER, new Integer(0));
        this.layeredPane.addComponentListener(new ComponentAdapter() {
        @Override
        public void componentResized(ComponentEvent e) {
            Dimension size = layeredPane.getSize(); // get size
            createHistogramPanel().setSize(size); // push size through
            createButtonPanel().setSize(size); // push size trhough
            // otherChildOfLayers.setSize(size); // push size trhough
            layeredPane.revalidate(); // revalidate to see updates
            layeredPane.repaint(); // "Always invoke repaint after
                                    // revalidate"
        }
    });
    }

    private JPanel createHistogramPanel() {

        JPanel histpanel = new JPanel();

        histpanel.setLayout(new GridLayout(2, 3));
        for (int i = 0; i < layerStrings.length; i++) {
            JLabel label = createColoredLabel(layerStrings[i], layerColors[i]);
            histpanel.add(label);
        }
        histpanel.setOpaque(false);
        histpanel.setBounds(10, 10, width, height);
        return histpanel;
    }

    private JLabel createColoredLabel(String text, Color color) {
        JLabel label = new JLabel("");
        label.setVerticalAlignment(JLabel.TOP);
        label.setHorizontalAlignment(JLabel.CENTER);
        label.setOpaque(true);
        label.setBackground(color);
        label.setForeground(Color.black);
        label.setBorder(BorderFactory.createLineBorder(Color.black));
        label.setPreferredSize(new Dimension(120, 120));
        return label;
    }

    private JPanel createButtonPanel() {
        ButtonGroup buttons = new ButtonGroup();

        JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout(new GridLayout(2, 3));
        for (int i = 0; i < 6; i++) {
            final int placer = i + 1;
            JButton freshButton = new JButton();
            freshButton.addActionListener(e -> {
                System.out.println("Button " + placer + " clicked");
            });

            freshButton.setText("Button " + (i + 1));
            freshButton.setOpaque(true);
            freshButton.setContentAreaFilled(false);
            freshButton.setBorderPainted(false);
            freshButton.setBounds(new Rectangle(132, 75 + (i * 20), 40, 20));
            buttonPanel.add(freshButton, null);
            buttons.add(freshButton);

        }
        buttonPanel.setOpaque(false);

        buttonPanel.setBounds(10, 10, width, height);
        return buttonPanel;
    }

    private static void createAndShowGUI() {
        JFrame frame = new JFrame("Testing");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JComponent newContentPane = new FrameDemo();
        newContentPane.setOpaque(true); // content panes must be opaque
        frame.setContentPane(newContentPane);
        frame.setLocationRelativeTo(null);

        frame.pack();

        frame.setVisible(true);
    }

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }

} 

您的代码将无法运行,因为在 componentResized 中您正在创建 new 面板并将尺寸应用于它们。您需要调整添加到分层窗格的 现有 面板的大小。这可以通过将 histogramPanelbuttonPanel 分配为实例变量来完成。