JLayeredPane与JPanel合作

时间:2017-10-16 22:55:06

标签: java jpanel jlayeredpane

问题:我没有控制在直方图包中实现更多,所以我创建了一个按钮数组,并使用JLayeredPane将它们叠加在直方图的顶部。但是,当JFrame放大或缩小时,我无法同时获得直方图和按钮面板。 JLayedPane由2个JPanel组成,参见MWE。

要重复此问题,只需运行程序并扩展JFrame。

我在SO帖子上看过以下内容; jlayeredpane-with-gridlayoutjlayeredpane-with-a-layoutmanagerjlayeredpane-not-resizing-with-jframeresize-jframe-to-jpanels-inside-jlayeredpaneautomatic-content-resizing-of-jlayeredpane, 以及Oracle page on JLayeredPane有一些例子

尽管这些链接很有用,但我仍然无法让两个JPanel扩展/收缩JFrame。

问题:有没有办法让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();
            }
        });
    }

} 

1 个答案:

答案 0 :(得分:1)

您的代码无法正常运行,因为在if (this.props.data) { const { personalInfo, address, links, statement } = this.props.data; this.setState({ personalInfo: personalInfo || null, address: address || null, links: links || null, statement: statement || null, }, () => { const { personalInfo, address, links, statement } = this.state; console.log({ personalInfo, address, links, statement }); }) } 您正在创建面板并将尺寸应用于它们。您需要调整添加到分层窗格的现有面板的大小。这可以通过将componentResizedhistogramPanel指定为实例变量来完成。