Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
265 views
in Technique[技术] by (71.8m points)

java - JOptionPane showInputDialog position

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...