Minimizing Jinternal Frame without clicking the button

后端 未结 2 1781
旧时难觅i
旧时难觅i 2020-11-29 14:22

Is there any way to minimize/maximize the JinternalFrame without clicking the minimize/maximize button at the top-right corner of the jinternalframe?

I followed this

2条回答
  •  独厮守ぢ
    2020-11-29 14:40

    The method getAllFrames() "Returns iconified frames as well as expanded frames." On Mac OS X, iconified frames may be ignored. The example below demonstrates the anomaly and employs a List to compensate.

    Normal iconified

    import java.awt.*;
    import java.awt.event.*;
    import java.beans.PropertyVetoException;
    import java.util.ArrayList;
    import java.util.List;
    import javax.swing.*;
    
    /**
     * @see http://stackoverflow.com/q/9414728/230513
     * @see http://stackoverflow.com/a/9415197/230513
     */
    public class InternalFrameCount extends JFrame {
    
        private static final int SIZE = 300;
        private static final String ICONIFY = "Iconify";
        private static final String RESTORE = "Restore";
        private static final String COUNT = "Count:";
        private JDesktopPane desktop = new JDesktopPane();
        private JLabel count = new JLabel(COUNT);
        private List list = new ArrayList();
    
        public InternalFrameCount() {
            desktop.add(createInternalFrame(1, Color.RED));
            desktop.add(createInternalFrame(2, Color.GREEN));
            desktop.add(createInternalFrame(3, Color.BLUE));
            this.add(desktop);
            JPanel panel = new JPanel();
            panel.add(new JButton(new ButtonAction(ICONIFY)));
            panel.add(new JButton(new ButtonAction(RESTORE)));
            panel.add(count);
            this.add(panel, BorderLayout.SOUTH);
            count.setText(COUNT + desktop.getAllFrames().length);
        }
    
        private class ButtonAction extends AbstractAction {
    
            private boolean iconify;
    
            public ButtonAction(String name) {
                super(name);
                iconify = ICONIFY.equals(name);
            }
    
            @Override
            public void actionPerformed(ActionEvent ae) {
                for (JInternalFrame jif : list) {
                    try {
                        jif.setIcon(iconify);
                    } catch (PropertyVetoException e) {
                        e.printStackTrace(System.err);
                    }
                }
                count.setText(COUNT + desktop.getAllFrames().length);
            }
        }
    
        private JInternalFrame createInternalFrame(int number, Color background) {
            JInternalFrame jif = new JInternalFrame(
                "F" + number, true, true, true, true);
            list.add(jif);
            jif.setBackground(background);
            int topLeft = SIZE / 10 * number;
            jif.pack();
            jif.setBounds(topLeft, topLeft, SIZE / 2, SIZE / 2);
            jif.setVisible(true);
            return jif;
        }
    
        public static void main(String args[]) throws PropertyVetoException {
            System.out.println(System.getProperty("os.name"));
            System.out.println(System.getProperty("java.runtime.version"));
            EventQueue.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    InternalFrameCount frame = new InternalFrameCount();
                    frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
                    frame.pack();
                    frame.setSize(SIZE, SIZE);
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }
    }
    

提交回复
热议问题