如何最小化容器内的面板

时间:2013-02-14 07:30:27

标签: java swing jpanel layout-manager flowlayout

我有三个按钮关闭,最小和最大。
当我想要最大化时,它将采用主容器的形状并重叠所有面板,当我关闭它时,只有该面板受到影响。但是当我点击最小按钮时,它会最小化到我不想要的任务栏。
当你点击最小化按钮然后它在主框架内被最小化时,我希望它在内部框架内部像内部框架一样。

这是代码

package Project;

import java.awt.Color;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.border.LineBorder;


public class MinPanel {

    public MinPanel() {
        createAndShowGui();
    }


    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new MinPanel();
            }
        });
    }

    private void createAndShowGui() {
        JFrame frame = new JFrame();
        frame.setSize(300, 500);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        final JPanel mainPanel = new JPanel(new GridLayout(2, 2));

        for (int i = 0; i < 4; i++) {
            final int num = i;
            OmniPanel op = new OmniPanel(mainPanel, frame) {
                @Override
                public JPanel createPanel() {
                    JPanel p = createSimplePanelInterface();
                    p.add(new JLabel("Panel " + (num + 1)));
                    return p;
                }

                @Override
                void toPanel() {
                    super.toPanel();
                    System.out.println("Frame requested to be brought to panel");
                }
            };
            mainPanel.add(op.getPanel());
        }

        frame.add(mainPanel);

        //frame.pack();
        frame.setVisible(true);
    }
}

abstract class OmniPanel {

    protected JFrame frame;
    protected JPanel panel;
    boolean maximized = false;
    private final JComponent owner;
    private final JFrame ownerFrame;

    public OmniPanel(JComponent owner, JFrame con) {
        this.owner = owner;
        initOmniPanel();
        this.ownerFrame = con;
    }

    private void initOmniPanel() {
        panel = createPanel();
        createFrame();
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowDeiconified(WindowEvent we) {
                super.windowDeiconified(we);
                toPanel();
            }
        });
    }

    public JPanel getPanel() {
        return panel;
    }

    public JFrame getFrame() {
        return frame;
    }

    public boolean goFrame() {
        frame.add(panel);
        frame.pack();
        frame.setState(JFrame.ICONIFIED);
        frame.setVisible(true);
        return true;
    }

    protected void createFrame() {
        frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    }

    void toPanel() {
        frame.remove(panel);
        frame.dispose();
        owner.add(panel);
        owner.revalidate();
        owner.repaint();
    }

    public JPanel createSimplePanelInterface() {
        JPanel p = new JPanel();
        JButton close = new JButton("X");
        JButton minimize = new JButton("_");
        JButton maximize = new JButton("[]");
        p.add(close);
        p.add(minimize);
        p.add(maximize);
        close.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                if (maximized) {
                    maximized = false;
                    ownerFrame.setGlassPane(new JComponent() {
                    });
                    ownerFrame.revalidate();
                    ownerFrame.repaint();
                } else {
                    removePanelFromOwner();
                    getFrame().dispose();
                }
            }
        });
        minimize.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                if (maximized) {
                    maximized = false;
                   ownerFrame.setGlassPane(new JComponent() {
                    });

                    owner.add(panel);
                    owner.revalidate();
                    owner.repaint();
                    ownerFrame.revalidate();
                    ownerFrame.repaint();
                } else {
                    removePanelFromOwner();
                    goFrame();


                frame.setState(Frame.ICONIFIED);
                }
            }
        });
        maximize.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                if (maximized) {
                    return;
                }
                maximized = true;
                removePanelFromOwner();
                ownerFrame.setGlassPane(panel);
                ownerFrame.revalidate();
                ownerFrame.repaint();
                panel.setVisible(true);//
            }
        });
        p.setBorder(new LineBorder(Color.black));
        return p;
    }

    private void removePanelFromOwner() {
        owner.remove(getPanel());
        owner.revalidate();
        owner.repaint();
    }

    abstract JPanel createPanel();
}

1 个答案:

答案 0 :(得分:3)

我建议您创建一个内部框架,然后添加按钮。如果您不想添加按钮,那么您可以使内部框架参数为true

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyVetoException;
import javax.swing.JButton;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.SwingUtilities;
import javax.swing.plaf.basic.BasicInternalFrameTitlePane;
import javax.swing.plaf.basic.BasicInternalFrameUI;

public class MinPanel {

    public MinPanel() throws HeadlessException, PropertyVetoException {
        createAndShowGUI();
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    new MinPanel();
                } catch (HeadlessException ex) {
                } catch (PropertyVetoException ex) {
                }

            }
        });
    }

    private void createAndShowGUI() throws HeadlessException, PropertyVetoException {
        JFrame frame = new JFrame();
        frame.setResizable(true);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        final JDesktopPane jdp = new JDesktopPane() {
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(400, 400);
            }
        };

        frame.setContentPane(jdp);
        frame.pack();

        createAndAddInternalFrame(jdp, 0, 0);
        createAndAddInternalFrame(jdp, 200, 0);

        frame.setVisible(true);
    }

    private void createAndAddInternalFrame(final JDesktopPane jdp, int x, int y) throws PropertyVetoException {
        final JInternalFrame jInternalFrame = new JInternalFrame("Test1", false, false, false, false);
        jInternalFrame.setLocation(x, y);

        jInternalFrame.setLayout(new GridLayout(2, 2));
        jInternalFrame.setSize(200, 200);//testing
        JButton jb = new JButton("min");
        jInternalFrame.add(jb);
        jb.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                try {
                    jInternalFrame.setIcon(true);
                } catch (PropertyVetoException ex) {
                }

            }
        });
        BasicInternalFrameTitlePane titlePane = (BasicInternalFrameTitlePane) ((BasicInternalFrameUI) jInternalFrame.getUI()).getNorthPane();
        jInternalFrame.remove(titlePane);


        jInternalFrame.setVisible(true);
        jdp.add(jInternalFrame);
    }
}