How to set the location of “JOptionPane.showMessag

2019-01-26 10:11发布

I want to make JOptionPane.showMessageDialog message appear

  • Any place in the screen.
  • Relative to JFrame. (not at the centre of the JFrame)

For example this will display the message at the centre of the JFrame provided as argument thisFrame

 JOptionPane.showMessageDialog(thisFrame, "Your message.");

And this will display the message at the centre of the screen irrelative to any JFrame.

JOptionPane.showMessageDialog(null, "Your message.");
  • what I want is to set the location of the message any place I want

  • what I want is to set the location of the message relative to the JFrame (not at the centre of the JFrame)

How?

3条回答
一纸荒年 Trace。
2楼-- · 2019-01-26 10:32

Try this

JOptionPane pane = new JOptionPane(arguments);
pane.setBounds(x, y,width, height);   
pane.setVisible(true);
查看更多
贪生不怕死
3楼-- · 2019-01-26 10:33
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);
    }
}
查看更多
何必那么认真
4楼-- · 2019-01-26 10:51

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);
查看更多
登录 后发表回答