簡體   English   中英

恢復已在本地創建的最小化的JFrame

[英]Restore minimized JFrame that has been locally created

我的類Output.java擴展了JPanel 用戶可以從另一個類中單擊一個圖標,然后在本地使用Output.java創建一個JFrame 我們發現有時用戶會最小化該窗口,然后再將其返回。 然后,他將重新單擊該圖標,並重新創建JFrame 通過幾次操作, Output.java類將顯示多次。

我發現可以通過添加以下命令來禁用多個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. 不要繼續創建新的 JFrame。
  2. 而是創建一個引用JFrame的字段,然后還原該字段,而不是新的JFrame。
  3. 創建一個引用JDialog的字段。 如果該字段為null,則在本地創建該字段並將其分配給該字段(這稱為“惰性”創建)。 如果該字段不為null,則不要重新創建它,而只需顯示它即可。
  4. 話雖如此,大多數所有Swing GUI應用程序都應該只有一個JFrame,只有一個主應用程序窗口。 如果需要子窗口,則它們應該是JDialogs,而不是JFrames。 請檢查使用多個JFrame,良好/不良做法?

“惰性”創建的示例:

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);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM