0

我希望有一个JComponent放置并与其他组件共享空间,具体取决于我在运行时选择的配置。对于第一个配置,JComponent放置在BorderLayout东侧或西侧,并与框架中的其他组件共享空间。对于第二种配置,JComponent必须将其放在所有其他组件的顶部,将它们重叠并让它们调整大小。
我所做的是创建一个JPanel带有 a 的BorderLayout并将其全部放置在我JComponent的 NORTH、CENTER 和 EAST 侧。这是初始配置。我将它JPanel放在JLayeredPaneDefaultLayer 层中,并将其设置JLayeredPane为我的JFrame. 我想用它JLayeredPane来放置东侧JComponent在上层(如果选择了第二个配置)并将其放回 DefaultLayer(如果选择了第一个配置)。

希望我很清楚并提供足够的细节以获得一些帮助。提前致谢。

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JToggleButton;

/**
 * @see http://stackoverflow.com/q/13776251/230513
 */
public class NewJavaGUI {

    class Desktop {

        private JPanel desktop;
        private JLayeredPane layeredPane;
        private HidingArea hidingArea;
        private final JToggleButton toggleShowHide;

        public Desktop() {
            desktop = new JPanel(new BorderLayout(5, 5));
            desktop.setBounds(0, 0, 600, 400);
            layeredPane = new JLayeredPane(){

                @Override
                public Dimension getPreferredSize() {
                    return new Dimension(800, 400);
                }

            };
            layeredPane.add(desktop);
            NorthArea northArea = new NorthArea();
            northArea.setLayout(new BoxLayout(northArea, BoxLayout.LINE_AXIS));
            toggleShowHide = new JToggleButton("Show");
            toggleShowHide.addItemListener(new ShowHideItemListener());
            JRadioButton conf1btn = new JRadioButton("In desktop");
            conf1btn.setOpaque(false);
            JRadioButton conf2btn = new JRadioButton("In layered pane");
            conf2btn.setOpaque(false);
            ButtonGroup group = new ButtonGroup();
            group.add(conf1btn);
            group.add(conf2btn);
            northArea.add(conf1btn);
            northArea.add(conf2btn);
            northArea.add(Box.createHorizontalGlue());
            northArea.add(toggleShowHide);
            conf1btn.addActionListener(new SetComponentInDesktopListener());
            conf2btn.addActionListener(new SetComponentInLayeredPaneListener());
            desktop.add(northArea, BorderLayout.PAGE_START);
            desktop.add(new CenterArea(), BorderLayout.CENTER);
            hidingArea = new HidingArea();
            desktop.add(hidingArea, BorderLayout.LINE_END);
            conf1btn.setSelected(true);
        }

        /**
        * The layered pane is added to the contentPane of a JFrame
        */
        JLayeredPane getComponent() {
            return layeredPane;
        }

        private class HidingArea extends JPanel {

            public HidingArea() {
                setBackground(Color.darkGray);
            }

            @Override
            public Dimension getPreferredSize() {
                return new Dimension(200, 400);
            }

            @Override
            public Dimension getMinimumSize() {
                return new Dimension(200, 350);
            }
        }

        private class NorthArea extends JPanel {

            public NorthArea() {
                setBackground(Color.gray);
            }

            @Override
            public Dimension getPreferredSize() {
                return new Dimension(600, 50);
            }

            @Override
            public Dimension getMinimumSize() {
                return new Dimension(600, 50);
            }
        }

        private class CenterArea extends JPanel {

            public CenterArea() {
                setBackground(Color.white);
            }

            @Override
            public Dimension getPreferredSize() {
                return new Dimension(400, 400);
            }

            @Override
            public Dimension getMinimumSize() {
                return new Dimension(400, 400);
            }
        }

        /**
        * Hide or show the area contained in the JLayeredPane.
        */
        private class ShowHideItemListener implements ItemListener {

            @Override
            public void itemStateChanged(ItemEvent e) {
                JToggleButton toggle = (JToggleButton) e.getSource();
                if (e.getStateChange() == ItemEvent.SELECTED) {
                    toggle.setText("Hide");
                    hidingArea.setBounds(
                        getBounds().width - hidingArea.getPreferredSize().width,
                        getBounds().height - hidingArea.getPreferredSize().height,
                        hidingArea.getPreferredSize().width,
                        hidingArea.getPreferredSize().height);
                } else {
                    toggle.setText("Show");
                    hidingArea.setBounds(
                        getBounds().width,
                        getBounds().height - hidingArea.getPreferredSize().height,
                        hidingArea.getPreferredSize().width,
                        hidingArea.getPreferredSize().height);
                }
            }
        }

        /**
        * @return the rectangular dimensions of the desktop.
        */
        private Rectangle getBounds() {
            return desktop.getBounds();
        }

        /**
        * Add Hiding area to desktop.
        */
        private class SetComponentInDesktopListener implements ActionListener {

            @Override
            public void actionPerformed(ActionEvent e) {
                toggleShowHide.setEnabled(false);
                Component[] components = desktop.getComponents();
                boolean toAdd = true;
                for (Component component : components) {
                    if (component.equals(hidingArea)) {
                        toAdd = false;
                    }
                }
                if (toAdd) {
                    desktop.add(hidingArea, BorderLayout.LINE_END);
                }
            }
        }

        /**
        * Remove Hiding area from desktop and add it to the JLayeredPane.
        */
        private class SetComponentInLayeredPaneListener implements ActionListener {

            @Override
            public void actionPerformed(ActionEvent e) {
                toggleShowHide.setEnabled(true);
                desktop.remove(hidingArea);
                getComponent().add(hidingArea, (Integer) (JLayeredPane.DEFAULT_LAYER + 50));
            }
        }
    }

    private void display() {
        JFrame f = new JFrame("NewJavaGUI");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Desktop d = new Desktop();
        f.add(d.getComponent());
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new NewJavaGUI().display();
            }
        });
    }
}
4

0 回答 0