2015-04-06 41 views
0

我有一個應用程序可以讓遊戲自動更新遊戲客戶端。擴展的JDialog(隱藏?)沒有出現在父JFrame的前面?

一旦你按下啓動,它會打開我的DownloadFrame(extends JDialog),並且將這個樣子:

screenshot1

如果單擊任務欄中的應用程序,(也許Windows 8的圖標問題是什麼?)它會像往常一樣最小化應用程序。但是,當您再次最大化應用程序時,JDialog將隱藏,我假設,在父項之後。它看起來像這樣:

screenshot2

這是我爲我的分機的JDialog的代碼。提前道歉,它是凌亂的。

public class DownloadFrame extends JDialog implements Runnable { 

private static final long serialVersionUID = -8764984599528942303L; 

private Background frame; 
private ImageIcon[] gifs; 

private JLabel spinner; 

public DownloadFrame() { 
    super(Loader.application, false); 
    setLayout(null); 
    setUndecorated(true); 
    setAutoRequestFocus(true); 
    new Thread(this).start(); 
    generateBackground(); 
    generateButton(); 
    generateGif(); 
} 

private void generateBackground() { 
    frame = new Background("sub_background.png"); 
    setSize(frame.getWidth(), frame.getHeight()); 
    setBackground(new Color(1.0f, 1.0f, 1.0f, 0.0f)); 
    setLocationRelativeTo(null); 
    setLocation(this.getX(), this.getY() + 5); 
    setLayout(null); 
    setContentPane(frame); 
} 

private void generateGif() { 
    gifs = Utils.generateGifImages(); 
    spinner = new JLabel(gifs[0]); 
    spinner.setBounds(70, 30, gifs[0].getIconWidth(), gifs[0].getIconHeight()); 
    add(spinner); 
} 

private HoverableButton cancel; 

public HoverableButton getCancelButton() { 
    return cancel; 
} 

private void generateButton() { 
    cancel = new HoverableButton(Settings.CANCEL_BUTTON, 75, 145); 
    cancel.addActionListener(new ActionListener() { 
     @Override 
     public void actionPerformed(ActionEvent event) { 
      /* 
      * TODO - 
      * stop the download in progress 
      */ 
      for (HoverableButton button : Loader.application.getPrimaryButtons()) { 
       button.setActive(true); 
       button.setVisible(true); 
      } 
      dispose(); 
     } 
    }); 
    add(cancel); 
} 

private int cycleCount; 

private void cycleGif() { 
    if (spinner == null) { 
     return; 
    } 
    cycleCount++; 
    if (cycleCount > 7) { 
     cycleCount = 0; 
    } 
    spinner.setIcon(gifs[cycleCount]); 
} 

@Override 
public void run() { 
    while (true) { 
     cycleGif(); 
     try { 
      Thread.sleep(100L); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     } 
    } 
} 

如果需要的話,下面是我的使用方法。我敢肯定,大部分內容都可以忽略,只是在下載過程中隱藏了四個按鈕。

((HoverableButton) components[2]).addActionListener(new ActionListener() { 
     @Override 
     public void actionPerformed(ActionEvent e) { 
      HoverableButton source = (HoverableButton) components[2]; 
      if (source.isActive()) { 
       try { 
        Thread.sleep(500L); 
       } catch (Exception ex) { 
        ex.printStackTrace(); 
       } 
       if (panel == null) { 
        panel = new DownloadFrame(); 
        panel.setVisible(true); 
       } else { 
        panel.setVisible(true); 
        panel.getCancelButton().removeHighlight(); 
       } 
       for (HoverableButton button : getPrimaryButtons()) { 
        button.setActive(false); 
        button.setVisible(false); 
        button.removeHighlight(); 
       } 
       /* 
       * TODO - 
       * handle checking for updates/downloading updates 
       */ 
      } 
     } 
    }); 

回答

3

但是當你去再次最大化應用,的JDialog被隱藏,我假設,父

是後面。在創建JDialog時,需要在構造函數中指定對話框的「所有者」JFrame。

因此,您必須創建並製作JFrame,並在創建對話框之前使框架可見。

+0

創建框架,並通過super(Loader.application,false)將其設置爲所有者。構造函數將應用程序框架指定爲父框架,布爾值用於製作對話框模式(我相信)。如果這不起作用,請告訴我! – Null 2015-04-06 16:00:39

+0

如果對話框沒有顯示在框架上方,那麼你做錯了什麼。創建一個簡單的例子來證明我剛纔解釋的概念。這是創建一個JFrame和一個JDialog使用框架作爲所有者,並使兩個窗口可見。然後最小化框架並單擊任務欄上的框架圖標。這種類型的代碼被稱爲[SSCCE](http://sscce.org/),用於演示一個問題。一旦你向自己證明了這個概念,你就會發現你的代碼爲什麼不同。如果你不能使這個概念發揮作用,那麼你有小代碼可以在論壇上發帖。 – camickr 2015-04-06 16:28:57