如何设置JOptionPane的位置? [英] How can I set the location of a JOptionPane?

查看:998
本文介绍了如何设置JOptionPane的位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个启动窗口,在程序加载时弹出。这个启动窗口使用方法 setAlwaysOnTop(true),这是我希望保留的。

I have a splash window that pops up while the program loads. This splash window uses the method setAlwaysOnTop(true), which is something I would prefer to keep.

问题当程序的一个单独部分弹出一个JOptionPane来通知用户他们需要更新软件时。在Linux上,它出现在Splash窗口的前面,但在Windows上它显示在后面,用户几乎看不到,然后可能会坐30分钟 - 小时才能找到它尚未加载的原因。

The issue comes when a separate part of the program pops up a JOptionPane to notify the user they need to update the software. On Linux this appears in front of the Splash window, but on Windows it appears behind, barely visible to the user who then might sit for 30mins - hour before finding out why it's not loaded yet.

我想要实现的修复是


  • 强制JOptionPane在屏幕上的其他位置弹出


  • 更改JOptionPane出现的位置,使其位于初始屏幕的顶部或底部。

我能看到的唯一可以做到这一点的函数是 setLocationRelativeTo(Component myComponent)方法。但这并不好,因为它没有什么比相对更好的了(并且没有任何东西!)。

The only function I can see that would do this is the setLocationRelativeTo(Component myComponent) method. But this is no good as there is nothing better for it to be relative to (and nothing present!).

这种事情有可能吗?如果是这样我该如何去做?

Is this sort of thing possible? If so how do I go about doing it?

这是我的SSCCE(基于他们答案中的SeniorJD代码):

Here is my SSCCE (based off of SeniorJD's code in their answer) :

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移出启动画面?

This more ore less emulates the behaviour my users are seeing. How do I move the JOptionPane out of the way of the splash screen?

推荐答案

您应该设置 frame 作为的父亲

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);
            }
        });
    }
}

这篇关于如何设置JOptionPane的位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆