How to set the location of “JOptionPane.showMessageDialog”

空扰寡人 提交于 2019-11-28 12:17:26
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JButton;

public class CustomDialog extends JDialog {
    private JPanel myPanel = null;
    private JButton yesButton = null;
    private JButton noButton = null;

    public CustomDialog(JFrame frame, boolean modal, String myMessage) {
    super(frame, modal);
    myPanel = new JPanel();
    getContentPane().add(myPanel);
    myPanel.add(new JLabel(myMessage));
    yesButton = new JButton("Yes");
    myPanel.add(yesButton);
    noButton = new JButton("No");
    myPanel.add(noButton);
    pack();
    //setLocationRelativeTo(frame);
    setLocation(200, 200); // <--
    setVisible(true);
    }
}

What you need is

    final JOptionPane pane = new JOptionPane("Hello");
    final JDialog d = pane.createDialog((JFrame)null, "Title");
    d.setLocation(10,10);
    d.setVisible(true);

Try this

JOptionPane pane = new JOptionPane(arguments);
pane.setBounds(x, y,width, height);   
pane.setVisible(true);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!