【问题标题】:How can I set the location of a JOptionPane?如何设置 JOptionPane 的位置?
【发布时间】: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


【解决方案1】:

您应该将frame 设置为JOptionPane 的父级:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;


public class Main {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                final JFrame frame = new JFrame("Frame");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                JButton button = new JButton("Button");
                frame.add(button);

                frame.setAlwaysOnTop(true);
                frame.setSize(500, 500);
                frame.setLocation(500, 500);

                button.addActionListener(new ActionListener() {

                    @Override
                    public void actionPerformed(ActionEvent e) {
                        JOptionPane optionPane = new JOptionPane("Option Pane");
                        optionPane.showMessageDialog(frame, "Message!"); // if you put null instead of frame, the option pane will be behind the frame
                    }
                });

                frame.setVisible(true);
            }
        });
    }
}

【讨论】:

  • 有趣的是,我将框架设置为“父”而不是空。如果我在您的示例中包含frame.setAlwaysOnTop(true);,则消息会弹出到一边。我会去看看是什么让我的情况有所不同。谢谢!
  • 我已经编辑了我的问题以向您展示我的 SSCCE,我希望这有助于解释我的情况!
  • @Pureferret 确实需要两个不相关的JFrames
  • 不,我没有,当我开始查看代码时我意识到了这一点。我还在想如果我想保留两个是否有解决方案?
  • 您应该将JFrame 设置为父级,当时它会在顶部。
【解决方案2】:

如果

setLocationRelativeTo(null);

由于某种原因在 Windows 上不能很好地显示它我要么将它的大小设置为比启动屏幕稍大一点,这样它会更明显,要么按照 SeniorJD 的建议进行。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-07-11
    • 2013-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多