2015-04-06 51 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