[英]JFrame - Icon not showing on Sys tray when JFrame has been minimised
[英]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
“惰性”創建的示例:
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.