0

我已经创建了 Jinternal 框架并在其中添加了一些按钮。如果我制作 new JInternalFrame("Blah", false, false, false, true); 那么它就不能正常工作。例如,如果一帧被最大化并且如果尝试最小化另一个面板,那么最大面板也会被最小化。但是如果全部为真,那么它可以正常工作。我的目标是创建我自己的按钮来执行最小/最大/恢复和关闭。请在下面找到代码

package Project;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.*;
import java.beans.PropertyVetoException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.plaf.basic.BasicInternalFrameTitlePane;
import javax.swing.plaf.basic.BasicInternalFrameUI;

public class Test5 implements MouseListener {

    private JDesktopPane pane;
public int mouseCount;
    public static void main(String[] args) {
        new Test5();
    }
    private int xpos = 0;
    private int ypos = 0;

    public Test5() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (Exception exp) {
                    exp.printStackTrace();
                }
                pane = new Test5.DesktopPane() {
                    @Override
                    public Dimension getPreferredSize() {
                        return new Dimension(400, 400);
                    }
                };
                pane.add(newInternalFrame());
                pane.add(newInternalFrame());
                pane.add(newInternalFrame());

                JFrame frame = new JFrame();
                frame.add(pane);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public JInternalFrame newInternalFrame() {
        final JInternalFrame inf = new JInternalFrame("Blah", false, false, false, true);
        inf.setLocation(xpos, ypos);
        inf.setSize(200, 100);
        inf.setVisible(true);

        xpos += 50;
        ypos += 50;

        JPanel jp = new JPanel();
        JLabel jl = new JLabel("panel" + xpos);

        JButton jb = new JButton("_");
        JButton jb2 = new JButton("[]");
        JButton jb3 = new JButton("X");

        inf.setLayout(new GridLayout(2, 2));
        jp.add(jl);
        jp.add(jb);
        jp.add(jb2);
        jp.add(jb3);

        inf.add(jp);
        jb.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                try {
                    if (inf.getLayer() == JDesktopPane.FRAME_CONTENT_LAYER) {
                        pane.remove(inf);
                        pane.add(inf, JDesktopPane.DEFAULT_LAYER);
                        pane.revalidate();
                        pane.repaint();
                    }
                    inf.pack();
                    inf.setIcon(true);
                } catch (PropertyVetoException ex) {
                    ex.printStackTrace();
                }

            }
        });
        jb2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                try {
                    if (inf.isMaximum()) {//restore
                        inf.pack();
                    } else {//maximize
                        inf.setMaximum(true);

                    }
                    pane.remove(inf);
                    pane.add(inf, JDesktopPane.FRAME_CONTENT_LAYER);
                    pane.revalidate();
                    pane.repaint();
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        });
        jb3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                try {
                    inf.dispose();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }

            }
        });


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

    public class DesktopPane extends JDesktopPane {

        @Override
        public void doLayout() {
            super.doLayout();
            List<Component> icons = new ArrayList<Component>(25);
            int maxLayer = 0;

            for (Component comp : getComponents()) {
                if (comp instanceof JInternalFrame.JDesktopIcon) {
                    icons.add(comp);
                    maxLayer = Math.max(getLayer(comp), maxLayer);
                }
            }

            maxLayer++;
            int x = 0;
            for (Component icon : icons) {

                int y = getHeight() - icon.getHeight();
                icon.setLocation(x, y);
                x += icon.getWidth();
                setLayer(icon, maxLayer);
            }
        }
    }

    @Override
    public void mouseClicked(MouseEvent me) {
        mouseCount=me.getClickCount();
        if(mouseCount==2)
        {
            System.out.println("clicked"+mouseCount);
        }
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseEntered(MouseEvent arg0) {
        System.out.println("clicked"+mouseCount);
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseExited(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mousePressed(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseReleased(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }
}
4

1 回答 1

2

你当然喜欢用艰难的方式做事。

该测试基本上会将任何最大化的窗口推到后台。当我有一个最大化的窗口时,我能够毫无问题地最小化和恢复其他框架。

如果要将帧移动到另一层,只需使用JLayered#setLayer(Component, int),您无需删除和添加它们。

public class Test5 implements MouseListener {

    private JDesktopPane pane;
    public int mouseCount;

    public static void main(String[] args) {
        new Test5();
    }

    private int xpos = 0;
    private int ypos = 0;

    public Test5() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (Exception exp) {
                    exp.printStackTrace();
                }
                pane = new Test5.DesktopPane() {
                    @Override
                    public Dimension getPreferredSize() {
                        return new Dimension(400, 400);
                    }

                };
                pane.add(newInternalFrame(), 10);
                pane.add(newInternalFrame(), 10);
                pane.add(newInternalFrame(), 10);

                JFrame frame = new JFrame();
                frame.add(pane);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }

        });
    }

    public JInternalFrame newInternalFrame() {
        final JInternalFrame inf = new JInternalFrame("Blah", false, false, false, true);
        inf.setLocation(xpos, ypos);
        inf.setSize(300, 300);
        inf.setVisible(true);

        xpos += 50;
        ypos += 50;

        JPanel jp = new JPanel();
        JLabel jl = new JLabel("panel" + xpos);

        JButton jb = new JButton("_");
        JButton jb2 = new JButton("[]");
        JButton jb3 = new JButton("X");

        inf.setLayout(new GridLayout(2, 2));
        jp.add(jl);
        jp.add(jb);
        jp.add(jb2);
        jp.add(jb3);

        inf.add(jp);
        jb.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                try {
                    inf.setIcon(true);
//                    if (inf.getLayer() == JDesktopPane.FRAME_CONTENT_LAYER) {
//                        pane.remove(inf);
//                        pane.add(inf, JDesktopPane.DEFAULT_LAYER);
//                        pane.revalidate();
//                        pane.repaint();
//                    }
//                    inf.pack();
//                    inf.setIcon(true);
                } catch (PropertyVetoException ex) {
                    ex.printStackTrace();
                }

            }

        });
        jb2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                try {
                    inf.setMaximum(!inf.isMaximum());
                    if (inf.isMaximum()) {
                        ((JLayeredPane)inf.getParent()).setLayer(inf, 0);
                    } else {
                        ((JLayeredPane)inf.getParent()).setLayer(inf, 10);
                    }
//                    if (inf.isMaximum()) {//restore
//                        inf.pack();
//                    } else {//maximize
//                        inf.setMaximum(true);
//
//                    }
//                    pane.remove(inf);
//                    pane.add(inf, JDesktopPane.FRAME_CONTENT_LAYER);
//                    pane.revalidate();
//                    pane.repaint();
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }

        });
        jb3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                Container parent = inf.getParent();
                inf.dispose();
                parent.remove(inf);
                //                try {
                //                    inf.dispose();
                //                } catch (Exception ex) {
                //                    ex.printStackTrace();
                //                }

            }

        });


//        BasicInternalFrameTitlePane titlePane = (BasicInternalFrameTitlePane) ((BasicInternalFrameUI) inf.getUI()).getNorthPane();
//        inf.remove(titlePane);
        return inf;
    }

    public class DesktopPane extends JDesktopPane {

        @Override
        public void doLayout() {
            super.doLayout();
            List<Component> icons = new ArrayList<Component>(25);
            int maxLayer = 0;

            for (Component comp : getComponents()) {
                if (comp instanceof JInternalFrame.JDesktopIcon) {
                    icons.add(comp);
                    maxLayer = Math.max(getLayer(comp), maxLayer);
                }
            }

            maxLayer++;
            int x = 0;
            for (Component icon : icons) {

                int y = getHeight() - icon.getHeight();
                icon.setLocation(x, y);
                x += icon.getWidth();
                setLayer(icon, maxLayer);
            }
        }

    }

    @Override
    public void mouseClicked(MouseEvent me) {
        mouseCount = me.getClickCount();
        if (mouseCount == 2) {
            System.out.println("clicked" + mouseCount);
        }
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseEntered(MouseEvent arg0) {
        System.out.println("clicked" + mouseCount);
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseExited(MouseEvent arg0) {
        // TODO Auto-generated method stub
    }

    @Override
    public void mousePressed(MouseEvent arg0) {
        // TODO Auto-generated method stub
    }

    @Override
    public void mouseReleased(MouseEvent arg0) {
        // TODO Auto-generated method stub
    }

}

顺便说一句,这...

BasicInternalFrameTitlePane titlePane = (BasicInternalFrameTitlePane) ((BasicInternalFrameUI) inf.getUI()).getNorthPane();
inf.remove(titlePane);

NullPointerExceptionMacOS下会生成一个

更新了错误信息

Windows Look & Feel 实现中有一个“错误”,DesktopManager当另一个窗口被图标化时,它想要恢复所有最大化的窗口。

一个简单的解决方法是DefaultDesktopManager改用。

当您创建 时JDesktopPane,请使用此...

pane.setDesktopManager(new DefaultDesktopManager());

在你添加任何东西之前......

于 2013-02-16T08:55:10.873 回答