JOptionPane showInputDialog position

自古美人都是妖i 提交于 2019-12-17 20:36:27

问题


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?


回答1:


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.

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



回答2:


A JOptionPane can be easily turned into a JDialog (check out the JOptionPane API and it will show you how with example). Then you can set the position of the created JDialog.

e.g., from the documentation:

 JOptionPane pane = new JOptionPane(arguments);
 pane.set.Xxxx(...); // Configure
 JDialog dialog = pane.createDialog(parentComponent, title);
 dialog.setLocation(....);  // added!
 dialog.setModal(....);  // added! Do you want it modal or not?
 // ....
 dialog.setVisible(true);


来源:https://stackoverflow.com/questions/9807890/joptionpane-showinputdialog-position

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!