JOptionPane showInputDialog的位置 [英] JOptionPane showInputDialog position

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

问题描述

如何指定JOptionPane的位置。任何人都可以创建一个扩展JOptionPane.showInputDialog的类,它也会占用x,y位置吗?

How can I specify the position of a JOptionPane. Can anyone make a class that extends JOptionPane.showInputDialog that also takes in an x,y position?

推荐答案

你可以使用 JOptionPane的setLocation(... )方法。或者不使用 JOptionPane ,而是扩展 JDialog ,然后在屏幕上指定它的位置。

You can use JOptionPane's setLocation(...) method. OR Instead of using JOptionPane you can extends a JDialog, and then specify it's location on the screen.

以下是@HovercraftFullOfEels建议的一个正常工作代码示例,这个示例将帮助您按照要求从用户那里获得输入:

Here is one working code sample as adviced by @HovercraftFullOfEels , just this example will help you to get input from the user as you asked for :

import javax.swing.*;

public class OptionPaneLocation 
{   
    private void createAndDisplayGUI()
    {       
        JOptionPane optionPane = new JOptionPane("Its me"
                                    , JOptionPane.PLAIN_MESSAGE
                                    , JOptionPane.DEFAULT_OPTION
                                    , null, null, "Please ENTER your NAME here");
        optionPane.setWantsInput(true);             
        JDialog dialog = optionPane.createDialog(null, "TEST");
        dialog.setLocation(10, 20);
        dialog.setVisible(true);
        System.out.println(optionPane.getInputValue());
    }

    public static void main(String... args)
    {
        Runnable runnable = new Runnable()
        {
            public void run()
            {
                new OptionPaneLocation().createAndDisplayGUI();
            }
        };
        SwingUtilities.invokeLater(runnable);
    }
}

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

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