【发布时间】:2013-06-25 10:06:16
【问题描述】:
我有一个在程序加载时弹出的启动窗口。这个启动窗口使用setAlwaysOnTop(true) 方法,这是我希望保留的。
当程序的一个单独部分弹出一个 JOptionPane 以通知用户他们需要更新软件时,就会出现问题。在 Linux 上,它出现在 Splash 窗口的前面,但在 Windows 上,它出现在后面,用户几乎看不到它,然后他们可能会坐 30 分钟 - 一个小时才知道为什么它还没有加载。
我想实施的解决方法是
- 强制 JOptionPane 在屏幕上的其他位置弹出
或 - 更改 JOptionPane 出现的位置,使其位于初始屏幕的顶部或底部。
我能看到的唯一可以做到这一点的函数是setLocationRelativeTo(Component myComponent) 方法。但这并不好,因为没有什么比它更好的了(而且没有任何东西存在!)。
这种事情可能吗?如果是这样,我该怎么做?
这是我的 SSCCE(基于 SeniorJD 在他们的回答中的代码):
public class Main {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
final Splash splash =new Splash();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
final Frame frame = new Frame("Frame");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
splash.dispose();
}
});
}
}
@SuppressWarnings("serial")
public class Frame extends JFrame implements ActionListener{
Frame(String title){
super(title);
JButton button = new JButton("Button");
add(button);
setAlwaysOnTop(true);
setSize(500, 500);
setLocationRelativeTo(getParent());
setAlwaysOnTop(true);
button.addActionListener(this);
actionPerformed(null);
}
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane optionPane = new JOptionPane("Option Pane");
optionPane.showMessageDialog(this, "Message: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas tristique gravida congue.");
}
}
@SuppressWarnings("serial")
public class Splash extends JFrame {
public Splash() throws HeadlessException {
setAlwaysOnTop(true);
setUndecorated(true);
setVisible(true);
setSize(600, 450);
setLocationRelativeTo(getParent());
}
}
这或多或少地模拟了我的用户所看到的行为。如何将 JOptionPane 移出闪屏?
【问题讨论】:
-
@AndrewThompson:也没有
J2SE这样的东西。几年前的Java SE。 -
@AndrewThompson 你还好吗?艰难的一天?意思是一样的。请耐心等待。
-
@AndrewThompson 我不在有代码的机器旁,而且我很少使用该类。不管我已经修好了。
标签: java swing location joptionpane