还原已在本地创建的最小化 JFrame

Restore minimized JFrame that has been locally created

我的 class Output.java 扩展 JPanel。从另一个 class,用户可以单击一个图标,然后它会在本地创建一个带有 Output.javaJFrame。我们发现有时用户将 window 最小化然后又想要它。然后,他将重新单击该图标并重新创建 JFrame。多做几次,Output.java class 就显示几次。

我发现可以通过添加以下内容来禁用多个 JFrame 创建:

    if (!output.isShowing())
        openPage(output);

但它不会恢复 JFrame。在这种情况下,有没有办法恢复最小化的 JFrame

icon.addMouseListener(new MouseAdapter() {  
    public void mouseClicked(MouseEvent e) {  
        openPage(outputsSlavePane);
    }  
});

private void openPage(final Output panel) {
    JFrame frame = new JFrame("Output");
    frame.add(panel);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
    frame.addWindowListener(new WindowAdapter() {
        @Override
        public void windowClosing(WindowEvent e) {
            panel.setLostFocus();
        }
    });
}

谢谢。

您可以通过调用

恢复最小化的框架
frame.setState(JFrame.NORMAL);

框架的当前状态可以通过

检索
frame.getState() // NORMAL or ICONIFIED
  1. 不要继续创建 JFrames。
  2. 而是创建一个引用 JFrame 的字段,并恢复该字段,而不是新的 JFrame。
  3. 创建一个字段以引用 JDialog。如果该字段为null,则在本地创建它并赋值给该字段(这称为"lazy"创建)。如果该字段不为空,则不要重新创建它,只显示它。
  4. 话虽如此,大多数Swing GUI应用程序应该只有一个JFrame,只有一个主应用程序window。如果需要 sub-windows,它们应该是 JDialogs,而不是 JFrames。请查看 The Use of Multiple JFrames, Good/Bad Practice?

"lazy"创建示例:

import java.awt.Dimension;
import java.awt.Font;
import java.awt.Window;
import java.awt.Dialog.ModalityType;
import java.awt.event.ActionEvent;
import javax.swing.*;

@SuppressWarnings("serial")
public class LazyCreation extends JPanel {
    private static final int PREF_W = 400;
    private static final int PREF_H = PREF_W;
    private Output output = new Output();
    private JDialog outputDialog = null;

    public LazyCreation() {
        add(new JButton(new DisplayOutputAction("Display Output")));
    }

    @Override
    public Dimension getPreferredSize() {
        if (isPreferredSizeSet()) {
            return super.getPreferredSize();
        }
        return new Dimension(PREF_W, PREF_H);
    }

    private class DisplayOutputAction extends AbstractAction {
        public DisplayOutputAction(String name) {
            super(name);
            int mnemonic = (int) name.charAt(0);
            putValue(MNEMONIC_KEY, mnemonic);
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            // lazily create dialog here
            if (outputDialog == null) {
                Window currentWin = SwingUtilities.getWindowAncestor(LazyCreation.this);
                outputDialog = new JDialog(currentWin, "Output Dialog", ModalityType.MODELESS);
                outputDialog.add(output);
                outputDialog.pack();
                outputDialog.setLocationRelativeTo(currentWin);
            }
            outputDialog.setVisible(true);

        }
    }

    private static void createAndShowGui() {
        LazyCreation mainPanel = new LazyCreation();

        JFrame frame = new JFrame("LazyCreation");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.getContentPane().add(mainPanel);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

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


class Output extends JPanel {
    private JLabel label = new JLabel("Output", SwingConstants.CENTER);

    public Output() {
        label.setFont(label.getFont().deriveFont(Font.BOLD, 36));
        add(label);
    }
}