简体   繁体   English

如何设置“JOptionPane.showMessageDialog”的位置

[英]How to set the location of “JOptionPane.showMessageDialog”

I want to make JOptionPane.showMessageDialog message appear 我想让JOptionPane.showMessageDialog消息出现

  • Any place in the screen. 屏幕上的任何位置。
  • Relative to JFrame. 相对于JFrame。 (not at the centre of the JFrame) (不在JFrame的中心)

For example this will display the message at the centre of the JFrame provided as argument thisFrame 例如,这将在作为参数thisFrame提供的JFrame的中心显示消息

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

And this will display the message at the centre of the screen irrelative to any JFrame. 这将在屏幕中心显示与任何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) 我想要的是设置消息相对于JFrame的位置(不在JFrame的中心)

How? 怎么样?

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

Try this 试试这个

JOptionPane pane = new JOptionPane(arguments);
pane.setBounds(x, y,width, height);   
pane.setVisible(true);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM